---
title: Write mobile tests
description: The YAML format for native Android and iOS tests. Declare the platform and app, choose a reset policy, locate elements by accessibility ID, resource ID, predicate, or XPath, and mix explicit and AI steps.
---

# Write mobile tests

<div class="view-markdown-wrapper">
<ViewMarkdown />
</div>

A mobile test is a `*.test.yaml` file like a web test, with two additions at the top: `platform`, and an `app` block that names the app under test. Steps use the same `intent:` and `action:` form, and `STEP`, `IF`, `WHILE`, `parameters`, `beforeEach`, `afterEach`, `teardown`, and [templates](/local/yaml-tests/templates) work as they do on the web. See [YAML Test Format](/local/yaml-tests/) for the shared structure.

## An Android test

```yaml
goal: Type into a text field and read it back
platform: android
app:
  path: ./build/ApiDemos-debug.apk
  identifier: io.appium.android.apis
statements:
  - intent: Open the Views category
    action: click
    locator: { kind: accessibility_id, value: Views }
  - intent: Scroll to the Controls demo
    action: scroll_to_element
    locator: { kind: accessibility_id, value: Controls }
  - intent: Open the Controls demo
    action: click
    locator: { kind: accessibility_id, value: Controls }
  - intent: Open the light theme controls
    action: click
    locator: { kind: accessibility_id, value: 1. Light Theme }
  - intent: Type into the text field
    action: input_text
    locator: { kind: id, value: io.appium.android.apis:id/edit }
    text: Hello from Shiplight
  - intent: Hide the keyboard
    action: hide_keyboard
  - intent: Check the text field holds the typed text
    action: assert_text
    locator: { kind: id, value: io.appium.android.apis:id/edit }
    equals: Hello from Shiplight
```

