Last active
April 6, 2019 22:40
-
-
Save jineshqa/f572cd6ce97de990f80003dbeb11a343 to your computer and use it in GitHub Desktop.
code to scroll to an element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copied from https://stackoverflow.com/a/49944637 | |
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, until: { 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 } | |
} | |
} |
Hi,
Nice work writing this method! I curious to know how to do call it in a testFuncition for swiping up a view that has a XCUIElement called application.table.staticText["Today"]
Thanks,
Eric
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on swift 4.2, XCode 10 and iOS 12.