"Neither element nor any descendant has keyboard focus" 当 运行 XCTestCase 在真实的 iPhone

"Neither element nor any descendant has keyboard focus" when running XCTestCase in a real iPhone

我正在尝试 运行 UI 测试用例,其中存在两个输入字段。以下是我的代码

let usernameTextField = app.webViews.otherElements["Identity Server"].textFields["Username"]
let passwordField = app.webViews.otherElements["Identity Server"].secureTextFields["Password"]

_ = usernameTextField.waitForExistence(timeout: 8)

usernameTextField.tap()
usernameTextField.typeText("TEST")  // Breakpoint line

app.typeText("\n")

passwordField.tap()
passwordField.typeText("test")

当我 运行 测试用例正常时,它失败并出现问题标题中给出的错误。但是如果我在注释行中添加一个断点,它将 运行 没有任何错误。

我尝试在断点行之后分别使用以下代码片段。

sleep(8)
_ = passwordField.waitForExistence(timeout: 8)

但这些都不起作用。至于更多信息,这是一个 Auth 流程场景,其中这些输入字段驻留在 Web 视图中。

将您发送的字符串末尾的 \n 发送到用户名文本字段:

usernameTextField.typeText("TEST\n")

我决定自己回答而不是关闭问题。我会告诉我的代码出了什么问题。我犯的主要错误是 continueAfterFailure 设置为 true。因此,错误行中显示的错误不是实际的错误抛出行。

所以解决方案是,

continueAfterFailure = false

usernameTextField.tap()
sleep(2)
usernameTextField.typeText("TEST")

在键入文本之前,键盘出现在 Web 视图中应该有一小段等待时间。