【发布时间】:2015-09-26 19:38:25
【问题描述】:
我正在编写一个 UIAutomation 测试用例,我需要等待用户被激活才能继续。似乎没有一种很好的方法来检查按钮是否更改为启用状态。
在检查状态之前,最好等待 UI 中发生某些事情?
dispatch_after 和 NSTimer 似乎都不起作用。他们只是阻塞然后失败。
【问题讨论】:
标签: ios ios-ui-automation xctest xcode7 xcode-ui-testing
我正在编写一个 UIAutomation 测试用例,我需要等待用户被激活才能继续。似乎没有一种很好的方法来检查按钮是否更改为启用状态。
在检查状态之前,最好等待 UI 中发生某些事情?
dispatch_after 和 NSTimer 似乎都不起作用。他们只是阻塞然后失败。
【问题讨论】:
标签: ios ios-ui-automation xctest xcode7 xcode-ui-testing
Etienne's answer 是正确的,但在我的场景中它需要一些额外的东西。
我正在使用 React Native 并且有一个 <TouchableWithoutFeedback disabled={true}> 组件。但是我可以看到,每当 XCUI 尝试检查其状态时,它都认为它已启用。事实上,使用断点并检查 element.IsEnabled 显然与我在 UI 中看到的相矛盾。
然而,使用AccessibilityState 可以实现这一点,例如:
<TouchableWithoutFeedback
accessibilityState={{
disabled: this.props.disabled,
}}
>
熟悉 RN 0.62.2
【讨论】:
等待和检查元素的更好方法不是delay() 函数,而是pushTimeout() 函数。 Apple 建议使用第二个功能。这是一个代码示例:
UIATarget.localTarget().pushTimeout(10)
button.tap()
UIATarget.localTarget().popTimeout()
Apple 将反复尝试点击该按钮并等待最多 10 秒。这是文档的link。
【讨论】:
如果您使用 NSPredicates 和期望,这实际上非常容易。您甚至可以设置超时值。此示例向您展示了如何使用 5 秒超时来执行此操作。
let exists = NSPredicate(format:"enabled == true")
expectationForPredicate(exists, evaluatedWithObject: app.tables.textFields["MY_FIELD_NAME"], handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
【讨论】:
您应该能够实现一个 while 循环来检查您想要的条件(例如启用按钮)。这将停止测试用例的进程,直到满足 while 条件并且测试将继续。构建延迟以减慢轮询并确保您有超时,以免无限期卡住。
伪代码:
While (/*button is disabled*/) {
if (/*timeout condition met*/) {
/*handle error*/
break;
}
UIATarget.delay(<duration in seconds>);
}
【讨论】: