我正在编写一个测试,其中需要测试用户单击注册按钮时,成功注册后将其带到登录页面。
func test_should_navigate_to_login_screen_for_successful_registration() {
let usernameTextField = self.app.textFields["usernameTextField"]
usernameTextField.tap()
usernameTextField.typeText("johndoe")
let passwordTextField = self.app.textFields["passwordTextField"]
passwordTextField.tap()
passwordTextField.typeText("password123")
// register button will navigate to login view when successfull
self.app.buttons["registerButton"].tap()
print(self.app.navigationBars.element.identifier)
// If I pause the test and then check the result of self.app.navigationBars.element.identifier
// then it contains the correct value but if I don't then it contains "Create an Account" which is the text of the back button
XCTAssertEqual(self.app.navigationBars.element.identifier, "BankAccount.LoginView")
}
问题是最后一个断言,该断言检查self.app.navigationBars.element.identifier值。该值不立即可用,这就是测试失败的原因。我对其进行了调试,并确认最终可以使用正确的值。
在Xcode中测试导航的正确方法是什么?
解决方案(在评论中使用Matt的建议):
这有效。确保为您的视图提供accessibilityIdentifier。
func test_should_navigate_to_login_screen_for_successful_registration(){
let usernameTextField = self.app.textFields["usernameTextField"]
usernameTextField.tap()
usernameTextField.typeText("johndoe")
let passwordTextField = self.app.textFields["passwordTextField"]
passwordTextField.tap()
passwordTextField.typeText("password123")
// register button will navigate to login view when successfull
self.app.buttons["registerButton"].tap()
let loginView = app.otherElements["LoginView"]
let loginViewExists = loginView.waitForExistence(timeout: 1.0)
XCTAssert(loginViewExists)