AuthenticationExampleUITests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. func testEmailLinkSentSuccessfully() {
  173. app.staticTexts["Email Link/Passwordless"].tap()
  174. let testEmail = "test@test.com"
  175. app.textFields["Enter Authentication Email"].tap()
  176. app.textFields["Enter Authentication Email"].typeText(testEmail)
  177. app.buttons["return"].tap() // Dismiss keyboard
  178. app.buttons["Send Sign In Link"].tap()
  179. // Wait for the error message to appear (if there is an error)
  180. let errorAlert = app.alerts.staticTexts["Error"]
  181. let errorExists = errorAlert.waitForExistence(timeout: 5.0)
  182. app.swipeDown(velocity: .fast)
  183. // Assert that there is no error message (success case)
  184. // The email sign in link is sent successfully if no error message appears
  185. XCTAssertFalse(errorExists, "Error")
  186. // Go back and check that there is no user that is signed in
  187. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  188. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  189. XCTAssertEqual(
  190. app.cells.count,
  191. 0,
  192. "The user shouldn't be signed in and the user view should have no cells."
  193. )
  194. }
  195. func testResetPasswordLinkCustomDomain() {
  196. // assuming action type is in-app + continue URL everytime the app launches
  197. // set Authorized Domain as Continue URL
  198. let testContinueURL = "fir-ios-auth-sample.firebaseapp.com"
  199. app.staticTexts["Continue URL"].tap()
  200. app.alerts.textFields.element.typeText(testContinueURL)
  201. app.buttons["Save"].tap()
  202. // set Custom Hosting Domain as Link Domain
  203. let testLinkDomain = "http://firebaseiosauthsample.testdomaindonotuse.com"
  204. app.staticTexts["Link Domain"].tap()
  205. app.alerts.textFields.element.typeText(testLinkDomain)
  206. app.buttons["Save"].tap()
  207. app.staticTexts["Request Password Reset"].tap()
  208. let testEmail = "test@test.com"
  209. app.alerts.textFields.element.typeText(testEmail)
  210. app.buttons["Save"].tap()
  211. // Go back and check that there is no user that is signed in
  212. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  213. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  214. XCTAssertEqual(
  215. app.cells.count,
  216. 0,
  217. "The user shouldn't be signed in and the user view should have no cells."
  218. )
  219. }
  220. func testResetPasswordLinkDefaultDomain() {
  221. // assuming action type is in-app + continue URL everytime the app launches
  222. // set Authorized Domain as Continue URL
  223. let testContinueURL = "fir-ios-auth-sample.firebaseapp.com"
  224. app.staticTexts["Continue URL"].tap()
  225. app.alerts.textFields.element.typeText(testContinueURL)
  226. app.buttons["Save"].tap()
  227. app.staticTexts["Request Password Reset"].tap()
  228. let testEmail = "test@test.com"
  229. app.alerts.textFields.element.typeText(testEmail)
  230. app.buttons["Save"].tap()
  231. // Go back and check that there is no user that is signed in
  232. app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
  233. wait(forElement: app.navigationBars["User"], timeout: 5.0)
  234. XCTAssertEqual(
  235. app.cells.count,
  236. 0,
  237. "The user shouldn't be signed in and the user view should have no cells."
  238. )
  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. }