Scroll until element is visible iOS UI Automation with xcode7

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

    All the previous answers are not 100% fail proof. The problem I was facing is that swipeUp() has a larger offset and I couldn't find a way to stop the scrolling when I have the element in view port. Sometimes the element gets scrolled away because of the excessive scroll and as a result test case fails. However I managed to control the scroll using the following piece of code.

    /**
    Scrolls to a particular element until it is rendered in the visible rect
    - Parameter elememt: the element we want to scroll to
    */
    func scrollToElement(element: XCUIElement)
    {
        while element.visible() == false
        {
            let app = XCUIApplication()
            let startCoord = app.collectionViews.element.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.5))
            let endCoord = startCoord.coordinateWithOffset(CGVector(dx: 0.0, dy: -262));
            startCoord.pressForDuration(0.01, thenDragToCoordinate: endCoord)
        }
    }
    
    func visible() -> Bool
    {
        guard self.exists && self.hittable && !CGRectIsEmpty(self.frame) else
        {
            return false
        }
    
        return CGRectContainsRect(XCUIApplication().windows.elementBoundByIndex(0).frame, self.frame)
    }
    

    Note : Please use app.tables if your view is tableview based

提交回复
热议问题