我正在测试 XCUItest 中单元格内容的表格 View 。就我而言,我不知道单元格文本的顺序,也不允许我为文本设置辅助功能 ID。给定里面的文本,如何获取单元格的索引?
例如,如果我想获取包含文本“Cell 2 Text”的单元格的索引,我会尝试这样的操作:
func testSample() {
let app = XCUIApplication()
let table = app.tables
let cells = table.cells
let indexOfCell2Text = cells.containing(.staticText, identifier: "Cell 2 Text").element.index(ofAccessibilityElement: "I dunno")
print(indexOfCell2Text)
}
我觉得我很接近,但我不确定。谁能提出解决方案?
如果之前有人问过这个问题,我深表歉意。我找不到任何关于此的具体信息。
我之前访问过的引用资料: https://developer.apple.com/documentation/xctest/xcuielementquery/1500842-element
How can I verify existence of text inside a table view row given its index in an XCTest UI Test?
最佳答案
真正最可靠的方法是将索引添加到可访问性标识符中。但是,你不能。您可以更改单元格的辅助功能标识符而不是文本吗?
无论如何,如果你不滚动你的 TableView ,你可以这样处理:
let idx = 0
for cell in table.cells.allElementsBoundByIndex {
if cell.staticTexts["Text you are looking for"].exists {
return idx
}
idx = idx + 1
}
否则,您将使用的索引与屏幕上显示的单元格相关。因此,在滚动之后,新的第一个可见单元格将成为索引 0 处的单元格,并且会搞砸您的搜索。
关于swift - 如何在给定 XCUITest 中的文本的情况下获取单元格的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56637126/