【发布时间】:2019-10-04 15:22:58
【问题描述】:
我正在使用 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'
【问题讨论】:
-
我遇到了同样的问题,我尝试在 com.apple.iphonesimulator.plist 中添加一个测试指令,将 ConnectHardwareKeyboard 设置为 false,但问题仍然存在。