我目前正在使用标准的XCUIElement函数waitForExistence(timeout: TimeInterval)来等待按钮的出现。我总是将超时设置为大于2.0秒,但始终遇到相同的问题。
断言失败:无法获得匹配的快照:无法在主运行循环上执行工作,进程主线程忙为2.0-建议由客户端*终止应用程序重试,原因:“中断测试”
这太令人沮丧了,因为这开始发生在Xcode 11和更高版本上。更糟糕的是,您无法捕获异常,因为waitForExistence方法被设置为从不抛出异常。有什么办法能抓到这个吗?所针对的元素只是XCUIApplication().buttons"id",所以它应该适合go。当应用程序正在检索数据时,不应该与主线程断开任何连接。
发布于 2019-11-25 14:28:43
在UI测试中,我使用的是自定义等待(我为XCTestCase创建了一个扩展):
public class var defaultTimeOut: TimeInterval { return 5 }
public func wait(forFulfillmentOf predicate: NSPredicate,
for element: XCUIElement,
withFailingMessage message: String = "Failed to fulfill predicate %@ for %@ within %.2f seconds.",
timeout: TimeInterval = XCTestCase.defaultTimeOut,
file: StaticString = #file,
line: UInt = #line) {
expectation(for: predicate, evaluatedWith: element, handler: nil)
waitForExpectations(timeout: timeout) { (error) -> Void in
guard error != nil else {
return
}
let failingMessage = String(format: message, arguments: [predicate, element, timeout])
self.recordFailure(withDescription: failingMessage, inFile: String(describing: file), atLine: Int(clamping: line), expected: true)
}
}然后,您可以在UI测试中实际使用此方法:
public func wait(forExistanceOf element: XCUIElement, timeout: TimeInterval = XCTestCase.defaultTimeOut, file: StaticString = #file, line: UInt = #line) {
let existancePredicate = NSPredicate(format: "exists == true")
wait(forFulfillmentOf: existancePredicate, for: element, withFailingMessage: "Failed to find %2$@ within %3$.2f seconds. Predicate was: %1$@.", timeout: timeout, file: file, line: line)
}希望这能有所帮助!
https://stackoverflow.com/questions/58810506
复制相似问题