AuthenticationExampleUITests.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import XCTest
  15. class AuthenticationExampleUITests: XCTestCase {
  16. var app: XCUIApplication!
  17. override func setUp() {
  18. super.setUp()
  19. continueAfterFailure = false
  20. app = XCUIApplication()
  21. app.launch()
  22. }
  23. override func tearDown() {
  24. super.tearDown()
  25. signOut()
  26. }
  27. func testAuth() {
  28. // Verify that Auth Example app launched successfully
  29. XCTAssertTrue(app.navigationBars["Firebase Auth"].exists)
  30. }
  31. func testAuthAnonymously() {
  32. app.staticTexts["Anonymous Authentication"].tap()
  33. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  34. XCTAssertTrue(app.navigationBars["User"].exists)
  35. let isAnonymousCell = app.cells.containing(.staticText, identifier: "Is User Anonymous?")
  36. .element
  37. XCTAssertTrue(isAnonymousCell.staticTexts["Yes"].exists, "The user should be anonymous")
  38. }
  39. func testAuthExistingAccount() {
  40. // Setup existing user for duplicate test below.
  41. let existingEmail = "existing@test.com"
  42. let existingPassword = "existingPW"
  43. app.staticTexts["Email & Password Login"].tap()
  44. let testEmail = existingEmail
  45. app.textFields["Email"].tap()
  46. app.textFields["Email"].typeText(testEmail)
  47. let testPassword = existingPassword
  48. app.textFields["Password"].tap()
  49. app.textFields["Password"].typeText(testPassword)
  50. app.buttons["Login"].tap()
  51. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  52. XCTAssertTrue(app.navigationBars["User"].exists)
  53. XCTAssertTrue(
  54. app.staticTexts[testEmail].exists,
  55. "The user should be signed in and the email field should display their email."
  56. )
  57. }
  58. func testAuthExistingAccountWrongPassword() {
  59. app.staticTexts["Email & Password Login"].tap()
  60. let testEmail = "test@test.com"
  61. app.textFields["Email"].tap()
  62. app.textFields["Email"].typeText(testEmail)
  63. app.textFields["Password"].tap()
  64. app.textFields["Password"].typeText("wrong password")
  65. app.buttons["Login"].tap()
  66. wait(forElement: app.alerts.staticTexts["Error"], timeout: 5.0)
  67. XCTAssertTrue(app.alerts.staticTexts["Error"].exists)
  68. // Dismiss alert that password was incorrect
  69. app.alerts.buttons["OK"].tap()
  70. // Go back and check that there is no user that is signed in
  71. app.navigationBars.buttons.firstMatch.tap()
  72. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  73. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  74. XCTAssertEqual(
  75. app.cells.count,
  76. 0,
  77. "The user shouldn't be signed in and the user view should have no cells."
  78. )
  79. }
  80. func testCreateAccountBadPassword() {
  81. app.staticTexts["Email & Password Login"].tap()
  82. let testEmail = "test@test.com"
  83. app.textFields["Email"].tap()
  84. app.textFields["Email"].typeText(testEmail)
  85. app.textFields["Password"].tap()
  86. // Enter an invalid password that is "too short"
  87. app.textFields["Password"].typeText("2shrt")
  88. app.buttons["Create Account"].tap()
  89. wait(forElement: app.alerts.staticTexts["Error"], timeout: 5.0)
  90. XCTAssertTrue(app.alerts.staticTexts["Error"].exists)
  91. // Dismiss alert that password was incorrect
  92. app.alerts.buttons["OK"].tap()
  93. // Go back and check that there is no user that is signed in
  94. app.navigationBars.buttons.firstMatch.tap()
  95. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  96. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  97. XCTAssertEqual(
  98. app.cells.count,
  99. 0,
  100. "The user shouldn't be signed in and the user view should have no cells."
  101. )
  102. }
  103. func testCreateAlreadyExistingAccount() {
  104. app.staticTexts["Email & Password Login"].tap()
  105. let testEmail = "test@test.com"
  106. app.textFields["Email"].tap()
  107. app.textFields["Email"].typeText(testEmail)
  108. let testPassword = "test12"
  109. app.textFields["Password"].tap()
  110. app.textFields["Password"].typeText(testPassword)
  111. app.buttons["Create Account"].tap()
  112. wait(forElement: app.alerts.staticTexts["Error"], timeout: 5.0)
  113. XCTAssertTrue(app.alerts.staticTexts["Error"].exists)
  114. // Dismiss alert that password was incorrect
  115. app.alerts.buttons["OK"].tap()
  116. // Go back and check that there is no user that is signed in
  117. app.navigationBars.buttons.firstMatch.tap()
  118. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  119. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  120. XCTAssertEqual(
  121. app.cells.count,
  122. 0,
  123. "The user shouldn't be signed in and the user view should have no cells."
  124. )
  125. }
  126. func testCreateAccountCorrectPassword() {
  127. app.staticTexts["Email & Password Login"].tap()
  128. let newEmail = "\(Date().timeIntervalSince1970)_test@test.com"
  129. app.textFields["Email"].tap()
  130. app.typeText(newEmail)
  131. let newPassword = "new password"
  132. app.textFields["Password"].tap()
  133. app.typeText(newPassword)
  134. app.buttons["Create Account"].tap()
  135. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  136. XCTAssertTrue(app.navigationBars["User"].exists)
  137. XCTAssertTrue(
  138. app.staticTexts[newEmail].exists,
  139. "The user should be signed into the new account."
  140. )
  141. }
  142. func DRAFT_testGoogleSignInAndLinkAccount() {
  143. let interruptionMonitor = addUIInterruptionMonitor(withDescription: "Sign in with Google") {
  144. alert -> Bool in
  145. alert.buttons["Continue"].tap()
  146. return true
  147. }
  148. app.staticTexts["Google"].tap()
  149. app.tap() // Triggers the UIInterruptionMonitor
  150. let testEmail = ""
  151. let testPassword = ""
  152. let firstTimeLogin = app.webViews.containing(.textField, identifier: "Email or phone")
  153. .element.exists
  154. if firstTimeLogin {
  155. app.webViews.textFields.firstMatch.tap()
  156. app.webViews.textFields.firstMatch.typeText(testEmail)
  157. app.buttons["Done"].tap() // Dismiss keyboard
  158. app.buttons["Next"].tap() // Transition to Google sign in password page
  159. app.webViews.secureTextFields.firstMatch.tap()
  160. app.webViews.secureTextFields.firstMatch.typeText(testPassword)
  161. app.buttons["Done"].tap() // Dismiss keyboard
  162. app.buttons["Next"].tap() // Complete sign in
  163. } else {
  164. app.webViews.staticTexts[testEmail].tap()
  165. }
  166. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  167. XCTAssertTrue(app.navigationBars["User"].exists)
  168. XCTAssertTrue(app.staticTexts[testEmail].exists)
  169. // Cleanup
  170. removeUIInterruptionMonitor(interruptionMonitor)
  171. }
  172. // MARK: - Private Helpers
  173. private func signOut() {
  174. if app.tabBars.firstMatch.buttons.element(boundBy: 1).exists {
  175. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  176. }
  177. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  178. if app.staticTexts["Sign Out"].exists {
  179. app.staticTexts["Sign Out"].tap()
  180. }
  181. if app.tabBars.firstMatch.buttons.element(boundBy: 0).exists {
  182. app.tabBars.firstMatch.buttons.element(boundBy: 0).tap()
  183. }
  184. }
  185. }
  186. extension XCTestCase {
  187. func wait(forElement element: XCUIElement, timeout: TimeInterval) {
  188. let predicate = NSPredicate(format: "exists == 1")
  189. expectation(for: predicate, evaluatedWith: element)
  190. waitForExpectations(timeout: timeout)
  191. }
  192. }