从UITest中选择集合视图中的第一个单元格

时间:2015-12-29 17:30:24

标签: ios xcode-ui-testing

如果第一个单元格中的UITest存在于集合视图中,是否可以从UITest中选择或触发didSelect?

录制时使用所选单元格中的静态文本。如果使用动态内容从网络填充单元格,并且集合视图可能不包含单元格,则此测试将中断。

1 个答案:

答案 0 :(得分:22)

您可以选择集合视图中的第一个单元格:

let app = XCUIApplication()
app.launch()

let firstChild = app.collectionViews.childrenMatchingType(.Any).elementBoundByIndex(0)
if firstChild.exists {
    firstChild.tap()
}

Swift 3

let firstChild = app.collectionViews.children(matching:.any).element(boundBy: 0)
if firstChild.exists {
     firstChild.tap()
}

从理论上讲,您的测试套件应该使用确定性数据。您应该始终确切地知道将从服务返回多少个单元格及其包含的内容。您可以通过使用种子开发服务器或在运行测试套件时模拟网络请求来实现此目的。