gpt4 book ai didi

swift - Xcode 11 : _XCTestCaseInterruptionException when using XCUIElement waitForExistence

转载 作者:行者123 更新时间:2023-12-03 20:59:56 34 4
gpt4 key购买 nike

我目前正在使用标准 XCUIElement 函数 waitForExistence(timeout: TimeInterval) 以等待按钮出现。我总是将超时设置为大于 2.0 秒,但一直遇到同样的问题。

Assertion Failure: Failed to get matching snapshots: Unable to perform work on main run loop, process main thread busy for 2.0s - recommend retry by client *** Terminating app due to uncaught exception '_XCTestCaseInterruptionException', reason: 'Interrupting test'



这太令人沮丧了,因为这开始发生在 Xcode 11 及更高版本上。更糟糕的是,您无法捕获异常,因为 waitForExistence 方法设置为从不抛出异常。关于如何捕捉这个的任何想法?被定位的元素只是 XCUIApplication().buttons["id"],所以它应该很好用。当应用程序正在检索数据时,不应与主线程断开连接。

最佳答案

我在始终有效的 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)
}

希望这可以帮助!

关于swift - Xcode 11 : _XCTestCaseInterruptionException when using XCUIElement waitForExistence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58810506/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com