This test uses the open-source [ApiDemos](https://github.com/appium/android-apidemos) app. Every step is explicit, so it runs without AI calls.

## An iOS test

```yaml
goal: Sign in, then replace and clear the username
platform: ios
app:
  path: ./build/MyApp.app
  identifier: com.example.myapp
statements:
  - intent: Open the login screen
    action: click
    locator: { kind: accessibility_id, value: nav.login }
  - intent: Type the username
    action: input_text
    locator: { kind: accessibility_id, value: login.username }
    text: ada
  - intent: Type the password into the secure field
    action: input_text
    locator: { kind: accessibility_id, value: login.password }
    text: "{{PASSWORD}}"
  - intent: Hide the keyboard
    action: hide_keyboard
  - intent: Sign in
    action: click
    locator: { kind: accessibility_id, value: login.submit }
  - intent: Check the greeting
    action: assert_text
    locator: { kind: accessibility_id, value: login.message }
    equals: Welcome, ada
  - intent: Clear the username
    action: clear_input
    locator: { kind: accessibility_id, value: login.username }
```

On iOS, `path` is the `.app` folder that Xcode builds for the simulator, for example with `xcodebuild -sdk iphonesimulator`. Variables such as <code v-pre>{{PASSWORD}}</code> work as in web tests; see [Variables](/local/yaml-tests/variables).

## The app block

| Field              | Platforms | Description                                                                                                        |
| ------------------ | --------- | ------------------------------------------------------------------------------------------------------------------ |
| `identifier`       | Both      | Required. The Android package name, or the iOS bundle identifier                                                   |
| `path`             | Both      | The app to install, relative to the YAML file: an `.apk` on Android, a simulator `.app` folder on iOS. Use with a local profile |
| `appId`            | Both      | An app already uploaded to a remote device service. Use with a [remote profile](/mobile/#remote-appium-endpoint) instead of `path` |
| `device`           | Both      | The device profile to use. Default: `defaultDevice`, or the only profile                                          |
| `resetPolicy`      | Both      | `fresh` (default), `reinstall`, or `none`. See below                                                               |
| `activity`         | Android   | The activity to launch. Default: the package's launcher activity                                                  |
| `grantPermissions` | Android   | Grant the app's runtime permissions on install. Default: `true`                                                    |

Set exactly one of `path` and `appId`.

### Reset policy

`resetPolicy` sets what the app keeps from earlier runs when a test opens it:

| Policy             | Android                                                       | iOS                                                    |
| ------------------ | ------------------------------------------------------------- | ------------------------------------------------------ |
| `fresh` (default)  | Installs your build over the app, then clears its data        | Removes the app and installs your build               |
| `reinstall`        | Uninstalls the app and installs your build                    | Removes the app and installs your build               |
| `none`             | Installs your build over the app and keeps its data           | Installs your build over the app and keeps its data   |

Every policy runs the build you name, so a test never runs a stale copy left on the device. With `none`, if the installed copy can't be updated in place (for example, it was signed with another key), the test stops with a message rather than delete the data. Uninstall the app, or use `fresh`.

Use `none` when a test depends on state an earlier test left, and run those files in order with `workers: 1`.

## Locators

Native steps locate their element with `locator: { kind, value }`. Each platform has its own kinds:

| Kind               | Platform | Matches                                                               | Example                                                           |
| ------------------ | -------- | --------------------------------------------------------------------- | ----------------------------------------------------------------- |
| `accessibility_id` | Both     | The content description (Android) or accessibility identifier (iOS)   | `{ kind: accessibility_id, value: login.submit }`                |
| `id`               | Android  | The resource ID                                                       | `{ kind: id, value: com.example.shop:id/email }`                  |
| `uiautomator`      | Android  | A UiAutomator selector                                                | `{ kind: uiautomator, value: 'new UiSelector().text("WebView")' }` |
| `predicate`        | iOS      | An NSPredicate string                                                 | `{ kind: predicate, value: "type == 'XCUIElementTypeButton' AND name == 'add'" }` |
| `class_chain`      | iOS      | An XCUITest class chain                                               | ``{ kind: class_chain, value: "**/XCUIElementTypeStaticText[`name == 'row.1'`]" }`` |
| `xpath`            | Both     | An XPath over the native element tree                                 | `{ kind: xpath, value: //android.webkit.WebView }`               |

Prefer `accessibility_id`, then `id` on Android. They survive layout changes better than `xpath`. Inside a WebView, locators are `css` and `xpath` over the page's DOM; see [Hybrid apps](/mobile/hybrid-apps).

## Actions

Explicit steps take an `action:` and its parameters. Steps that act on an element also take a `locator:`.

| Group                  | Actions                                                                                         |
| ---------------------- | ----------------------------------------------------------------------------------------------- |
| Touch                  | `click`, `double_click`, `long_press`, `swipe`, `drag_drop`, `scroll`, `scroll_to_element`      |
| Text                   | `input_text` (clears the field first), `clear_input`, `hide_keyboard`                          |
| Keys and navigation    | `home`, `open_deep_link`; on Android also `back` and `press`                                   |
| App lifecycle          | `launch_app`, `terminate_app`, `reset_app`                                                      |
| Device                 | `set_orientation` (`portrait` or `landscape`)                                                   |
| System alerts          | `accept_alert`, `dismiss_alert`, each with an optional exact `button` name                      |
| Assertions             | `assert_visible`, `assert_absent`, `assert_text` (with `equals` or `contains`)                 |
| Screen coordinates     | `coordinate_tap`, `coordinate_double_tap`, `coordinate_long_press`, `coordinate_drag`          |
| Contexts               | `switch_context` (see [Hybrid apps](/mobile/hybrid-apps))                                       |
| Other                  | `wait`, `save_variable`, `generate_2fa_code`                                                    |

`launch_app` and `terminate_app` act on the test app unless you pass another `identifier`. `reset_app` clears the test app's data on Android, or reinstalls it on iOS, and relaunches it. A `URL:` step opens a deep link, so it needs a full URL such as `myapp://cart`; mobile tests have no `base_url`.

For every action's parameters, print the reference for a platform:

```bash
bun run shiplight spec actions --platform android
bun run shiplight spec actions --platform ios
```

Some examples:

```yaml
  - intent: Press Backspace to delete the last letter
    action: press
    keys: Backspace
  - intent: Open the app through its deep link
    action: open_deep_link
    url: myapp://hello
  - intent: Allow notifications in the system alert
    action: accept_alert
    button: Allow
  - intent: Rotate to landscape
    action: set_orientation
    orientation: landscape
  - intent: Go to the device home screen
    action: home
  - intent: Bring the app back
    action: launch_app
```

## AI steps

The AI statements from web tests work on mobile too. Shiplight's hosted service reads the screen and decides each one, so these steps need a [Shiplight API token](/mobile/#what-needs-a-shiplight-account):

```yaml
statements:
  - intent: Open the settings
  - VERIFY: The settings screen is shown
  - WAIT_UNTIL: The list has loaded
    timeout_seconds: 20
  - intent: Read the order number
    action: ai_extract
    element_description: the order number
    variable_name: order
  - IF: A dialog is open
    THEN:
      - intent: Close it
        action: back
    ELSE:
      - intent: Go home
        action: home
```

An `intent:` with no `action:` is planned on every run. With a token, an explicit step whose locator no longer matches is healed: Shiplight finds the element again from the step's intent. Assertions are not healed. Without a token, a step whose locator doesn't match fails.

## Related

- [Mobile app testing](/mobile/): devices and setup
- [Hybrid apps](/mobile/hybrid-apps): WebViews in a native app
- [Statement Types](/local/yaml-tests/statement-types): `STEP`, `IF`, `WHILE`, and the other shared statements
