AuthenticationExampleUITests.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 testAuthOptions() {
  32. // There are 15 sign in methods, each with its own cell
  33. XCTAssertEqual(app.tables.cells.count, 15)
  34. }
  35. func testAuthAnonymously() {
  36. app.staticTexts["Anonymous Authentication"].tap()
  37. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  38. XCTAssertTrue(app.navigationBars["User"].exists)
  39. let isAnonymousCell = app.cells.containing(.staticText, identifier: "Is User Anonymous?")
  40. .element
  41. XCTAssertTrue(isAnonymousCell.staticTexts["Yes"].exists, "The user should be anonymous")
  42. }
  43. func testAuthExistingAccount() {
  44. // Setup existing user for duplicate test below.
  45. let existingEmail = "existing@test.com"
  46. let existingPassword = "existingPW"
  47. app.staticTexts["Email & Password Login"].tap()
  48. let testEmail = existingEmail
  49. app.textFields["Email"].tap()
  50. app.textFields["Email"].typeText(testEmail)
  51. let testPassword = existingPassword
  52. app.textFields["Password"].tap()
  53. app.textFields["Password"].typeText(testPassword)
  54. app.buttons["Login"].tap()
  55. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  56. XCTAssertTrue(app.navigationBars["User"].exists)
  57. XCTAssertTrue(
  58. app.staticTexts[testEmail].exists,
  59. "The user should be signed in and the email field should display their email."
  60. )
  61. }
  62. func testAuthExistingAccountWrongPassword() {
  63. app.staticTexts["Email & Password Login"].tap()
  64. let testEmail = "test@test.com"
  65. app.textFields["Email"].tap()
  66. app.textFields["Email"].typeText(testEmail)
  67. app.textFields["Password"].tap()
  68. app.textFields["Password"].typeText("wrong password")
  69. app.buttons["Login"].tap()
  70. wait(forElement: app.alerts.staticTexts["Error"], timeout: 5.0)
  71. XCTAssertTrue(app.alerts.staticTexts["Error"].exists)
  72. // Dismiss alert that password was incorrect
  73. app.alerts.buttons["OK"].tap()
  74. // Go back and check that there is no user that is signed in
  75. app.navigationBars.buttons.firstMatch.tap()
  76. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  77. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  78. XCTAssertEqual(
  79. app.cells.count,
  80. 0,
  81. "The user shouldn't be signed in and the user view should have no cells."
  82. )
  83. }
  84. func testCreateAccountBadPassword() {
  85. app.staticTexts["Email & Password Login"].tap()
  86. let testEmail = "test@test.com"
  87. app.textFields["Email"].tap()
  88. app.textFields["Email"].typeText(testEmail)
  89. app.textFields["Password"].tap()
  90. // Enter an invalid password that is "too short"
  91. app.textFields["Password"].typeText("2shrt")
  92. app.buttons["Create Account"].tap()
  93. wait(forElement: app.alerts.staticTexts["Error"], timeout: 5.0)
  94. XCTAssertTrue(app.alerts.staticTexts["Error"].exists)
  95. // Dismiss alert that password was incorrect
  96. app.alerts.buttons["OK"].tap()
  97. // Go back and check that there is no user that is signed in
  98. app.navigationBars.buttons.firstMatch.tap()
  99. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  100. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  101. XCTAssertEqual(
  102. app.cells.count,
  103. 0,
  104. "The user shouldn't be signed in and the user view should have no cells."
  105. )
  106. }
  107. func testCreateAlreadyExistingAccount() {
  108. app.staticTexts["Email & Password Login"].tap()
  109. let testEmail = "test@test.com"
  110. app.textFields["Email"].tap()
  111. app.textFields["Email"].typeText(testEmail)
  112. let testPassword = "test12"
  113. app.textFields["Password"].tap()
  114. app.textFields["Password"].typeText(testPassword)
  115. app.buttons["Create Account"].tap()
  116. wait(forElement: app.alerts.staticTexts["Error"], timeout: 5.0)
  117. XCTAssertTrue(app.alerts.staticTexts["Error"].exists)
  118. // Dismiss alert that password was incorrect
  119. app.alerts.buttons["OK"].tap()
  120. // Go back and check that there is no user that is signed in
  121. app.navigationBars.buttons.firstMatch.tap()
  122. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  123. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  124. XCTAssertEqual(
  125. app.cells.count,
  126. 0,
  127. "The user shouldn't be signed in and the user view should have no cells."
  128. )
  129. }
  130. func testCreateAccountCorrectPassword() {
  131. app.staticTexts["Email & Password Login"].tap()
  132. let newEmail = "\(Date().timeIntervalSince1970)_test@test.com"
  133. app.textFields["Email"].tap()
  134. app.typeText(newEmail)
  135. let newPassword = "new password"
  136. app.textFields["Password"].tap()
  137. app.typeText(newPassword)
  138. app.buttons["Create Account"].tap()
  139. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  140. XCTAssertTrue(app.navigationBars["User"].exists)
  141. XCTAssertTrue(
  142. app.staticTexts[newEmail].exists,
  143. "The user should be signed into the new account."
  144. )
  145. }
  146. func DRAFT_testGoogleSignInAndLinkAccount() {
  147. let interruptionMonitor = addUIInterruptionMonitor(withDescription: "Sign in with Google") {
  148. alert -> Bool in
  149. alert.buttons["Continue"].tap()
  150. return true
  151. }
  152. app.staticTexts["Google"].tap()
  153. app.tap() // Triggers the UIInterruptionMonitor
  154. let testEmail = ""
  155. let testPassword = ""
  156. let firstTimeLogin = app.webViews.containing(.textField, identifier: "Email or phone")
  157. .element.exists
  158. if firstTimeLogin {
  159. app.webViews.textFields.firstMatch.tap()
  160. app.webViews.textFields.firstMatch.typeText(testEmail)
  161. app.buttons["Done"].tap() // Dismiss keyboard
  162. app.buttons["Next"].tap() // Transition to Google sign in password page
  163. app.webViews.secureTextFields.firstMatch.tap()
  164. app.webViews.secureTextFields.firstMatch.typeText(testPassword)
  165. app.buttons["Done"].tap() // Dismiss keyboard
  166. app.buttons["Next"].tap() // Complete sign in
  167. } else {
  168. app.webViews.staticTexts[testEmail].tap()
  169. }
  170. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  171. XCTAssertTrue(app.navigationBars["User"].exists)
  172. XCTAssertTrue(app.staticTexts[testEmail].exists)
  173. // Cleanup
  174. removeUIInterruptionMonitor(interruptionMonitor)
  175. }
  176. // MARK: - Private Helpers
  177. private func signOut() {
  178. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  179. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  180. if app.staticTexts["Sign Out"].exists {
  181. app.staticTexts["Sign Out"].tap()
  182. }
  183. app.tabBars.firstMatch.buttons.element(boundBy: 0).tap()
  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. }