UI 测试 - 等待对话框,然后点击确定按钮并检查控制器

UI Testing - wait for dialog then tap Ok button and check controller

我正在尝试为我的控制器创建 UI 测试。我填写文本字段并点击按钮,现在我想等待对话框出现,当它出现时我想点击确定按钮并检查显示的控制器。

我的测试方法到此结束:

let alert = app.alerts["Error"]
let exists = NSPredicate(format: "exists == 1")
self.expectationForPredicate(exists, evaluatedWithObject: alert, handler: nil)
self.waitForExpectationsWithTimeout(5.0) { (error) in

  XCTAssertNil(error, "Something went horribly wrong")
  alert.buttons["Ok"].tap()
  XCTAssertEqual(app.navigationBars.element.identifier, "RegistrationViewController")
}

问题是测试被评估为失败,但是当我查看 phone 时,出现对​​话框并点击“确定”按钮,控制器也正常。

我在调试中遇到了这个错误 window:

UI Testing Failure - No matches found for Alert

我猜那个 Tap 有什么问题。我该如何解决?我做错了什么?谢谢

waitforExpectationsWithTimeout() 仅在超过超时时调用完成处理程序。只需将您的控制器断言移至异步等待调用下方。

let alert = app.alerts["Error"]
let exists = NSPredicate(format: "exists == 1")

expectation(for: exists, evaluatedWith: alert, handler: nil)
waitForExpectations(timeout: timeout, handler: nil)

alert.buttons["Ok"].tap()
XCTAssertEqual(app.navigationBars.element.identifier, "RegistrationViewController")