我使用的是xCode 7.1。我想自动化与表/集合视图中所有单元格的交互。我希望它是这样的:
for i in 0..<tableView.cells.count {
let cell = collectionView.cells.elementBoundByIndex(i)
cell.tap()
backBtn.tap()
}但是,此代码段仅查询表视图的当前后代,因此它将遍历数据源中总共n个单元格中的前m (m < n)个已加载单元格。
循环访问数据源中所有可用的单元格的最佳方式是什么?显然,查询.Cell后代不是正确的方法。
附言:我试图在每次点击单元格后在表格视图上执行卷动。然而,它可以刷到很远的地方(scrollByOffset不可用)。同样,不知道如何从数据源中提取总单元格数量。
干杯,列昂尼德
发布于 2015-11-10 04:18:12
所以这里的问题是您不能在不可见的单元格上调用tap()。SoI在XCUIElement - XCUIElement+UITableViewCell上写了一个扩展
func makeCellVisibleInWindow(window: XCUIElement, inTableView tableView: XCUIElement) {
var windowMaxY: CGFloat = CGRectGetMaxY(window.frame)
while 1 {
if self.frame.origin.y < 0 {
tableView.swipeDown()
}
else {
if self.frame.origin.y > windowMaxY {
tableView.swipeUp()
}
else {
break
}
}
}
}现在您可以使用此方法使您的单元格可见,然后点击它。
var window: XCUIElement = application.windows.elementBoundByIndex(0)
for i in 0..<tableView.cells.count {
let cell = collectionView.cells.elementBoundByIndex(i)
cell.makeCellVisibleInWindow(window, inTableView: tableView)
cell.tap()
backBtn.tap()
}发布于 2016-04-27 00:02:15
let cells = XCUIApplication().tables.cells
for cell in cells.allElementsBoundByIndex {
cell.tap()
cell.backButton.tap()
}发布于 2017-10-24 19:01:22
然而,在我的试验中,我遇到了同样的情况,您可以在不可见的单元格上执行tap()。然而,它是不可靠的,并且由于模糊的原因而失败。在我看来,这是因为在某些情况下,我在解析表时想要滚动到的下一个单元格没有加载。
所以这是我使用的技巧:在解析我的表格之前,我首先点击最后一个单元格,在我的例子中,我输入了一个可编辑的UITextField,因为所有其他点击都会触发一个段。
第一次点击()会导致滚动到最后一个单元格,从而加载数据。
然后我检查我的单元格内容
let cells = app.tables.cells
/*
this is a trick,
enter in editing for last cell of the table view so that all the cells are loaded once
avoid the next trick to fail sometime because it can't find a textField
*/
app.tables.children(matching: .cell).element(boundBy: cells.count - 1).children(matching: .textField).element(boundBy: 0).tap()
app.typeText("\r") // exit editing
for cellIdx in 0..<cells.count {
/*
this is a trick
cell may be partially or not visible, so data not loaded in table view.
Taping in it is will make it visible and so do load the data (as well as doing a scroll to the cell)
Here taping in the editable text (the name) as taping elsewhere will cause a segue to the detail view
this is why we just tap return to canel name edidting
*/
app.tables.children(matching: .cell).element(boundBy: cellIdx).children(matching: .textField).element(boundBy: 0).tap()
app.typeText("\r")
// doing my checks
}至少到目前为止,它对我来说是有效的,不确定这是不是100%有效,例如在非常长的列表上。
https://stackoverflow.com/questions/33612453
复制相似问题