| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- // Copyright 2020 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- import XCTest
- class AuthenticationExampleUITests: XCTestCase {
- var app: XCUIApplication!
- override func setUp() {
- super.setUp()
- continueAfterFailure = false
- app = XCUIApplication()
- app.launch()
- }
- override func tearDown() {
- super.tearDown()
- signOut()
- }
- func testAuth() {
- // Verify that Auth Example app launched successfully
- XCTAssertTrue(app.navigationBars["Firebase Auth"].exists)
- }
- func testAuthOptions() {
- // There are 15 sign in methods, each with its own cell
- XCTAssertEqual(app.tables.cells.count, 15)
- }
- func testAuthAnonymously() {
- app.staticTexts["Anonymous Authentication"].tap()
- wait(forElement: app.navigationBars["User"], timeout: 5.0)
- XCTAssertTrue(app.navigationBars["User"].exists)
- let isAnonymousCell = app.cells.containing(.staticText, identifier: "Is User Anonymous?")
- .element
- XCTAssertTrue(isAnonymousCell.staticTexts["Yes"].exists, "The user should be anonymous")
- }
- func testAuthExistingAccount() {
- // Setup existing user for duplicate test below.
- let existingEmail = "existing@test.com"
- let existingPassword = "existingPW"
- app.staticTexts["Email & Password Login"].tap()
- let testEmail = existingEmail
- app.textFields["Email"].tap()
- app.textFields["Email"].typeText(testEmail)
- let testPassword = existingPassword
- app.textFields["Password"].tap()
- app.textFields["Password"].typeText(testPassword)
- app.buttons["Login"].tap()
- wait(forElement: app.navigationBars["User"], timeout: 5.0)
- XCTAssertTrue(app.navigationBars["User"].exists)
- XCTAssertTrue(
- app.staticTexts[testEmail].exists,
- "The user should be signed in and the email field should display their email."
- )
- }
- func testAuthExistingAccountWrongPassword() {
- app.staticTexts["Email & Password Login"].tap()
- let testEmail = "test@test.com"
- app.textFields["Email"].tap()
- app.textFields["Email"].typeText(testEmail)
- app.textFields["Password"].tap()
- app.textFields["Password"].typeText("wrong password")
- app.buttons["Login"].tap()
- wait(forElement: app.alerts.staticTexts["Error"], timeout: 5.0)
- XCTAssertTrue(app.alerts.staticTexts["Error"].exists)
- // Dismiss alert that password was incorrect
- app.alerts.buttons["OK"].tap()
- // Go back and check that there is no user that is signed in
- app.navigationBars.buttons.firstMatch.tap()
- app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
- wait(forElement: app.navigationBars["User"], timeout: 5.0)
- XCTAssertEqual(
- app.cells.count,
- 0,
- "The user shouldn't be signed in and the user view should have no cells."
- )
- }
- func testCreateAccountBadPassword() {
- app.staticTexts["Email & Password Login"].tap()
- let testEmail = "test@test.com"
- app.textFields["Email"].tap()
- app.textFields["Email"].typeText(testEmail)
- app.textFields["Password"].tap()
- // Enter an invalid password that is "too short"
- app.textFields["Password"].typeText("2shrt")
- app.buttons["Create Account"].tap()
- wait(forElement: app.alerts.staticTexts["Error"], timeout: 5.0)
- XCTAssertTrue(app.alerts.staticTexts["Error"].exists)
- // Dismiss alert that password was incorrect
- app.alerts.buttons["OK"].tap()
- // Go back and check that there is no user that is signed in
- app.navigationBars.buttons.firstMatch.tap()
- app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
- wait(forElement: app.navigationBars["User"], timeout: 5.0)
- XCTAssertEqual(
- app.cells.count,
- 0,
- "The user shouldn't be signed in and the user view should have no cells."
- )
- }
- func testCreateAlreadyExistingAccount() {
- app.staticTexts["Email & Password Login"].tap()
- let testEmail = "test@test.com"
- app.textFields["Email"].tap()
- app.textFields["Email"].typeText(testEmail)
- let testPassword = "test12"
- app.textFields["Password"].tap()
- app.textFields["Password"].typeText(testPassword)
- app.buttons["Create Account"].tap()
- wait(forElement: app.alerts.staticTexts["Error"], timeout: 5.0)
- XCTAssertTrue(app.alerts.staticTexts["Error"].exists)
- // Dismiss alert that password was incorrect
- app.alerts.buttons["OK"].tap()
- // Go back and check that there is no user that is signed in
- app.navigationBars.buttons.firstMatch.tap()
- app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
- wait(forElement: app.navigationBars["User"], timeout: 5.0)
- XCTAssertEqual(
- app.cells.count,
- 0,
- "The user shouldn't be signed in and the user view should have no cells."
- )
- }
- func testCreateAccountCorrectPassword() {
- app.staticTexts["Email & Password Login"].tap()
- let newEmail = "\(Date().timeIntervalSince1970)_test@test.com"
- app.textFields["Email"].tap()
- app.typeText(newEmail)
- let newPassword = "new password"
- app.textFields["Password"].tap()
- app.typeText(newPassword)
- app.buttons["Create Account"].tap()
- wait(forElement: app.navigationBars["User"], timeout: 5.0)
- XCTAssertTrue(app.navigationBars["User"].exists)
- XCTAssertTrue(
- app.staticTexts[newEmail].exists,
- "The user should be signed into the new account."
- )
- }
- func DRAFT_testGoogleSignInAndLinkAccount() {
- let interruptionMonitor = addUIInterruptionMonitor(withDescription: "Sign in with Google") {
- alert -> Bool in
- alert.buttons["Continue"].tap()
- return true
- }
- app.staticTexts["Google"].tap()
- app.tap() // Triggers the UIInterruptionMonitor
- let testEmail = ""
- let testPassword = ""
- let firstTimeLogin = app.webViews.containing(.textField, identifier: "Email or phone")
- .element.exists
- if firstTimeLogin {
- app.webViews.textFields.firstMatch.tap()
- app.webViews.textFields.firstMatch.typeText(testEmail)
- app.buttons["Done"].tap() // Dismiss keyboard
- app.buttons["Next"].tap() // Transition to Google sign in password page
- app.webViews.secureTextFields.firstMatch.tap()
- app.webViews.secureTextFields.firstMatch.typeText(testPassword)
- app.buttons["Done"].tap() // Dismiss keyboard
- app.buttons["Next"].tap() // Complete sign in
- } else {
- app.webViews.staticTexts[testEmail].tap()
- }
- wait(forElement: app.navigationBars["User"], timeout: 5.0)
- XCTAssertTrue(app.navigationBars["User"].exists)
- XCTAssertTrue(app.staticTexts[testEmail].exists)
- // Cleanup
- removeUIInterruptionMonitor(interruptionMonitor)
- }
- // MARK: - Private Helpers
- private func signOut() {
- app.tabBars.firstMatch.buttons.element(boundBy: 1).tap()
- wait(forElement: app.navigationBars["User"], timeout: 5.0)
- if app.staticTexts["Sign Out"].exists {
- app.staticTexts["Sign Out"].tap()
- }
- app.tabBars.firstMatch.buttons.element(boundBy: 0).tap()
- }
- }
- extension XCTestCase {
- func wait(forElement element: XCUIElement, timeout: TimeInterval) {
- let predicate = NSPredicate(format: "exists == 1")
- expectation(for: predicate, evaluatedWith: element)
- waitForExpectations(timeout: timeout)
- }
- }
|