Scroll until element is visible iOS UI Automation with xcode7

后端 未结 9 829
小鲜肉
小鲜肉 2021-01-30 10:39

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 visib

相关标签:
9条回答
  • 2021-01-30 11:26

    you can do something like this:

    extension XCUIElement {
        internal func scrollToElement(element: XCUIElement) {
            while !element.exists {
                swipeDown()
            }
        }
    }
    

    and than use scrollToElement to find element

    0 讨论(0)
  • 2021-01-30 11:28

    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)
        }
    }
    
    0 讨论(0)
  • 2021-01-30 11:36

    in swift 4.2, if your element exist at bottom frame of table view or top frame of table view you can use this command to scroll up and scroll down to find element

    let app = XCUIApplication()
    app.swipeUp()
    

    or

    app.swipeDown()
    
    0 讨论(0)
提交回复
热议问题