问题描述
使用 Xcode 12.4 模拟器 14.4
对于下面显示的警报,我在 XCUITests 中有以下函数,用于提取系统警报上显示的静态文本:
_ = addUIInterruptionMonitor(withDescription: "",handler: { (alert) -> Bool in
var data = alert.staticTexts.allElementsBoundByIndex.map {$0.label}
...
})
这在使用 Xcode 11.3 Sims 13.3 时有效;但是,我刚刚升级了 Xcode,现在它在运行 map 功能时崩溃并出现以下错误:
error: Execution was interrupted,reason: internal ObjC exception breakpoint(-8)..The process has been returned to the state before expression evaluation.
po alert.staticTexts.allElementsBoundByIndex.count -> returns 3
po alert.staticTexts.allElementsBoundByIndex[0].label -> returns "Allow “X” to use your location?"
po alert.staticTexts.allElementsBoundByIndex[1].label -> returns "Your location is used to find and display nearby X facilities."
po alert.staticTexts.allElementsBoundByIndex[2] -> returns StaticText,{{32.0,12.0},{72.5,16.0}},label: 'Precise: On'
但是,当我运行以下(或在我的测试期间运行)时,它失败了,但正如您从上面看到的,它在该位置确实有一个静态文本元素:
po alert.staticTexts.allElementsBoundByIndex[2].label -> No matches found for Element at index 2 from input {(
StaticText)} error: Execution was interrupted,reason: internal ObjC exception breakpoint(-8)
如果我留在控制台并再次重新运行相同的调用,它仍然可以工作:
po alert.staticTexts.allElementsBoundByIndex[2].label -> "Precise: On"
有谁知道是什么导致了这个错误或者我在测试中如何处理它的解决方案?
解决方法
由于此警报在您的应用程序“外部”,并且在您调试/暂停执行时一切正常,我不得不认为这与同步有关。
您是否有任何等待警报完全显示的内容?我通常不相信 UIInterruptionHandler 会做更多的事情,而不是自动关闭未测试的警报。
我使用的是页面对象模型,通常使用 _ = XCUIApplication().alerts.firstMatch.waitForExistence(timeout: 2.0)
之类的东西在初始化程序中等待新页面
这并不能解释为什么您在控制台中调用它两次的示例有效,但时间是我要寻找的地方。我觉得这是我唯一一次看到这样的错误。
这也可能与地图不是静态的或加载缓慢有关,因此在 XCUITest 认为它已完成加载并继续后更改了可访问性。抛出一个很长的 Thread.sleep
将是调试它的最简单方法。