Forked from ryanmeisters/XCUIApplication+Scrolling.swift
Created
November 14, 2018 19:33
-
-
Save jineshqa/42d73bbfe882820c232744c5621c2ba4 to your computer and use it in GitHub Desktop.
Scrolling helpers automated ui tests using XCUIApplication
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
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..<maxScrolls { | |
if element.exists && element.isHittable { element.scrollToTop(); break } | |
scrollDown() | |
} | |
} | |
func scrollUpToElement(element: XCUIElement, maxScrolls: Int = 5) { | |
for _ in 0..<maxScrolls { | |
if element.exists && element.isHittable { element.scrollToTop(); break } | |
scrollUp() | |
} | |
} | |
func scrollDown() { | |
screenBottomCoordinate.press(forDuration: 0.1, thenDragTo: screenTopCoordinate) | |
} | |
func scrollUp() { | |
screenTopCoordinate.press(forDuration: 0.1, thenDragTo: screenBottomCoordinate) | |
} | |
} | |
extension XCUIElement { | |
func scrollToTop() { | |
let topCoordinate = XCUIApplication().screenTopCoordinate | |
let elementCoordinate = coordinate(withNormalizedOffset: .zero) | |
let delta = topCoordinate.screenPoint.x - elementCoordinate.screenPoint.x | |
let deltaVector = CGVector(dx: delta, dy: 0.0) | |
elementCoordinate.withOffset(deltaVector).press(forDuration: 0.1, thenDragTo: topCoordinate) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment