首页 > 解决方案 > Xcode 14.4 Sims XCUITest 获取模拟器警报数据错误 - 内部 ObjC 异常断点

问题描述

使用 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,现在它在运行地图功能时崩溃并给出以下错误:

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"

有谁知道是什么导致了这个错误或我如何在我的测试中处理它的解决方案?

标签: iosxcode12xcuitestxctestcase

解决方案


由于此警报在您的应用程序“外部”并且在您调试/暂停执行时一切正常,我不得不认为这与同步有关。

你有什么等待警报完全显示的吗?我通常不相信 UIInterruptionHandler 会自动关闭未测试的警报。

我正在使用页面对象模型,通常使用类似的东西在初始化程序中等待新页面_ = XCUIApplication().alerts.firstMatch.waitForExistence(timeout: 2.0)

这并不能解释为什么您在控制台中调用它两次的示例有效,但时间是我要寻找的地方。我觉得这是我唯一一次看到这样的错误。

它也可能与地图不是静态的或加载缓慢有关,因此在 XCUITest 认为它已完成加载并继续之后更改可访问性。投入很长的时间Thread.sleep将是调试它的最简单方法。


推荐阅读