2

For UI testing I want to perform a swipeRight-gesture to make further buttons accessible. The element I want to swipe is at the bottom of the screen. I access it by:

element.staticTexts["TEST TEXT"].swipeRight()

When performing the test the swipe goes not far enough. It does not trigger the element to slide completely to the right and show the further buttons I want to tap.

I have the feeling that swipeRight()grabs the middle of the static text and performs the gesture.

Is there any possibility to grab the element more to the left, such that it slides more to the right?

Thanks for any suggestions!

Phil24
  • 61
  • 6

2 Answers2

5

Try this approach:

    let startPoint = element.staticTexts["TEST TEXT"].coordinateWithNormalizedOffset(CGVectorMake(0, 0)) // center of the element
    let finishPoint = startPoint.coordinateWithOffset(CGVectorMake(1000, 0))
    startPoint.pressForDuration(0, thenDragToCoordinate: finishPoint)

You can adjust 1000 to reach the effect you want.

brigadir
  • 6,874
  • 6
  • 46
  • 81
  • 1
    The 1000 seems to be a much larger value than needed at least in Xcode 8 and Swift 3. It seems like 1.0 works pretty well but I'm still trying to figure out what coordinates are being used. Does anyone have any details on that? Is 0,0 the middle of the UI Element being swiped? – spig Aug 01 '17 at 19:45
2

At least as of Swift 4.1, Xcode 9.4.1, the values you need represent a percentage of the element's width and height - 0.0 to 1.0.

For a long swipe, you need to drag across the element about 60%, so like this:

// right long swipe - move your 'x' from 0.0 to 0.6
let startPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0))
let endPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.6, dy: 0.0))
startPoint.press(forDuration: 0, thenDragTo: endPoint)

and:

// left long swipe - move your 'x' from 0.6 to 0.0
let startPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.6, dy: 0.0))
let endPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0))
startPoint.press(forDuration: 0, thenDragTo: endPoint)

You could set your dy value to 0.5, say, if you're wanting to drag at the center of the element. But, note that your dy values must be the same for swiping right or left. If you were to convert this for a swipe up or down, the dx values would need to be identical, while the dy values would change appropriately instead. (The swipe needs to move along a straight line.)

Note, also, that the forDuration value represents the number of seconds to hold the touch before dragging. The higher the value, the longer the initial press.

I posted an XCUIElement extension for long swipes in this answer.

leanne
  • 7,940
  • 48
  • 77