AuthenticationExampleUITests.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 testPhoneAuthLoginRCEInEnforceMode() {
  143. app.staticTexts["Phone Number"].tap()
  144. XCTAssertTrue(app.staticTexts["Sign in using Phone Auth"].waitForExistence(timeout: 3))
  145. let testPhone = "12345678901"
  146. app.textFields["Enter Phone Number"].tap()
  147. app.textFields["Enter Phone Number"].typeText(testPhone)
  148. app.buttons["Send Verification Code"].tap()
  149. let verificationCodeInput = app.textFields["Enter verification code."]
  150. let exists = verificationCodeInput.waitForExistence(timeout: 10)
  151. XCTAssertTrue(exists, "Verification code field does not exist")
  152. let testVerificationCode = "123456"
  153. verificationCodeInput.typeText(testVerificationCode)
  154. app.buttons["Continue"].tap()
  155. // Sign out
  156. let signOutButton = app.buttons["Sign Out"]
  157. if signOutButton.exists {
  158. signOutButton.tap()
  159. }
  160. }
  161. func testPhoneAuthLoginRCEInEnforceModeIncorrectNumber() {
  162. app.staticTexts["Phone Number"].tap()
  163. XCTAssertTrue(app.staticTexts["Sign in using Phone Auth"].waitForExistence(timeout: 3))
  164. let testPhone = "1234567890"
  165. app.textFields["Enter Phone Number"].tap()
  166. app.textFields["Enter Phone Number"].typeText(testPhone)
  167. app.buttons["Send Verification Code"].tap()
  168. // Verify that the error dialog appears
  169. let errorDialog = app.alerts["Error"]
  170. XCTAssertTrue(
  171. errorDialog.waitForExistence(timeout: 5),
  172. "Error dialog should appear."
  173. )
  174. let okButton = errorDialog.buttons["OK"] // Dismiss the error dialog
  175. XCTAssertTrue(okButton.exists, "The 'OK' button should be present in the error dialog.")
  176. okButton.tap()
  177. // Ensure the dialog is dismissed
  178. XCTAssertFalse(errorDialog.exists, "The error dialog should be dismissed after tapping 'OK'.")
  179. // Go back and check that there is no user that is signed in
  180. app.swipeDown(velocity: .fast)
  181. // Go back and check that there is no user that is signed in
  182. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  183. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  184. XCTAssertEqual(
  185. app.cells.count,
  186. 0,
  187. "The user shouldn't be signed in and the user view should have no cells."
  188. )
  189. }
  190. func DRAFT_testGoogleSignInAndLinkAccount() {
  191. let interruptionMonitor = addUIInterruptionMonitor(withDescription: "Sign in with Google") {
  192. alert -> Bool in
  193. alert.buttons["Continue"].tap()
  194. return true
  195. }
  196. app.staticTexts["Google"].tap()
  197. app.tap() // Triggers the UIInterruptionMonitor
  198. let testEmail = ""
  199. let testPassword = ""
  200. let firstTimeLogin = app.webViews.containing(.textField, identifier: "Email or phone")
  201. .element.exists
  202. if firstTimeLogin {
  203. app.webViews.textFields.firstMatch.tap()
  204. app.webViews.textFields.firstMatch.typeText(testEmail)
  205. app.buttons["Done"].tap() // Dismiss keyboard
  206. app.buttons["Next"].tap() // Transition to Google sign in password page
  207. app.webViews.secureTextFields.firstMatch.tap()
  208. app.webViews.secureTextFields.firstMatch.typeText(testPassword)
  209. app.buttons["Done"].tap() // Dismiss keyboard
  210. app.buttons["Next"].tap() // Complete sign in
  211. } else {
  212. app.webViews.staticTexts[testEmail].tap()
  213. }
  214. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  215. XCTAssertTrue(app.navigationBars["User"].exists)
  216. XCTAssertTrue(app.staticTexts[testEmail].exists)
  217. // Cleanup
  218. removeUIInterruptionMonitor(interruptionMonitor)
  219. }
  220. // MARK: - Private Helpers
  221. private func signOut() {
  222. if app.tabBars.firstMatch.buttons.element(boundBy: 1).exists {
  223. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  224. }
  225. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  226. if app.staticTexts["Sign Out"].exists {
  227. app.staticTexts["Sign Out"].tap()
  228. }
  229. if app.tabBars.firstMatch.buttons.element(boundBy: 0).exists {
  230. app.tabBars.firstMatch.buttons.element(boundBy: 0).tap()
  231. }
  232. }
  233. }
  234. extension XCTestCase {
  235. func wait(forElement element: XCUIElement, timeout: TimeInterval) {
  236. let predicate = NSPredicate(format: "exists == 1")
  237. expectation(for: predicate, evaluatedWith: element)
  238. waitForExpectations(timeout: timeout)
  239. }
  240. }