Scroll until element is visible iOS UI Automation with xcode7

后端 未结 9 828
小鲜肉
小鲜肉 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:21

    Update to @ravisekahrp's answer for newer Swift:

    extension XCUIElement {
        func isVisible() -> Bool {
            if !self.exists || !self.isHittable || self.frame.isEmpty {
                return false
            }
    
            return XCUIApplication().windows.element(boundBy: 0).frame.contains(self.frame)
        }
    }
    
    extension XCTestCase {
        func scrollToElement(_ element: XCUIElement) {
            while !element.isVisible() {
                let app = XCUIApplication()
                let startCoord = app.tables.element.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
                let endCoord = startCoord.withOffset(CGVector(dx: 0.0, dy: -262))
                startCoord.press(forDuration: 0.01, thenDragTo: endCoord)
            }
        }
    }
    

提交回复
热议问题