Skip to content

Instantly share code, notes, and snippets.

@UglyBlueCat
Created January 19, 2018 15:32
Show Gist options
  • Save UglyBlueCat/9c9657574924bbe8e1f0d5efac355d28 to your computer and use it in GitHub Desktop.
Save UglyBlueCat/9c9657574924bbe8e1f0d5efac355d28 to your computer and use it in GitHub Desktop.
Recently I found that XCUIElement.swipeUp() was far too violent, swiping way past the element in the UI I wanted my test to tap, so I wrote an extension to XCUIElement that swiped gently using XCUIElement.press(forDuration:thenDragTo:)
//
// XCUIElement+GentleSwipe.swift
//
// Created by Robin Spinks on 11/10/2017.
//
import Foundation
import XCTest
extension XCUIElement
{
enum direction : Int {
case Up, Down, Left, Right
}
func gentleSwipe(_ direction : direction) {
let half : CGFloat = 0.5
let adjustment : CGFloat = 0.25
let pressDuration : TimeInterval = 0.05
let lessThanHalf = half - adjustment
let moreThanHalf = half + adjustment
let centre = self.coordinate(withNormalizedOffset: CGVector(dx: half, dy: half))
let aboveCentre = self.coordinate(withNormalizedOffset: CGVector(dx: half, dy: lessThanHalf))
let belowCentre = self.coordinate(withNormalizedOffset: CGVector(dx: half, dy: moreThanHalf))
let leftOfCentre = self.coordinate(withNormalizedOffset: CGVector(dx: lessThanHalf, dy: half))
let rightOfCentre = self.coordinate(withNormalizedOffset: CGVector(dx: moreThanHalf, dy: half))
switch direction {
case .Up:
centre.press(forDuration: pressDuration, thenDragTo: aboveCentre)
break
case .Down:
centre.press(forDuration: pressDuration, thenDragTo: belowCentre)
break
case .Left:
centre.press(forDuration: pressDuration, thenDragTo: leftOfCentre)
break
case .Right:
centre.press(forDuration: pressDuration, thenDragTo: rightOfCentre)
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment