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.
- 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.
- 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.
- 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.
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.
Interacting with Pickers
- adjust(toPickerWheelValue pickerWheelValue: String) – Changes the displayed value for the picker wheel.
- typeText(_ text: String) – Types a string into the element.