XCUIElement Actions and Gestures

When writing UI Tests for our Swift mobile application, our test method needs to interact with different app UI elements by tapping on a button, swiping scroll view, typing text into a text field and etc. The UI test method is able to perform these different actions and gestures thanks to an XCUIElement class that provides gestural interactions such as tapping, pressing, swiping, pinching and rotating. Below is a list of actions and gestures you can use to interact with app UI elements from a UI test method.

If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App

Tapping

  • tap()Sends a tap event to a hittable point computed for the element.
  • doubleTap()Sends a double-tap event to a hittable point computed for the element.
  • twoFingerTap() – Available on iOS only. Sends a two-finger tap event to a hittable point computed for the element.
  • tap(withNumberOfTaps numberOfTaps: Int, numberOfTouches: Int) – Available on iOS only. Sends one or more taps with one or more touchpoints.

Below is a short Swift code example on how to use the tap() action in a UI Test method.

XCUIApplication().buttons["LoginButton"].tap()

Pressing

  • press(forDuration duration: TimeInterval) – Sends a long-press gesture to a hittable point computed for the element, holding for the specified duration.
  • press(forDuration duration: TimeInterval, thenDragTo otherElement: XCUIElement) – Initiates a press-and-hold gesture, then drags to another element.

Below is a short example of how to use the press(forDuration duration: TimeInterval) action.

XCUIApplication().buttons["loginThumbnailImageButton"].press(forDuration: 2)

Gestures

  • swipeUp() – Sends a swipe-up gesture.
  • swipeDown() – Sends a swipe-down gesture. 
  • swipeRight() – Sends a swipe-right gesture.
  • swipeLeft() – Sends a swipe-left gesture.
  • pinch(withScale scale: CGFloat, velocity: CGFloat) – Sends a pinching gesture with two touches.
  • rotate(_ rotation: CGFloat, withVelocity velocity: CGFloat) – Sends a rotation gesture with two touches.

Below is a short example of how to use the swipeUp() gesture.

XCUIApplication().tables["thumbnailImages"].swipeUp()

Interacting with Sliders

  • adjust(toNormalizedSliderPosition normalizedSliderPosition: CGFloat) – Changes the displayed value of the slider to one based on a normalized position. Zero corresponds to the minimum value of the slider, 1 corresponds to its maximum value.
XCUIApplication().sliders["progress"].adjust(toNormalizedSliderPosition: 0.8)

Interacting with Pickers

  • adjust(toPickerWheelValue pickerWheelValue: String) – Changes the displayed value for the picker wheel.
XCUIApplication().pickerWheels["months"].adjust(toPickerWheelValue: "September")

Typing Text

  • typeText(_ text: String) – Types a string into the element.
XCUIApplication().textFields["email"].typeText("[email protected]")

 

Leave a Reply

Your email address will not be published. Required fields are marked *