Testing the text of an animated tag that appears and disappears

I am trying to check the appearance of a shortcut (toastLabel) that I have, which briefly revives when someone enters the wrong letter.

private func registerNewUser(email: String, password: String, confirmationPassword: String) {
    if password == confirmationPassword {
        firebaseData.createUser(email: email, password: password, completion: { (error, _ ) in
            if let error = error {
                self.showToast(in: self.view, with: error.localizedDescription)
            } else {
                self.showToast(in: self.view, with: "Registered succesfully")
                self.signInUser(email: email, password: password)
            }
        })
    } else {
        //raise password mismatch error
        print("password mismatch error")
    }
}

func showToast(in toastSuperView: UIView, with text: String) {
    let toastLabel = ToastLabel()
    toastLabel.text = text
    toastSuperView.addSubview(toastLabel)
    layoutToastLabel(toastLabel)
    animateToastLabel(toastLabel)
}

private func layoutToastLabel(_ toastLabel: ToastLabel) {
    toastLabel.centerYToSuperview()
    toastLabel.pinToSuperview(edges: [.left, .right])
}

private func animateToastLabel(_ toastLabel: ToastLabel) {
    UIView.animate(withDuration: 2.5, delay: 0, options: .curveEaseOut, animations: {
        toastLabel.alpha = 0.0
    }, completion: { _ in
        toastLabel.removeFromSuperview()
    })
}

I just want to check that the error text received back from firebase appears after the user enters an email that has already been accepted.

func testRegisteringWithUsedEmailDisplaysFirebaseError() {
    let email = registeredEmail
    let password = "password"

    welcomeScreenHelper.register(email: email,
                                 password: password,
                                 confirmationPassword: password,
                                 completion: {

        let firebaseErrorMessage = "The email address is already in use by another account."
        XCTAssert(self.app.staticTexts[firebaseErrorMessage].exists)
    })
}

func register(email: String, password: String, confirmationPassword: String, completion: (() -> Void)? = nil) {
    let emailTextField = app.textFields[AccesID.emailTextField]
    let passwordTextField = app.secureTextFields[AccesID.passwordTextField]
    let confirmPasswordTextField = app.secureTextFields[AccesID.confirmPasswordTextField]
    let registerButton = app.buttons[AccesID.registerButton]

    emailTextField.tap()
    emailTextField.typeText(email)
    passwordTextField.tap()
    passwordTextField.typeText(password)
    registerButton.tap()
    confirmPasswordTextField.tap()
    confirmPasswordTextField.typeText(confirmationPassword)
    registerButton.tap()

    completion?()
}

when I use other tools like wait and XCTWaiter, tests still fail, even though the text and label definitely appear. I have never had to do such a test, so I don’t know where I can be wrong, do I need to do something to check the animated view or something like that.

Update1:

, , , registerButton, , , , . , , .

Update2:

:

func testRegisteringWithUsedEmailDisplaysFirebaseError() {

    welcomeScreenHelper.register(email: registeredEmail,
                                 password: password,
                                 confirmationPassword: password,
                                 completion: {

        let firebaseErrorMessage = "The email address is already in use by another account."

        let text = self.app.staticTexts[firebaseErrorMessage]
        let exists = NSPredicate(format: "exists == true")

        self.expectation(for: exists, evaluatedWith: text, handler: nil)
        self.waitForExpectations(timeout: 10, handler: nil)
        XCTAssert(self.app.staticTexts[firebaseErrorMessage].exists)
    })
}

:

override func setUp() {
    app.launch()
    UIView.setAnimationsEnabled(false)
    super.setUp()
}

override func tearDown() {
    if let email = createdUserEmail {
        firebaseHelper.removeUser(with: email)
    }
    UIView.setAnimationsEnabled(true)
    super.tearDown()
}

. , func register, , , , toastLabel.

+6
2

, :

  • , , DispatchQueue.async, XCTestCase.expectation
  • UIView.animate ( , ), , , UIView.setAnimationsEnabled(false) , , XCTestCase.setUp XCTestCase.tearDown.
  • , , , , ( , firebaseData), mocks/stubs, , XCTestCase.expectation, API/ OK .

XCTestCase.expectation + UIView.setAnimationsEnabled(false) . XCTestCase.expectation .

1: :

func test() {
    let exp = expectation(description: "completion called")
    someAsyncMethodWithCompletion() {
        exp.fulfill()
    }
    waitForExpectations(timeout: 1) { _ in }
    // assert here
}

, :

func testRegisteringWithUsedEmailDisplaysFirebaseError() {
    let exp = expectation(description: "completion called")
    welcomeScreenHelper.register(email: registeredEmail,
                                 password: password,
                                 confirmationPassword: password,
                                 completion: { exp.fulfill() })
    self.waitForExpectations(timeout: 10, handler: nil)
    let firebaseErrorMessage = "The email address is already in use by another account."
    XCTAssert(self.app.staticTexts[firebaseErrorMessage].exists)
}
+3

, , UIAlertController :

func toast(message: String!) -> Void {

        let controller: UIAlertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        self.present(controller, animated: true, completion: nil)
        let when = DispatchTime.now() + 2.5 // change 2.5 to desired number of seconds
        DispatchQueue.main.asyncAfter(deadline: when) {
            // Your code with delay
            self.dismiss(animated: true, completion: nil)
        }

    }

iOS

-1

All Articles