AuthenticationExampleUITests.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 testPhoneMultiFactorEnrollUnenroll() {
  143. // login with email password
  144. app.staticTexts["Email & Password Login"].tap()
  145. let testEmail = "sample.auth.ios@gmail.com"
  146. app.textFields["Email"].tap()
  147. app.typeText(testEmail)
  148. let testPassword = "sampleauthios"
  149. app.textFields["Password"].tap()
  150. app.typeText(testPassword)
  151. app.buttons["Login"].tap()
  152. // enroll multifactor with phone
  153. let authenticationButton = app.tabBars.buttons["Authentication"]
  154. let exists = authenticationButton.waitForExistence(timeout: 5)
  155. XCTAssertTrue(exists, "Authentication button did not appear in time.")
  156. authenticationButton.tap()
  157. app.tables.cells.staticTexts["Phone Enroll"].tap()
  158. let testSecondFactorPhone = "+11234567890"
  159. app.typeText(testSecondFactorPhone)
  160. app.buttons["Save"].tap()
  161. let testVerificationCode = "123456"
  162. app.typeText(testVerificationCode)
  163. app.buttons["Save"].tap()
  164. let testPhoneSecondFactorDisplayName = "phone1"
  165. app.typeText(testPhoneSecondFactorDisplayName)
  166. app.buttons["Save"].tap()
  167. // unenroll multifactor
  168. app.swipeUp(velocity: .fast)
  169. app.tables.cells.staticTexts["Multifactor unenroll"].tap()
  170. XCTAssertTrue(app.buttons["phone1"].exists) // enrollment successful
  171. app.buttons["phone1"].tap()
  172. app.swipeUp(velocity: .fast)
  173. app.tables.cells.staticTexts["Multifactor unenroll"].tap()
  174. XCTAssertFalse(app.buttons["phone1"].exists) // unenrollment successful
  175. app.buttons["Cancel"].tap()
  176. // sign out after unenroll
  177. app.tabBars.buttons["Current User"].tap()
  178. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  179. }
  180. func testPhoneSecondFactorSignIn() {
  181. // login with email password
  182. app.staticTexts["Email & Password Login"].tap()
  183. let testEmail = "sample.ios.auth@gmail.com"
  184. app.textFields["Email"].tap()
  185. app.typeText(testEmail)
  186. let testPassword = "sampleios123"
  187. app.textFields["Password"].tap()
  188. app.typeText(testPassword)
  189. app.buttons["Login"].tap()
  190. // login with second factor
  191. XCTAssertTrue(app.staticTexts["Choose a second factor to continue."]
  192. .waitForExistence(timeout: 5))
  193. let secondFactor = app.staticTexts["phone2"] // select 'phone2' as second factor
  194. XCTAssertTrue(secondFactor.exists, "'phone2' option should be visible.")
  195. secondFactor.tap()
  196. app.buttons["Send Verification Code"].tap()
  197. let verificationCodeInput = app.textFields["Enter verification code."]
  198. verificationCodeInput.tap()
  199. let testVerificationCode = "123456"
  200. verificationCodeInput.typeText(testVerificationCode)
  201. let signInButton = app.buttons["Sign in"]
  202. XCTAssertTrue(signInButton.waitForExistence(timeout: 2), "'Sign in' button should be visible.")
  203. signInButton.tap()
  204. // sign out
  205. let signOutButton = app.buttons["Sign Out"]
  206. if signOutButton.exists {
  207. signOutButton.tap()
  208. }
  209. }
  210. func DRAFT_testGoogleSignInAndLinkAccount() {
  211. let interruptionMonitor = addUIInterruptionMonitor(withDescription: "Sign in with Google") {
  212. alert -> Bool in
  213. alert.buttons["Continue"].tap()
  214. return true
  215. }
  216. app.staticTexts["Google"].tap()
  217. app.tap() // Triggers the UIInterruptionMonitor
  218. let testEmail = ""
  219. let testPassword = ""
  220. let firstTimeLogin = app.webViews.containing(.textField, identifier: "Email or phone")
  221. .element.exists
  222. if firstTimeLogin {
  223. app.webViews.textFields.firstMatch.tap()
  224. app.webViews.textFields.firstMatch.typeText(testEmail)
  225. app.buttons["Done"].tap() // Dismiss keyboard
  226. app.buttons["Next"].tap() // Transition to Google sign in password page
  227. app.webViews.secureTextFields.firstMatch.tap()
  228. app.webViews.secureTextFields.firstMatch.typeText(testPassword)
  229. app.buttons["Done"].tap() // Dismiss keyboard
  230. app.buttons["Next"].tap() // Complete sign in
  231. } else {
  232. app.webViews.staticTexts[testEmail].tap()
  233. }
  234. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  235. XCTAssertTrue(app.navigationBars["User"].exists)
  236. XCTAssertTrue(app.staticTexts[testEmail].exists)
  237. // Cleanup
  238. removeUIInterruptionMonitor(interruptionMonitor)
  239. }
  240. // MARK: - Private Helpers
  241. private func signOut() {
  242. if app.tabBars.firstMatch.buttons.element(boundBy: 1).exists {
  243. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  244. }
  245. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  246. if app.staticTexts["Sign Out"].exists {
  247. app.staticTexts["Sign Out"].tap()
  248. }
  249. if app.tabBars.firstMatch.buttons.element(boundBy: 0).exists {
  250. app.tabBars.firstMatch.buttons.element(boundBy: 0).tap()
  251. }
  252. }
  253. }
  254. extension XCTestCase {
  255. func wait(forElement element: XCUIElement, timeout: TimeInterval) {
  256. let predicate = NSPredicate(format: "exists == 1")
  257. expectation(for: predicate, evaluatedWith: element)
  258. waitForExpectations(timeout: timeout)
  259. }
  260. }