Replicate pull to refresh in XCTest UI testing
You can use the XCUICoordinate
API to interact with specific points on the screen, not necessarily tied to any particular element.
- Grab a reference to the first cell in your table. Then create a coordinate with zero offset,
CGVectorMake(0, 0)
. This will normalize a point right on top of the first cell. - Create an imaginary coordinate farther down the screen. (I've found that a
dy
of six is the smallest amount needed to trigger the pull-to-refresh gesture.) - Execute the gesture by grabbing the first coordinate and dragging it to the second one.
let firstCell = app.staticTexts["Adrienne"]
let start = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 0))
let finish = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 6))
start.pressForDuration(0, thenDragToCoordinate: finish)
More information along with a working sample app is also available.
Here is a Swift 5.1 version
let firstCell = staticTexts["Id"]
let start = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0))
let finish = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 100))
start.press(forDuration: 0, thenDragTo: finish)
I managed to do such tasks with coordinates like Joe suggested. But i went a step further using coordinateWithOffset()
let startPosition = CGPointMake(200, 300)
let endPosition = CGPointMake(200, 0)
let start = elementToSwipeOn.coordinateWithNormalizedOffset(CGVectorMake(0, 0)).coordinateWithOffset(CGVector(dx: startPosition.x, dy: startPosition.y))
let finish = elementToSwipeOn.coordinateWithNormalizedOffset(CGVector(dx: 0, dy: 0)).coordinateWithOffset(CGVector(dx: endPosition.x, dy: endPosition.y))
start.pressForDuration(0, thenDragToCoordinate: finish)
Now i am able to drag from a specific point to another specific point. I implemented a custom refresh on some of my views. While implementing this, i also discovered that i can even use this to access the control center or the top menu.