You should extend the XCUIElement's method list. The first method (scrollToElement:
) will be called on the tableView, the second extension method helps you decide if the element is on the main window.
extension XCUIElement {
func scrollToElement(element: XCUIElement) {
while !element.visible() {
swipeUp()
}
}
func visible() -> Bool {
guard self.exists && !CGRectIsEmpty(self.frame) else { return false }
return CGRectContainsRect(XCUIApplication().windows.elementBoundByIndex(0).frame, self.frame)
}
}
The scrolling code should look like this (e.g. scrolling to last cell):
func testScrollTable() {
let app = XCUIApplication()
let table = app.tables.elementBoundByIndex(0)
let lastCell = table.cells.elementBoundByIndex(table.cells.count-1)
table.scrollToElement(lastCell)
}
Swift 3:
extension XCUIElement {
func scrollToElement(element: XCUIElement) {
while !element.visible() {
swipeUp()
}
}
func visible() -> Bool {
guard self.exists && !self.frame.isEmpty else { return false }
return XCUIApplication().windows.element(boundBy: 0).frame.contains(self.frame)
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…