DEV Community

Cover image for XCUITest Actions: Tap, Type, Swipe, Scroll and Long Press
QAPulse by SK
QAPulse by SK

Posted on Originally published at skakarh.com

XCUITest Actions: Tap, Type, Swipe, Scroll and Long Press

XCUITest Actions are the interaction layer of iOS UI automation. After a test locates an XCUIElement, actions such as tap(), typeText(), swipeUp(), swipeDown(), and long press allow the test to reproduce real user behavior and validate application responses.

What are XCUITest Actions?

XCUITest actions are APIs provided by Apple’s XCUITest framework for interacting with UI elements during automated iOS tests.

A typical automation flow looks like this:

XCUIApplication
      ↓
XCUIElementQuery
      ↓
XCUIElement
      ↓
XCUITest Action
      ↓
Application State Change
      ↓
Assertion
Enter fullscreen mode Exit fullscreen mode

For example:

let app = XCUIApplication()

let loginButton =
    app.buttons["login.submitButton"]

loginButton.tap()
Enter fullscreen mode Exit fullscreen mode

The test first identifies the element and then performs an action against it.

Definition

XCUITest actions are interaction methods used to tap, type, swipe, scroll, press, and otherwise manipulate iOS UI elements during automated UI tests.

They allow SDETs to validate complete user journeys instead of testing application screens only through static assertions.

Key Points

  • tap() performs a standard tap.
  • doubleTap() performs a double tap.
  • typeText() enters text into supported controls.
  • swipeUp() and swipeDown() perform common swipe gestures.
  • swipeLeft() and swipeRight() support horizontal gestures.
  • press(forDuration:) performs long press interactions.
  • swipe(to:) supports element-to-element drag interactions.
  • waitForExistence() helps synchronize interactions.
  • isHittable helps determine whether an element can currently receive interaction.
  • Coordinate-based gestures should be used only when element-level interaction is insufficient.
  • Every important action should lead to a meaningful assertion.

The XCUITest Action Model

A reliable test should separate four responsibilities:

Find
  ↓
Wait
  ↓
Act
  ↓
Verify
Enter fullscreen mode Exit fullscreen mode

For example:

let app = XCUIApplication()

let emailField =
    app.textFields["login.emailField"]

XCTAssertTrue(
    emailField.waitForExistence(timeout: 10)
)

emailField.tap()
emailField.typeText("qa@example.com")

XCTAssertEqual(
    emailField.value as? String,
    "qa@example.com"
)
Enter fullscreen mode Exit fullscreen mode

The test does not simply interact with the UI.

It establishes that the UI is ready, performs the action, and validates the resulting state.

1. Tap Actions

The most common interaction is:

element.tap()
Enter fullscreen mode Exit fullscreen mode

Example:

let app = XCUIApplication()

let loginButton =
    app.buttons["login.submitButton"]

XCTAssertTrue(
    loginButton.waitForExistence(timeout: 10)
)

loginButton.tap()
Enter fullscreen mode Exit fullscreen mode

For a test framework, this is preferable to coordinate tapping because the action is associated with the semantic UI element.

Double Tap

Some applications use double-tap interactions.

let image =
    app.images["profile.avatar"]

image.doubleTap()
Enter fullscreen mode Exit fullscreen mode

This can be used for behaviors such as:

  • Zoom
  • Favorite actions
  • Image interactions
  • Custom gestures

Use it only when double tapping is part of the actual product behavior.

2. Type Actions

Text entry is another core XCUITest interaction.

let emailField =
    app.textFields["login.emailField"]

emailField.tap()
emailField.typeText("qa@example.com")
Enter fullscreen mode Exit fullscreen mode

For secure fields:

let passwordField =
    app.secureTextFields["login.passwordField"]

passwordField.tap()
passwordField.typeText("Password123!")
Enter fullscreen mode Exit fullscreen mode

Clear Existing Text

A field may already contain text.

A common approach is:

let field =
    app.textFields["profile.nameField"]

field.tap()

field.press(forDuration: 1.0)
Enter fullscreen mode Exit fullscreen mode

However, long pressing does not universally provide a reliable “select all” behavior across application implementations.

A more robust test architecture is to start from a known application state.

For example:

func testUpdateName() {

    let app = XCUIApplication()
    app.launchArguments = ["-UITestResetState"]
    app.launch()

    let nameField =
        app.textFields["profile.nameField"]

    XCTAssertTrue(
        nameField.waitForExistence(timeout: 10)
    )

    nameField.tap()
    nameField.typeText("Shahnawaz")

    XCTAssertEqual(
        nameField.value as? String,
        "Shahnawaz"
    )
}
Enter fullscreen mode Exit fullscreen mode

The principle is important:

Control test state instead of relying on unpredictable editing behavior.

Control test state instead of relying on unpredictable editing behavior.

3. Swipe Actions

XCUITest provides directional swipe methods.

element.swipeUp()
element.swipeDown()
element.swipeLeft()
element.swipeRight()
Enter fullscreen mode Exit fullscreen mode

Example:

let app = XCUIApplication()

let table =
    app.tables["settings.table"]

table.swipeUp()
Enter fullscreen mode Exit fullscreen mode

A swipe is useful for:

  • Moving through lists
  • Revealing content
  • Navigating collection views
  • Testing horizontally scrolling interfaces
  • Triggering swipe-based UI behavior

4. Scroll Actions

Scrolling deserves special attention because a scroll is often used to make another element available for interaction.

Example:

let app = XCUIApplication()

let settings =
    app.tables["settings.table"]

settings.swipeUp()

let logoutButton =
    app.buttons["settings.logoutButton"]

XCTAssertTrue(
    logoutButton.waitForExistence(timeout: 5)
)

logoutButton.tap()
Enter fullscreen mode Exit fullscreen mode

The test performs:

Settings Table
      ↓
Swipe Up
      ↓
Logout Becomes Available
      ↓
Wait
      ↓
Tap
      ↓
Verify
Enter fullscreen mode Exit fullscreen mode

Repeated Scrolling


👉 Continue reading the full article on skakarh.com →

Originally published at skakarh.com/xcuitest-actions.
Subscribe to QA Pulse by SK
weekly signal for QA, Test Automation and AI in Software Engineering.

Top comments (0)