ios - 无法访问XCTest中以编程方式添加的UIView

标签 ios xcode swift3 xctest ui-testing

下面是我录制的XCTest

    let app = XCUIApplication()
    let tablesQuery = app.tables
    tablesQuery.staticTexts["Video"].tap()
    tablesQuery.staticTexts["\tWindowed"].tap()
    app.buttons["Launch"].tap()
    app.buttons["Popout Video"].tap()
    app.children(matching: .window).element(boundBy: 0).children(matching: .other).element(boundBy: 1).tap()

当我尝试运行测试时,最后一部分是:

    app.children(matching: .window).element(boundBy: 0).children(matching: .other).element(boundBy: 1).tap()

无法访问。它不会抛出任何错误,但最后一行代码不会执行。

我尝试通过引用以下 stackoverflow 问题来解决该问题: Xcode UI Tests can't find views that are added programatically

此外,我还引用了以下 Apple 文档:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/iPhoneAccessibility/Making_Application_Accessible/Making_Application_Accessible.html#//apple_ref/doc/uid/TP40008785-CH102-SW2

https://developer.apple.com/reference/uikit/uiview

但这一切似乎并不能解决问题。任何帮助将不胜感激。

最佳答案

有问题的线路非常脆弱。您的应用程序的 View 可能并不总是在每次应用程序启动时以相同的顺序提供,具体取决于竞争条件,因此记录 session 中记录的内容不一定适用于应用程序的所有启动。您可能会发现代码实际上正在运行,但没有点击您期望的元素。

为了降低代码的脆弱性,请在应用代码中向 UIView 添加辅助功能标识符,并使用 otherElements 查询来查找 UIView。

// app code
let view: UIView!
view.accessibilityIdentifier = "myAccessibilityIdentifier"

// UI test code
app.otherElements["myAccessibilityIdentifier"].tap()

关于ios - 无法访问XCTest中以编程方式添加的UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41412339/

相关文章:

javascript - 设备就绪处理程序未被调用 ios phonegap 3.3.3

ios - 更新配置文件

xcode - AVAudioPlayer 在播放前释放?

generics - 在Swift中,您可以将泛型限制为两种类型吗?字符串和整数

ios - 应用程序因连接到 IPv6 网络而被拒绝,以确保其启动时不会崩溃

ios - 如何在 View 中延迟/异步加载各种元素(标签中的数据)?

ios - Qt4iOS XCode 找不到 QUIWebView 类

ios - NSDateFormatter 时区格式出现问题

swift - 无法使用类型为 'value' 的参数列表调用 '(String)'

ios - 如何跟踪歌曲何时完成 AVAudioPlayer