我试图在我的UI测试中验证表视图是否已填充了一定数量的单元格。这是我的代码:
XCUIApplication().tables.element.swipeUp()
let count = XCUIApplication().tables.element.children(matching: .cell).count
XCTAssert(count > 0)
断言失败,因为count
始终为0,即使向上滑动显示成功填充的表视图也是如此。我也尝试过:
XCTAssert(app.tables.cells.count > 0)
具有相同的结果。我甚至在一个屏幕上创建了一个带有3个静态单元格的表视图的空项目,以消除UI测试的任何其他可能的干扰,并且它仍然总是返回0.我在iOS 11上使用Xcode 9进行测试
答案 0 :(得分:4)
细胞不会始终注册为细胞,具体取决于您配置辅助功能的方式。仔细检查可访问性实际上是否在Xcode menu -> Open Developer Tool -> Accessibility Inspector
中看到了单元格。
可访问性的可能性是staticTexts
而不是cells
,具体取决于您的布局代码 - 在这种情况下,您应断言
XCTAssert(app.tables.staticTexts.count > 0)
如果您需要单元格,请在tableView:cellForItemAtIndexPath
:
cell.isAccessibilityElement = true
cell.accessibilityLabel = cell.titleLabel.text // (for example)
cell.accessibilityIdentifier = "MyCellId" // (or whatever you want XCUITest to identify it as)
这会将单元格标记为根目录可访问的项目,而不是每个单元格的子视图都是离散元素。
答案 1 :(得分:0)
XCUIApplication().tables.children(matching: .cell).count
从表视图中删除所有行。使用Xcode 11.1编写,并使用SwiftUI进行了测试。
func deleteAllCellsFromTable() {
let app = XCUIApplication()
let tablesQuery = app.tables
app.navigationBars["Navigation Bar Title"].buttons["Edit"].tap()
while app.tables.children(matching: .cell).count > 0 {
let deleteButton = tablesQuery.children(matching: .cell).element(boundBy: 0).buttons["Delete "]
deleteButton.tap()
let deleteButton2 = tablesQuery.children(matching: .cell).element(boundBy: 0).buttons["trailing0"]
deleteButton2.tap()
}
}