我正在编写一个UI测试用例,在其中需要执行一个操作,然后在当前页面上,将唯一的UITableView
滚动到底部,以检查UITableView
的最后一个单元格内是否出现了特定的文本。
现在,我唯一想到的方法是使用app.tables.cells.element(boundBy: 0).swipeUp()
滚动它,但是如果单元格太多,它不会一直滚动到底部。而且UITableView
中的单元格数量并不总是相同的,我不能多次向上滑动,因为表中可能只有一个单元格。
最佳答案
一种解决方法是从tableView获取最后一个单元格。然后,运行一个while循环,滚动并检查每次滚动之间的单元格isHittable
。一旦确定isHittable == true
,就可以声明该元素。
https://developer.apple.com/documentation/xctest/xcuielement/1500561-ishittable
它看起来像这样(快速答案):
XCTestCase
文件中,编写查询以标识表。然后,进行后续查询以标识最后一个单元格。 let tableView = app.descendants(matching: .table).firstMatch
guard let lastCell = tableView.cells.allElementsBoundByIndex.last else { return }
isHittable
单元格。注意:isHittable
依赖于将单元格的userInteractionEnabled
属性设置为true //Add in a count, so that the loop can escape if it's scrolled too many times
let MAX_SCROLLS = 10
var count = 0
while lastCell.isHittable == false && count < MAX_SCROLLS {
apps.swipeUp()
count += 1
}
label
属性检查单元格的文本,并将其与期望的文本进行比较。 //If there is only one label within the cell
let textInLastCell = lastCell.descendants(matching: .staticText).firstMatch
XCTAssertTrue(textInLastCell.label == "Expected Text" && textInLastCell.isHittable)
关于Xcode UITest滚动到UITableView的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316004/