UIAutomation和XCTestCase:如何等待按钮激活

Bri*_*ian 4 ios ios-ui-automation xctest xcode7 xcode-ui-testing

我正在写一个UIAutomation测试用例,我需要等待用户激活才能继续.似乎没有一种很好的方法可以检查按钮是否可以更改为启用状态.

什么是最好的是在检查它的状态之前等待UI中发生的事情?

dispatch_after和NSTimer似乎都不起作用.他们只是阻止失败.

Eti*_*nne 7

如果您使用NSPredicates和期望,这实际上非常简单.您甚至可以设置超时值.此示例显示如何使用5秒超时执行此操作.

let exists = NSPredicate(format:"enabled == true")
expectationForPredicate(exists, evaluatedWithObject: app.tables.textFields["MY_FIELD_NAME"], handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
Run Code Online (Sandbox Code Playgroud)


Mur*_*ock -4

您应该能够实现一个 while 循环来检查您想要的条件(例如按钮启用)。这将停止测试用例的进展,直到满足 while 条件并且测试将继续。建立延迟以减慢轮询并确保超时,这样就不会无限期地陷入困境。

伪代码

While (/*button is disabled*/) {
    if (/*timeout condition met*/) {
        /*handle error*/
        break;
    }

    UIATarget.delay(<duration in seconds>);
}
Run Code Online (Sandbox Code Playgroud)