Tadeh Alexani
Tadeh Alexani

Reputation: 55

How to access a variable o watch a change in SwiftUI UI testing?

I'm new at UI testing with SwiftUI and I just wondering:
How can I access a variable or watch for sheet appearance in the process?
E.g. After login, I want to check if it was successful or not and then assert that boolean (which indicates which login was successful or not) or after-login sheet appearance exists.

Code Example:

import XCTest
import SwiftUI
@testable import Formaloo

class when_the_user_types_username_and_password_and_press_login_button: XCTestCase {

  private var app: XCUIApplication!
  
  override func setUp() {
    super.setUp()
    
    self.app = XCUIApplication()
    self.app.launch()
  }
  
  func test_user_should_be_logged_in_with_correct_username_and_password() {
    
    let usernameTextField = app.textFields["usernameTextField"]
    let passwordTextField = app.secureTextFields["passwordTextField"]
    let loginButton = app.buttons["loginButton"]
    
    XCTAssert(usernameTextField.exists)
    XCTAssert(passwordTextField.exists)
    
    usernameTextField.tap()
    usernameTextField.typeText("*****")
    
    passwordTextField.tap()
    usernameTextField.typeText("*****")
    
    loginButton.tap()
    
    // I don't know how to check the login being successful here!
  }
}

Upvotes: 1

Views: 534

Answers (1)

Mike Collins
Mike Collins

Reputation: 4559

You already know how to find things on the screen so you just need to identify “what, after hitting the login button with correct credentials proves to me I was logged in?”

A success message? An entirely new screen? Is there a control that is unique to that screen?

Assert on one of those things you expect to exist using exists

An added note, there is no reason to assert the existence of your login fields after you’ve pulled the value from them; getting the value proves they’re there so the assertions are redundant.

Upvotes: 1

Related Questions