我正在使用 XCUITest,在一个简单的屏幕上,只有用户名文本字段、密码文本字段和一个登录按钮。 我可以像点击登录按钮一样断言,用户名 textField 和密码 textField 都是非空的。 但是该怎么做,我想在用户名和密码文本字段中输入文本。 这仅在第一个字段上是可能的,因为我在使用以下代码时遇到错误。
func testUserNameShouldNotBeEmptyOnLoginButtonTapped() {
app.launch()
let loginButton = app.buttons["Login"]
let userNameField = app.textFields["UsernameTextField"]
userNameField.tap()
userNameField.typeText("Dummy User")
sleep(2)
let passwordField = app.textFields["PasswordTextField"]
passwordField.tap()
passwordField.typeText("Yohooooooso")
loginButton.tap()
XCTAssertTrue(userNameField.exists)
guard let userNameValue = userNameField.value as? String,
let passwordValue = passwordField.value as? String else {
XCTAssert(true, "UsernameTextFieldUsernameTextFieldUsernameTextField")
return
}
if passwordValue.isEmpty {
XCTAssert(false, "Password Cannot be Empty")
} else if userNameValue.isEmpty {
XCTAssert(false, "UserName Cannot be empty")
} else {
XCTAssert(true, "Both Fields are valid")
}
}
我如何才能将键盘的焦点更改为选择字段,或者在第一个字段上使用 typeText
隐藏键盘。
我在 passwordTextField 上收到如下异常
Neither element nor any descendant has keyboard focus. Event dispatch snapshot: TextField, identifier: 'PasswordTextField'
最佳答案
我的第一个想法是,当您尝试点击它以获得焦点时,您的密码文本字段被键盘隐藏了。 您应该关闭键盘或稍微滚动一下以使文本字段可见。
您还可以使用 isHittable
属性来确保可以命中元素。
关于swift - 多个文本字段的 XCUITest 键盘未隐藏或键盘未聚焦于第二个文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183955/