滚动直到可见元素,并使用xcode7进行iOS UI自动化 [英] Scroll until element is visible iOS UI Automation with xcode7

查看:176
本文介绍了滚动直到可见元素,并使用xcode7进行iOS UI自动化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,通过新的xcode更新,Apple改进了我们进行UI测试的方式.在工具中,我们使用了Java脚本函数"isVisible"来确定目标元素是否可见.

So with the new xcode update apple has revamped the way we do UI testing. In instruments we used java script function "isVisible" to determine if our targeted element is visible.

我试图在目标c中复制它,但是我似乎找不到与之等效的东西.我有一个表格视图,上面有两个标签的原型单元.可以说这个原型单元被重复使用了50次.

I'm trying to replicate this in objective c but i can't seem to find the equivalent of this. I have a table view, a prototype cell with two labels on it. This prototype cell is reused 50 times lets say.

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

I'm trying to scroll until the last cell is visible, i did this by doing this:

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];
}

但是不会滑动,因为加载视图时该元素存在.请帮忙,因为这使我发疯.

But this won't swipe since the element exists when the view is loaded. Please help cause this is driving me crazy.

推荐答案

您应该扩展XCUIElement的方法列表.第一个方法(scrollToElement:)将在tableView上调用,第二个扩展方法可帮助您确定元素是否在主窗口中.

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

快捷键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)
    }
}

这篇关于滚动直到可见元素,并使用xcode7进行iOS UI自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