Scroll until element is visible iOS UI Automation with xcode7

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

    Here is my version which I think is bullet proof (swift 4.0):

    import XCTest
    
    enum TestSwipeDirections {
        case up
        case down
        case left
        case right
    }
    
    fileprivate let min = 0.05
    fileprivate let mid = 0.5
    fileprivate let max = 0.95
    
    fileprivate let leftPoint = CGVector(dx: min, dy: mid)
    fileprivate let rightPoint = CGVector(dx: max, dy: mid)
    fileprivate let topPoint = CGVector(dx: mid, dy: min)
    fileprivate let bottomPoint = CGVector(dx: mid, dy: max)
    
    extension TestSwipeDirections {
        var vector: (begin: CGVector, end: CGVector) {
            switch self {
            case .up:
                return (begin: bottomPoint,
                        end:   topPoint)
            case .down:
                return (begin: topPoint,
                        end:   bottomPoint)
            case .left:
                return (begin: rightPoint,
                        end:   leftPoint)
            case .right:
                return (begin: leftPoint,
                        end:   rightPoint)
            }
        }
    }
    
    extension XCUIElement {
        @discardableResult func swipeOnIt(_ direction: TestSwipeDirections,
                                          swipeLimit: Int = 6,
                                          swipeDuration: TimeInterval = 1.0,
                                          until: () -> Bool) -> Bool {
            XCTAssert(exists)
    
            let begining = coordinate(withNormalizedOffset: direction.vector.begin)
            let ending = coordinate(withNormalizedOffset: direction.vector.end)
    
            var swipesRemaining = swipeLimit
            while !until() && swipesRemaining > 0 {
                begining.press(forDuration: swipeDuration, thenDragTo: ending)
                swipesRemaining = swipesRemaining - 1
            }
            return !until()
        }
    
        @discardableResult func swipeOnIt(_ direction: TestSwipeDirections,
                                          swipeLimit: Int = 6,
                                          swipeDuration: TimeInterval = 1.0,
                                          untilHittable element: XCUIElement) -> Bool {
            return swipeOnIt(direction, swipeLimit: swipeLimit, swipeDuration: swipeDuration) { element.isHittable }
        }
    
        @discardableResult func swipeOnIt(_ direction: TestSwipeDirections,
                                          swipeLimit: Int = 6,
                                          swipeDuration: TimeInterval = 1.0,
                                          untilExists element: XCUIElement) -> Bool {
            return swipeOnIt(direction, swipeLimit: swipeLimit, swipeDuration: swipeDuration) { element.exists }
        }
    }
    

    It take into account that item may not be found (in this case it should not hang). Also scroll is performed in steps of size of the item so search element will not pass through visible area what is possible in case of swipe.

提交回复
热议问题