Michel Camarotta
Michel Camarotta

Reputation: 31

There is a way to do a "long" Swipe Left gesture to delete an element in XCUITest

I'm trying to delete an element doing swipe left over it. This option exist in two ways in my application.

  1. Do short swipe left and the delete button appears, then you can press it. No problem here.
  2. Do long swipe left and the element will be deleted without second asking.

The second option I could not reproduce it with XCUIElements function.

The only function I found to do is element.swipeLeft() but did not found how to do a long swipe left.

Upvotes: 3

Views: 1310

Answers (1)

Oletha
Oletha

Reputation: 7649

You can use press(forDuration:thenDragTo:) to execute a drag from one side of the cell to the other.

let app = XCUIApplication()
let cell = app.cells.element(boundBy: 0) // first cell on the page

let rightOffset = CGVector(dx: 0.95, dy: 0.5)
let leftOffset = CGVector(dx: 0.05, dy: 0.5)

let cellFarRightCoordinate = cell.coordinate(withNormalizedOffset: rightOffset)
let cellFarLeftCoordinate = cell.coordinate(withNormalizedOffset: leftOffset)

// drag from right to left to delete
cellFarRightCoordinate.press(forDuration: 0.1, thenDragTo: cellFarLeftCoordinate)

Note that when creating coordinates from an element, you give a CGVector which is normalized to the size of the element (where 1.0 is the full width/height of the element), but if you create a coordinate from a coordinate, you must give a CGVector with absolute values.

Upvotes: 4

Related Questions