当它不是可访问性元素时,是否可以通过其 `accessibilityId` 获取 `XCUIElement`?

Is it possible to get an `XCUIElement` by its `accessibilityId` when it is not an accessibility element?

我有自定义 UITableViewCell,其中包含一些子图像和标签。我将整个单元格作为一个单一的可访问元素。我已经为所有元素设置了辅助功能 ID - 单元格及其子视图,希望能够在 XCUITests.

中使用它

现在,我无法获取图像,比如说,使用 XCUIApplication().images["cell_image_acc_id"]

我应该怎么做才能获得这些元素?我是 XC ui 测试的新手。如果我缺少一些基本的东西,请告诉我。

提前致谢。

如果您将视图设置为辅助功能元素,则其子视图对 VoiceOver 用户不可用,因此不可测试。虽然它对辅助功能用户有好处,但可能会影响可测试性。

您可以在测试时为您的应用传递环境变量或启动参数。如果设置了此类变量(或参数),请不要在您的应用程序代码中设置 isAccessibilityElementhttps://developer.apple.com/documentation/objectivec/nsobject/1615141-isaccessibilityelement https://developer.apple.com/documentation/xctest/xcuiapplication/1500477-launcharguments https://developer.apple.com/documentation/xctest/xcuiapplication/1500427-launchenvironment How to detect if iOS app is running in UI Testing mode

这样您仍然可以为 VoiceOver 用户提供出色的体验并正确测试您的代码。