滚动直到元素可见iOS UI Automation with xcode7

所以随着新的 xcode更新,苹果已经改变了我们进行UI测试的方式.在仪器中,我们使用 java脚本函数“isVisible”来确定我们的目标元素是否可见.

我试图在客观c中复制这个,但我似乎找不到相当于这个.我有一个表视图,一个原型单元格上有两个标签.这个原型单元可以重复使用50次.

我试图滚动直到最后一个单元格可见,我这样做:

if (![[[[[[XCUIApplication alloc] init].tables childrenMatchingType:XCUIElementTypeCell] matchingIdentifier:@"cell"] elementBoundByIndex:49].staticTexts[@"text"] exists]) {
        [[[[[[XCUIApplication alloc] init].tables childrenMatchingType:XCUIElementTypeCell] matchingIdentifier:@"cell"] elementBoundByIndex:0].staticTexts[@"text"] swipeUp];
}

但是,由于元素在加载视图时存在,所以不会刷卡.请帮忙,因为这让我疯狂.

解决方法

您应该扩展XCUIElement的方法列表.第一个方法(scrolltoElement :)将在tableView上调用,第二个扩展方法可以帮助您确定元素是否在主窗口上.
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)
    }

}

滚动代码应该这样(例如滚动到最后一个单元格):

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)
    }
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...