Scroll until element is visible iOS UI Automation with xcode7

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

    Solutions using swipeUp() and swipeDown() are not ideal because they can potentially scroll past the target element due to the momentum of the swipe. After much searching and frustration I found a magical method on XCUICoordinate:

    func press(forDuration duration: TimeInterval, thenDragTo otherCoordinate: XCUICoordinate)
    

    So we can do something like:

    let topCoordinate = XCUIApplication().statusBars.firstMatch.coordinate(withNormalizedOffset: .zero)
    let myElement = XCUIApplication().staticTexts["My Element"].coordinate(withNormalizedOffset: .zero)
    // drag from element to top of screen (status bar)
    myElement.press(forDuration: 0.1, thenDragTo: topCoordinate)
    

    As far as checking whether something is visible goes, you want to use isHittable in conjunction with exists. see scrollDownToElement in the extension below

    Here's a handy extension that will scroll until an element is on screen and then scroll that element to the top of the screen :)

    extension XCUIApplication {
        private struct Constants {
            // Half way accross the screen and 10% from top
            static let topOffset = CGVector(dx: 0.5, dy: 0.1)
    
            // Half way accross the screen and 90% from top
            static let bottomOffset = CGVector(dx: 0.5, dy: 0.9)
        }
    
        var screenTopCoordinate: XCUICoordinate {
            return windows.firstMatch.coordinate(withNormalizedOffset: Constants.topOffset)
        }
    
        var screenBottomCoordinate: XCUICoordinate {
            return windows.firstMatch.coordinate(withNormalizedOffset: Constants.bottomOffset)
        }
    
        func scrollDownToElement(element: XCUIElement, maxScrolls: Int = 5) {
            for _ in 0..

    Gist over here with added scrollUp methods

提交回复
热议问题