如何在Xcode中的UITests下等待直到某些视图可以点击?

Bar*_*zyk 5 ios swift xcode-ui-testing

有时在Xcode中的UITests下,编译器会在加载和显示之前尝试点击按钮.然后就出现问题了no matched found for....

但对此的简单解决方案是:

sleep(1) //wait 1 second and give `mybutton` time to load and be accessible for uitests
mybutton.tap()
Run Code Online (Sandbox Code Playgroud)

但这太可怕了,因为我不能把它0.1作为参数.在很多按钮之前等待1秒让我很烦人.

有没有办法等到uitests可见?

Tom*_*min 8

您应该创建一个XCTestExpectation并等待它完成

expectationForPredicate(NSPredicate(format: "hittable == true"), evaluatedWithObject: mybutton, handler: nil)
waitForExpectationsWithTimeout(10.0, handler: nil)

mybutton.tap()
Run Code Online (Sandbox Code Playgroud)

  • 对于Swift 3,您可以保留`handler`参数. (2认同)