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

# Maestro

> What changes for your Maestro flows when you add KeepSnappy.

This page is for teams whose end-to-end tests are Maestro flows. It covers only what is different with Maestro. [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 Maestro, each flow file is one journey. KeepSnappy runs each flow on its own, 5 times per session by default. It compares each flow only with the same flow on `main`.

## Install

The CI machine needs Maestro on its `PATH`. Init copies the job that runs your tests today, so a job that already installs Maestro keeps doing so. If yours doesn't, copy the `Install Maestro` step of the [CI example](#ci-example).

Maestro also needs the Java runtime that its own [install guide](https://maestro.mobile.dev) names.

## Settings in keepsnappy.yml

Init writes `verdict_gate`, `app.bundle_id`, `flows.runner` and `flows.paths`. 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: maestro
  paths:
    - e2e/flows/*.yaml
  tags: [perf]
  exclude_tags: [flaky]
  maestro_version: '1.39.13'
timeout_seconds: 600
```

- `flows.paths`: flow files, folders or globs. A folder means the flow files directly in it, without its `config.yaml`.
- `flows.tags` and `flows.exclude_tags`: a flow runs when it has one of `tags` and none of `exclude_tags`.
- `flows.maestro_version`: KeepSnappy writes it to the log, so you can see when Maestro changed. It doesn't install or pick a version. Pin Maestro in CI yourself, because a new Maestro version changes how long its commands take.
- `device`: the simulator's id (its UDID, from `xcrun simctl list devices booted`), or `booted`. If several simulators are booted and none is named, the run stops and lists them. 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 one flow may take.

Only a flow file with a top-level `appId:` in its header runs as a journey. Helper flows without one, which you call with `runFlow`, are skipped. A helper with its own `appId:` runs as a journey too. To keep it out, tag your real flows and set `flows.tags`, or list the files.

A flow's `name:` is its title on the dashboard. The first paragraph of the comment at the top of the file is its description. [Journeys](https://keepsnappy.com/docs/journeys.md#maestro-flows) shows how to set both in `keepsnappy.yml` instead.

### Variables your flows read

Maestro passes a flow only the environment variables whose names start with `MAESTRO_`. With `runner: maestro`, KeepSnappy runs each flow with no `-e` option. So a flow that reads `${APP_ID}` in its `appId:` line gets no value.

Rename the variable in your flows to one that starts with `MAESTRO_`, such as `${MAESTRO_APP_ID}`. Then set `MAESTRO_APP_ID` in the job's environment.

If you run your own Maestro command (see the next section), you can add Maestro's `-e` option to it instead. In `keepsnappy run … -- <command>`, the part after `--` is the test command that KeepSnappy runs:

```sh
npx --no-install keepsnappy run --upload --wait -- maestro test -e APP_ID=com.example.app .maestro
```

## One journey per flow, or one for the suite

Each flow gets its own result when KeepSnappy runs the flows itself. That is `runner: maestro`, with no command after `--` in the workflow. Init sets it up this way when your job runs `maestro test` on the flows that `keepsnappy.yml` lists, with no options of its own.

Your whole suite is one journey, named `e2e`, in two cases:

- Your `maestro` line has options of its own. Init keeps it after `--` in the workflow and says that the suite is one journey.
- Your flows are in a Maestro workspace. Init keeps your Maestro command, so that Maestro applies the workspace's selection and order. A new `keepsnappy.yml` stores the same command (`flows.runner` is `command`, and `flows.command` runs Maestro on the workspace folder).

A workspace is a `config.yml` or `config.yaml` with `flows:`, `includeTags:`, `excludeTags:` or `executionOrder:`, in the flow folder or a folder above it.

Before measuring the workspace, check which flows it selects and in what order. Flows that sign up, post, delete content or change account settings can leave different data for the next run. For your first baseline, select a flow that can repeat on fixed data, or reset what it changes before each run. See [Use the same data every run](https://keepsnappy.com/docs/journeys.md#use-the-same-data-every-run).

To get one journey per flow from a workspace:

1. In the workflow, delete the `--` and the command after it.
2. In `keepsnappy.yml`, set `flows.runner` to `maestro`, and delete `flows.command`.
3. List the flow files in `flows.paths`, or a folder that holds them directly.

Each flow then runs on its own, without the workspace's selection and order. If `flows.paths` lists the workspace folder itself, `keepsnappy doctor` reports it and says what to use instead. To read init's plan before it changes anything, run it with `--detect`.

## Journeys that measure the same way

KeepSnappy compares a flow with itself across commits, so the flow 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 Maestro:

- Use the same data every run ([why and how](https://keepsnappy.com/docs/journeys.md#use-the-same-data-every-run)). To tell the app it is being measured, for example to use a mock server or a fixed date, pass a launch argument: `launchApp` with `arguments:`. Don't set `clearState` to `true`. It also clears what KeepSnappy keeps in the app's data, and the run can be lost.
- Start with `launchApp`, so that every run starts from the home screen. Reset test data before the flow, not inside it.
- Wait on a condition. `extendedWaitUntil` with a `timeout` waits for an element to show or go away. It fails the flow when that doesn't happen.
- Name elements by `id`, which is the element's `testID`. Use `text` only for content that has no other name.
- Scroll with `swipe` inside a `repeat`, with a fixed start, end and `duration`. Don't use `scrollUntilVisible` for a measured scroll, because the number of gestures it sends depends on what is on screen.
- End on the home screen, with the last screen closed.

```yaml
# Opens the chat list, opens one chat, scrolls it, and goes back home.
appId: com.example.app
name: Scroll a chat
tags:
  - perf
---
- launchApp
- tapOn:
    id: open-chat
- extendedWaitUntil:
    visible:
      id: message-list
    timeout: 10000
- repeat:
    times: 3
    commands:
      - swipe:
          start: 50%, 80%
          end: 50%, 20%
          duration: 400
- tapOn:
    id: back
- extendedWaitUntil:
    visible:
      id: home
    timeout: 10000
```

[Step markers](https://keepsnappy.com/docs/glossary.md#step-marker) are optional, and results are correct without them. A step marker names a part of a journey, so the dashboard shows that part as a test step of its own (here `chat.open`). To name a step, copy `node_modules/@keepsnappy/agent/maestro/keepsnappy/` into the folder of your flows. Then call its `step.yaml` at the start and at the end of the step.

```yaml
appId: com.example.app
name: Open a chat with step markers
---
- launchApp
- runFlow:
    file: keepsnappy/step.yaml
    env:
      STEP: chat.open
      PHASE: start
- tapOn:
    id: open-chat
- extendedWaitUntil:
    visible:
      id: message-list
    timeout: 10000
- runFlow:
    file: keepsnappy/step.yaml
    env:
      STEP: chat.open
      PHASE: end
```

Keep file names stable. A renamed flow is a new journey, with no history on `main`. Change a flow in its own pull request: a pull request that edits a flow is compared with a `main` that ran the old flow.

## 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 Maestro and runs `keepsnappy run`.

```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 Maestro
        run: |
          curl -fsSL "https://get.maestro.mobile.dev" | bash
          echo "$HOME/.maestro/bin" >> "$GITHUB_PATH"
      - 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 KeepSnappy step has no command after `--`. It runs the flows that `keepsnappy.yml` lists.
- 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 flows 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. The time Maestro spends finding an element, or waiting between commands, is not part of it.
- KeepSnappy starts Maestro once per flow file in each run. Each flow starts from its own `launchApp`.
- A run whose flow fails does not count. A run with no tap doesn't count either, because it has no screen to measure.
- Maestro finds simulators only in Xcode's default device set. With a custom set (`--device-set`, `ios.device_set` or `SIMULATOR_DEVICE_SET_PATH`), KeepSnappy stops a run that starts `maestro`. Use a simulator in the default set, or another test tool ([A custom simulator set](https://keepsnappy.com/docs/settings.md#a-custom-simulator-set)).

## 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. On the emulator and the phone alike, KeepSnappy passes the device it picked to `maestro` with `--device`, so leave `--device` out of your command.
- Real phones on BrowserStack, iOS or Android: these run through the [`farm`](https://keepsnappy.com/docs/glossary.md#farm) command, which runs a session on real phones in place of `keepsnappy run`. See [BrowserStack](https://keepsnappy.com/docs/browserstack.md) and the [CLI reference](https://keepsnappy.com/docs/reference/cli.md#farm).

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

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