<!-- KeepSnappy docs for keepsnappy CLI 0.1.0-alpha.35 -->

# Appium

> What changes for your Appium (WebdriverIO) tests when you add KeepSnappy.

This page is for teams whose end-to-end tests are an Appium suite, for example with WebdriverIO. It covers only what is different with Appium. [Getting started](https://keepsnappy.com/docs/getting-started.md) has the rest of the setup.

The examples use iOS, on a simulator in GitHub Actions. For Android, or for real phones on BrowserStack, see [Other setups](#other-setups).

With Appium, KeepSnappy wraps the test command you already run, such as `npx wdio run wdio.conf.js`. It runs that command 5 times per session by default, and measures what the app does while your tests drive it. Your test code, helpers and test data setup stay as they are.

## Install

You need an Appium setup that already runs on an iOS simulator. The [CI example](#ci-example) installs Appium's iOS driver, `xcuitest`.

Init installs the SDK and the `keepsnappy` command at [versions that work together](https://keepsnappy.com/docs/install.md#versions-that-work-together). Your CI job builds the app with `KEEPSNAPPY=1` and installs it on the simulator. If your app has no Release profile, see [Make a Release build](https://keepsnappy.com/docs/sdk.md#make-a-release-build).

In your Appium setup:

- Point Appium at the simulator that KeepSnappy measures (the one in `device`), and at the installed app.
- Don't let the Appium session reinstall the app or erase the simulator when it ends. KeepSnappy reads the app's data after your command exits.
- Keep `maxInstances` at 1. Two sessions at once on one simulator would measure each other.

The capabilities of a `wdio.conf.js` for this:

```js
exports.config = {
  runner: 'local',
  specs: ['./e2e/**/*.e2e.js'],
  maxInstances: 1,
  capabilities: [
    {
      platformName: 'iOS',
      'appium:automationName': 'XCUITest',
      'appium:udid': process.env.SIMULATOR_UDID,
      'appium:bundleId': 'com.example.app',
    },
  ],
  framework: 'mocha',
  services: ['appium'],
};
```

Your Appium and WebdriverIO setup may differ. See [appium.io](https://appium.io) and [webdriver.io](https://webdriver.io).

Use a Node.js release supported by your WebdriverIO version. For example, WebdriverIO 10.2.0 installs on Node 22.23.4, and its Node 24 range starts at 24.22.1. If your job uses a prebuilt WebDriverAgent, match it to the XCUITest driver that will start it. Keep the Appium, driver and WebDriverAgent versions together in the job's setup.

## Settings in keepsnappy.yml

Init writes `verdict_gate`, `app.bundle_id`, `flows.runner` and `flows.command`, with your own test command. The other settings are optional. [keepsnappy.yml and run flags](https://keepsnappy.com/docs/settings.md#keepsnappyyml) lists every setting.

```yaml
verdict_gate: advisory
app:
  bundle_id: com.example.app
device: booted
flows:
  runner: command
  command: npx wdio run wdio.conf.js
timeout_seconds: 900
```

- `flows.command`: your test command, as one line or as a list of arguments. KeepSnappy runs it as it is, without a shell. It must exit 0 when the tests pass.
- `device`: the simulator's id (its UDID, from `xcrun simctl list devices booted`), or `booted`. Your Appium capabilities must name the same simulator. In CI, set `SIMULATOR_UDID` in the job and read it in the capabilities, as the CI example does. KeepSnappy groups uploaded runs by [simulator runtime](https://keepsnappy.com/docs/settings.md#simulator-and-emulator-identity).
- `timeout_seconds`: the longest that one run of the command may take. A first Appium session on a fresh CI machine starts slower than the next ones, so leave room.

## Journeys

The whole command is one journey, named `e2e`. To give it a name of your own, use `--scenario`. The part after `--` is the test command that KeepSnappy runs:

```sh
npx --no-install keepsnappy run --scenario chat --upload --wait -- npx wdio run wdio.conf.js
```

For a second path through the app, run a second command with its own `--scenario` name, as its own job. Each name has its own history on `main`. Keep the names stable: a new name is a new journey, with no history on `main`.

## Journeys that measure the same way

KeepSnappy compares the journey with itself across commits, so the suite must do the same work every time. [Journeys](https://keepsnappy.com/docs/journeys.md#write-journeys-that-measure-the-same-way-every-run) has the rules for every test tool. In an Appium test:

- Use the same data every run ([why and how](https://keepsnappy.com/docs/journeys.md#use-the-same-data-every-run)). Reset the test account or the mock server through your backend in WebdriverIO's `beforeSession` hook, so the data is ready before the app launches. Don't clear the app.
- Start from the same state. The test launches the installed app fresh.
- Wait on a condition. `waitForDisplayed` with a `timeout` waits for an element, and fails the test when it doesn't show. Don't use `pause`.
- Find elements by accessibility id. On iOS, a React Native `testID` is the accessibility id, so `$('~open-chat')` finds it fastest. A lookup by text or XPath has to search the whole screen.
- Look up each element once. A tap waits for its element, so a `waitForDisplayed` on the same element before the tap is a second lookup.
- Scroll with gestures of a fixed shape: the same count, start, end and duration in every run. Don't look for an element between two measured gestures.
- Keep retries at 0. Set `specFileRetries` and the retries of your test framework to 0. KeepSnappy doesn't notice an Appium retry, so a retry that passes adds its extra work to the measurement.
- End on the home screen, with the last screen closed.

```js
describe('scroll a chat', () => {
  it('opens a chat, scrolls it and goes back home', async () => {
    await $('~open-chat').click();
    await $('~message-list').waitForDisplayed({ timeout: 10000 });
    for (let i = 0; i < 3; i += 1) {
      await driver.execute('mobile: dragFromToForDuration', { duration: 0.4, fromX: 200, fromY: 600, toX: 200, toY: 200 });
    }
    await $('~back').click();
    await $('~home').waitForDisplayed({ timeout: 10000 });
  });
});
```

There is no step helper for an Appium suite. The SDK finds the screens and taps by itself, and results are correct without step names.

Change the suite in its own pull request. A pull request that edits a test is compared with a `main` that ran the old test.

## CI example

[Init writes](https://keepsnappy.com/docs/ci.md#what-init-writes-in-your-workflow) a job like this for you, so copy this one only when init can't use your job. Like the [hand-made workflow](https://keepsnappy.com/docs/ci.md#a-complete-workflow), it runs the latest CLI release through `npx` and runs nightly at 03:00 UTC. It is one job on a macOS runner. It builds the app with the SDK, boots a simulator, installs Appium's iOS driver and runs `keepsnappy run` around your command.

```yaml
# .github/workflows/keepsnappy.yml
name: KeepSnappy
on:
  pull_request:
  push:
    branches: [main] # your default branch
  schedule:
    - cron: '0 3 * * *'
  workflow_dispatch:
jobs:
  keepsnappy:
    runs-on: macos-15
    timeout-minutes: 90
    env:
      KEEPSNAPPY: '1'
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}
          fetch-depth: 0
      - uses: actions/setup-node@v4
        with:
          node-version: '24'
      - run: npm ci
      - name: Boot a simulator
        run: |
          UDID=$(xcrun simctl list devices --json | jq -r '[.devices[][] | select(.name == "iPhone 16" and .isAvailable)][0].udid')
          xcrun simctl boot "$UDID" || true
          xcrun simctl bootstatus "$UDID" -b
          echo "SIMULATOR_UDID=$UDID" >> "$GITHUB_ENV"
      - name: Build and install the KeepSnappy build
        run: |
          npx expo prebuild --platform ios
          npx expo run:ios --configuration Release --device "$SIMULATOR_UDID"
      - name: Install the Appium iOS driver
        run: npx appium driver install xcuitest
      - name: Measure with KeepSnappy
        env:
          KEEPSNAPPY_KEY: ${{ secrets.KEEPSNAPPY_KEY }}
        run: >
          npx @keepsnappy/agent run --upload --wait
          --device "$SIMULATOR_UDID"
          --commit ${{ github.event.pull_request.head.sha || github.sha }}
```

- The test command comes from `flows.command` in `keepsnappy.yml`. Your suite reads `SIMULATOR_UDID` from the job's environment.
- A pull request gets its first real result once `main` has 3 sessions ([Getting started](https://keepsnappy.com/docs/getting-started.md#3-merge-then-measure-the-comparison-branch-3-times)).
- During the pilot, CI passes on a **Worse** ([why](https://keepsnappy.com/docs/ci.md#required-or-advisory-checks)).
- If your build and tests take longer than 90 minutes, raise `timeout-minutes` ([Budget the first session](https://keepsnappy.com/docs/ci.md#budget-the-first-session)).

## Known limits

- KeepSnappy measures what the app does, not Appium's own time. Each lookup is a round trip to the Appium server, and a lookup that searches the whole screen can take seconds. That makes the job longer. It doesn't change the measurement, as long as the waits between taps stay the same.
- A run whose command fails does not count. A run with no tap doesn't count either, because it has no screen to measure.

## Other setups

- iOS, on an iPhone on a cable: see [On your Mac](https://keepsnappy.com/docs/on-your-mac.md).
- Android, on an emulator in your CI: start with `keepsnappy init --detect`. If init cannot use your emulator job, follow the [manual Android setup](https://keepsnappy.com/docs/android.md#android-emulator).
- Android, on a phone on a cable: [Android](https://keepsnappy.com/docs/android.md#android-usb-phone) has the steps by hand, with `runner: command` around your Appium command. On the emulator and the phone alike, KeepSnappy puts the serial of the device it picked in `ANDROID_SERIAL`, so point your capabilities at that device. On Android the app sends its own measurements, so the build also needs the [SDK key](https://keepsnappy.com/docs/keys.md#sdk-key) and the API address.
- Real phones on BrowserStack, iOS or Android: these run through the [`farm`](https://keepsnappy.com/docs/glossary.md#farm) command, in place of `keepsnappy run`. `farm open` starts the session, your suite runs, and `farm close` ends it, while the app sends its own measurements, so its build must carry `EXPO_PUBLIC_KEEPSNAPPY_KEY` and `EXPO_PUBLIC_KEEPSNAPPY_INGEST_URL` ([SDK](https://keepsnappy.com/docs/sdk.md#the-build-switch)). Instead of your own suite, `farm` can run a journey module: a JavaScript file that uses `@keepsnappy/appium-driver` to say what to tap and what to wait for in each journey, while the driver runs the Appium session. See [BrowserStack](https://keepsnappy.com/docs/browserstack.md) and the [CLI reference](https://keepsnappy.com/docs/reference/cli.md#farm).

The iOS simulator setup on this page needs neither the SDK key nor the API address in the build.

## Journey module options

These options are for a journey module that `farm` runs with `@keepsnappy/appium-driver`. They don't apply to your own WebdriverIO suite. Take the driver version from the newest row of [Versions that work together](https://keepsnappy.com/docs/install.md#versions-that-work-together); older driver releases don't have them.

### Space repeated taps

Some controls ignore a tap that comes too soon after the last one, for example a button that throttles its taps. When a journey taps such a control quickly, some runs take the tap and some don't, so the same step does different work from run to run. `tapSpacingMs` keeps a minimum time, in milliseconds, between two touches on the same control. Set it above the time your control ignores taps for.

- The default is 0, which turns spacing off.
- A value must be a whole number from 0 to 2000. Any other value stops the run before the BrowserStack session opens.
- The same control means the same selector, or a point within 8 pixels of the last touch. Taps on other controls never wait.
- The driver waits only for the part of the time that is missing, so a journey that already waits long enough loses nothing.
- It applies to `t.tap`, `t.tapAt`, `tapAllAt` and the extra taps of `t.tapUntil`. Each wait is written to the driver's log, `driver-log.json`, as a `tap-spacing` entry.
- To set another value for one call, pass `tapSpacingMs` in the options of `t.tap`, `t.tapAt` or `tapAllAt`. 0 turns spacing off for that call.

```js
import { defineJourney, id } from '@keepsnappy/appium-driver';

export default defineJourney({
  app: { bundleId: 'com.example.app', scheme: 'example://' },
  home: id('home'),
  tapSpacingMs: 250,
  journeys: {
    react: async (t) => {
      await t.step('react.like', async () => {
        await t.tap(id('like'));
        await t.tap(id('like'));
        t.expectNext(id('like.count'));
      });
      // Another value for one call:
      await t.tap(id('close'), 10000, { tapSpacingMs: 0 });
    },
  },
});
```

Turning spacing on, or changing its value, changes what the journey measures. Do it in its own pull request.

### Scroll a list under a bar

`scrollCycles` scrolls a list to its end and back. Its strokes start inside the list's frame. When something that takes touches is drawn over part of the list, tell `scrollCycles` about it, so that no stroke lands on it:

- `overlay`: an element drawn over the list's bottom, such as a floating row of filter chips. Every stroke starts above it.
- `overlayTop`: an element drawn over the list's top, such as a tab bar under a see-through header that the list scrolls under. Every stroke starts below it.

The driver reads the element's position once, after it has stopped moving and before the first measured step. Neither option is set by default. If the bar sits above the list and not over it, you don't need `overlayTop`.

```js
import { id, scrollCycles } from '@keepsnappy/appium-driver';

await scrollCycles(t, {
  list: id('feed'),
  first: id('feed.first'),
  lastPage: id('feed.last'),
  any: id('feed.row'),
  pages: [],
  stepName: (toward, cycle) => `feed.${toward}.${cycle}`,
  overlayTop: id('feed.tabs'),
});
```

When the element given as `overlayTop` leaves no part of the list free, `scrollCycles` fails before its first stroke, and the run doesn't count. The same happens when it meets the `overlay` element. The error says that it leaves no part of the list to start a stroke on, and gives the positions. Check that `overlayTop` names the bar itself, not a larger element that covers the screen.

### Deep view trees on iOS

On iOS, WebDriverAgent reads the element tree to a depth of 50 by default. If a journey needs to find elements deeper in the tree, set `iosSnapshotMaxDepth` on its `defineJourney` options. Use a whole number from 1 to 62. The driver applies it on iOS before it calls the journey's `setup`. A deeper snapshot can slow down element lookups.

If Appium refuses the settings request with an explicit iOS depth, the driver stops the journey before setup. If Appium refuses the default settings request, the driver prints `driver settings not applied` with the setting names and the reason, then continues with Appium's settings.

[Troubleshooting](https://keepsnappy.com/docs/troubleshooting.md#appium) lists what you may see with Appium, and how to fix it.

All KeepSnappy docs for agents: https://keepsnappy.com/llms.txt
