For AI agents: every page is available as Markdown by appending .md to its URL, and the full documentation index is at /llms.txt

Testing with e2e

How to run e2e end-to-end tests on iOS and Android with codemagic.yaml

e2e is an open source AI testing framework for web, iOS, and Android apps. Tests are TypeScript files. A test can find controls by test ID, like most UI test tools, or hand a step to an AI agent in plain language, such as agent.act('add one cold brew to the cart and place the order').

e2e is an npm package, so it runs on the Codemagic build machine itself, together with the simulator or emulator it drives. There is no server to host and no device farm to lease. The only thing an agent step needs is access to a model, and tests that use no agent steps need nothing at all. On Codemagic, you can run the same tests on every push and gate a release on the result.

This guide uses a Flutter app, but the steps that boot the device and run e2e are the same for any app you can build for a simulator or emulator. Only the dependency, build and app-path lines change. The complete sample, with tests, four workflows and a committed replay cache, is in the codemagic-e2e-flutter-sample repository.

Prerequisites

  1. An app that builds for the iOS Simulator and for an Android emulator.
  2. A Codemagic app with the repository connected.
  3. For Android, a Linux machine (instance_type: linux_x2), because the Android emulator needs hardware virtualization. Linux instances are only available for teams and users with billing enabled.
e2e needs Node.js 24, so set node: 24 in the workflow’s environment.

Adding e2e to the project

Create an e2e folder next to the app and install the packages:

mkdir e2e && cd e2e
npm init -y
npm install -D e2e @e2e-dev/mobile ai zod typescript

Add e2e/e2e.config.ts with one target per platform and the app’s bundle ID (iOS) or application ID (Android):

import type { E2EConfig } from 'e2e';
import { mobile } from '@e2e-dev/mobile';

// Use each target's own identifier: the iOS bundle ID and the Android
// application ID are often different strings for the same app.
const iosApp = { bundleId: 'com.example.myapp' };
const androidApp = { bundleId: 'com.example.myapp' };

export default {
  targets: [
    // CI passes the UDID of the simulator it booted.
    { name: 'ios', engine: mobile({ platform: 'ios', device: process.env.E2E_IOS_DEVICE }), app: iosApp },
    { name: 'android', engine: mobile({ platform: 'android' }), app: androidApp },
  ],
  workers: 1,
} satisfies E2EConfig;

Giving controls test IDs

e2e finds controls by role, name or test ID. In Flutter, a Semantics widget with an identifier becomes the test ID:

Semantics(
  identifier: 'add-cold-brew',
  child: ElevatedButton(onPressed: addColdBrew, child: const Text('Add')),
)

Writing a test

import { test } from '@e2e-dev/mobile';
import { expect } from 'e2e';

test('adds a drink to the cart', async ({ app, screen }) => {
  await app.open();
  await screen.getByTestId('add-cold-brew').tap();
  await expect(screen.getByTestId('cart-button')).toHaveText('view cart ยท 1');
});

The workflows

The sample’s codemagic.yaml defines each step once under definitions and reuses it with YAML anchors. The check workflows e2e-ios and e2e-android use them, and the release workflow later in this guide reuses the same steps, so the tests that gate a release are the ones that run on every push.

definitions:
  e2e_artifacts: &e2e_artifacts
    - e2e/.e2e/report.json
    - e2e/.e2e/junit.xml
    - e2e/.e2e/run.log
    - e2e/.e2e/results/**

  # The tests run in the same steps in the check and the release workflows, so
  # a release is only built from a revision that passed exactly these tests.
  steps:
    install_dependencies: &install_dependencies
      name: Install dependencies
      script: | 
        flutter pub get
        (cd e2e && npm ci)

    build_ios_simulator: &build_ios_simulator
      name: Build for the simulator
      # Flutter cannot build a Release app for the simulator, so this is Debug.
      script: flutter build ios --simulator --debug

    start_ios_simulator: &start_ios_simulator
      name: Start a simulator
      # Starts booting in the background while the app builds.
      script: | 
        set -euo pipefail
        xcrun simctl list runtimes
        UDID=$(xcrun simctl list devices available -j | python3 -c "
        import json,sys
        d=json.load(sys.stdin)['devices']
        for rt in sorted(d, reverse=True):
            for dev in d[rt]:
                if dev['name'].startswith('iPhone') and 'Pro' in dev['name'] and 'Max' not in dev['name']:
                    print(dev['udid']); sys.exit()
        ")
        test -n "$UDID"
        echo "Using simulator $UDID"
        xcrun simctl boot "$UDID" || true
        echo "E2E_IOS_DEVICE=$UDID" >> "$CM_ENV"

    install_ios_app: &install_ios_app
      name: Install the app
      script: | 
        set -euo pipefail
        xcrun simctl bootstatus "$E2E_IOS_DEVICE" -b
        xcrun simctl install "$E2E_IOS_DEVICE" build/ios/iphonesimulator/Runner.app

    run_e2e_ios: &run_e2e_ios
      name: Run e2e
      script: | 
        set -o pipefail
        cd e2e
        mkdir -p .e2e
        # Sample only: list your own test files here, or drop $FILES below to run the whole suite.
        FILES="tests/sign-in.e2e.ts tests/order.e2e.ts"
        # Sample only: the agent test runs with AI_GATEWAY_API_KEY or a committed recording for this
        # platform. For another model provider, check its key variable here instead.
        if [ -n "${AI_GATEWAY_API_KEY:-}" ] || grep -qs '"targetId": "ios"' .e2e/cache/*.json; then
          FILES="$FILES tests/agent.e2e.ts"
        fi
        npx e2e run --target ios --reporter list,junit $FILES 2>&1 | tee .e2e/run.log
      test_report: e2e/.e2e/junit.xml

    start_android_emulator: &start_android_emulator
      name: Start the emulator
      # Boots in the background while the APK builds.
      script: | 
        # The machine already runs an adb server that does not know this emulator.
        # Create an adb key and restart the server, or the device shows as "unauthorized".
        adb kill-server || true
        mkdir -p "$HOME/.android"
        [ -f "$HOME/.android/adbkey" ] || adb keygen "$HOME/.android/adbkey"
        adb start-server
        nohup emulator -avd emulator-35 -no-window -no-audio -no-boot-anim -no-snapshot -gpu swiftshader_indirect > "$HOME/emulator.log" 2>&1 &

    build_android_apk: &build_android_apk
      name: Build the APK
      script: flutter build apk --release --target-platform android-x64

    install_android_app: &install_android_app
      name: Install the app
      script: | 
        for i in $(seq 1 150); do
          [ "$(adb shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')" = "1" ] && break
          sleep 2
        done
        if [ "$(adb shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')" != "1" ]; then
          echo "Emulator did not boot. adb devices:"; adb devices
          echo "emulator.log:"; tail -n 60 "$HOME/emulator.log"
          exit 1
        fi
        sleep 10 # let the UI settle before the first test
        adb install -r build/app/outputs/flutter-apk/app-release.apk

    run_e2e_android: &run_e2e_android
      name: Run e2e
      script: | 
        set -o pipefail
        cd e2e
        mkdir -p .e2e
        # Sample only: list your own test files here, or drop $FILES below to run the whole suite.
        FILES="tests/sign-in.e2e.ts tests/order.e2e.ts"
        # Sample only: the agent test runs with AI_GATEWAY_API_KEY or a committed recording for this
        # platform. For another model provider, check its key variable here instead.
        if [ -n "${AI_GATEWAY_API_KEY:-}" ] || grep -qs '"targetId": "android"' .e2e/cache/*.json; then
          FILES="$FILES tests/agent.e2e.ts"
        fi
        npx e2e run --target android --reporter list,junit $FILES 2>&1 | tee .e2e/run.log
      test_report: e2e/.e2e/junit.xml

workflows:
  e2e-ios:
    name: e2e iOS
    max_build_duration: 60
    environment:
      groups:
        - e2e
      flutter: 3.44.6
      xcode: latest
      node: 24
    cache:
      cache_paths:
        - $HOME/.pub-cache
        - $HOME/.agent-device/apple-runner/derived
    triggering:
      events:
        - push
        - pull_request
    scripts:
      - *start_ios_simulator
      - *install_dependencies
      - *build_ios_simulator
      - *install_ios_app
      - *run_e2e_ios
    artifacts: *e2e_artifacts

  e2e-android:
    name: e2e Android
    # The emulator needs a Linux machine (hardware virtualization).
    instance_type: linux_x2
    max_build_duration: 60
    environment:
      groups:
        - e2e
      flutter: 3.44.6
      node: 24
    cache:
      cache_paths:
        - $HOME/.pub-cache
        - $HOME/.gradle/caches
    triggering:
      events:
        - push
        - pull_request
    scripts:
      - *start_android_emulator
      - *install_dependencies
      - *build_android_apk
      - *install_android_app
      - *run_e2e_android
    artifacts: *e2e_artifacts

iOS

The iOS workflow starts booting a simulator, builds the app while it boots, installs the app and runs npx e2e run. The workflow sets no instance_type, so it runs on your team’s default Mac machine. To choose one, add for example instance_type: mac_mini_m2 or instance_type: mac_mini_m4 to the workflow. See instance type for the machines and which of them need billing.

Some details worth knowing:

  • A simulator build needs no code signing. See iOS simulator builds for native apps. Flutter cannot build a Release app for the simulator, so the build step uses --debug.
  • The simulator boots in the first step, so it is ready by the time the build finishes.
  • The first run builds a small XCUITest runner that e2e uses to drive the simulator. Caching $HOME/.agent-device/apple-runner/derived skips that step on later runs.
  • Writing E2E_IOS_DEVICE to $CM_ENV makes the simulator’s UDID available to the next steps, and e2e.config.ts reads it.
  • With CI set, which Codemagic does, e2e retries a failed test once and reads the replay cache without writing to it.
Xcode 27. Xcode 27 needs an iOS deployment target of 15.0 or later, and it lists the simulator Flutter framework as x86_64 arm64 while Flutter 3.44 expects arm64 x86_64, which fails the build. This is a one-time fix in the Flutter project, not something to change in the workflow: raise IPHONEOS_DEPLOYMENT_TARGET to 15.0 and add ARCHS[sdk=iphonesimulator*] = arm64 to ios/Flutter/Debug.xcconfig (the second setting suits Apple Silicon Macs only). The sample repository already has both, so its xcode: latest workflow builds as shown.

Android

The Android workflow starts an emulator in the background while the app builds, then installs the APK and runs the tests. The Linux image includes the emulator-35 AVD (see Ubuntu 24.04), but no emulator is running when the build starts.

Build the APK for android-x64, because the emulator on a Linux instance is x86_64. The first step creates an adb key and restarts the adb server, because an adb server is already running on the machine and ignores a key created later. The install step waits at most five minutes for the emulator to boot and prints adb devices and the emulator log if it never does, which turns a stuck build into a clear error. The short pause before the install lets the UI settle, so the first test does not run against a screen that refuses input.

In the sample, the run step lists its two test files and decides whether to run the agent test. Replace the file list with your own test files, or drop $FILES to run the whole suite. The agent test runs when AI_GATEWAY_API_KEY is set or a committed recording exists for the platform. If you use another model provider, check its key variable there instead.

Test results

The test_report field sends e2e’s JUnit report to the Tests tab of the build. The artifacts entries keep the JSON report, the log and a trace page for every test. A trace lists each step with the screen changes it caused, which makes a failing test easy to read.

Agent steps and the model key

An agent.act step asks a model to work through the app:

await agent.act('add one cold brew to the cart, open the cart and place the order');

// A locator check that confirms the step, so the recording below is saved.
await expect(screen.getByTestId('order-confirmation')).toBeVisible();

Add the model’s key to the e2e variable group, marked as secure, and list the group in the workflow as above. The sample reads AI_GATEWAY_API_KEY from the Vercel AI Gateway. e2e works with any AI SDK provider, so you can also use a key from OpenAI, Anthropic or OpenRouter.

Replaying agent steps without a model

Once a later check confirms the outcome, e2e records what an agent step did, and later runs replay it without calling a model. The recordings are plain JSON files in e2e/.e2e/cache by default. Commit them, and Codemagic replays them on every build. In CI, e2e only reads the cache and never writes to it. If the app changes so that a recording no longer matches, the agent takes over from the current screen, which needs a key and calls the model. Add --strict-cache to npx e2e run to fail on a stale recording instead.

To make or refresh a recording, run the test with E2E_RECORD_CACHE=1 on your own machine or in a workflow you start by hand, and commit the e2e/.e2e/cache folder it produces. Review the files before you commit them, as you would any test data.

Releasing only what passed

Run the same test steps in a release workflow, placed before the build that you publish. A failing test stops the build, so nothing is signed or uploaded.

You upload the certificate and provisioning profile once under Team settings > Code signing identities, add your App Store Connect API key under Developer Portal, and refer to them from the workflow. Codemagic fetches the files by distribution type and bundle identifier, so no keychain or certificate scripts are needed. See Signing iOS apps.

This is the sample’s release-ios workflow with its signing and publishing blocks switched on. It reuses the steps defined above.

workflows:
  release-ios:
    name: Release iOS
    max_build_duration: 90
    environment:
      groups:
        - e2e
      flutter: 3.44.6
      xcode: latest
      node: 24
      # A certificate and profile you uploaded once under Team settings >
      # Code signing identities. Codemagic fetches them by distribution type
      # and bundle id, so no keychain scripts are needed.
      ios_signing:
        distribution_type: app_store
        bundle_identifier: com.example.myapp # use your own
    # The App Store Connect API key you added under Team settings > Developer Portal.
    integrations:
      app_store_connect: YOUR_APP_STORE_CONNECT_KEY_NAME
    cache:
      cache_paths:
        - $HOME/.pub-cache
        - $HOME/.agent-device/apple-runner/derived
    triggering:
      events:
        - tag
      tag_patterns:
        - pattern: 'v*'
          include: true
    scripts:
      # The gate: if any e2e test fails, the build stops here and nothing below runs.
      - *start_ios_simulator
      - *install_dependencies
      - *build_ios_simulator
      - *install_ios_app
      - *run_e2e_ios
      - name: Apply the provisioning profile
        script: xcode-project use-profiles
      - name: Build the signed IPA
        script: | 
          flutter build ipa --release \
            --build-number="$BUILD_NUMBER" \
            --export-options-plist=/Users/builder/export_options.plist
    artifacts:
      - build/ios/ipa/*.ipa
      - e2e/.e2e/report.json
      - e2e/.e2e/junit.xml
      - e2e/.e2e/run.log
      - e2e/.e2e/results/**
    # Needs the signed IPA above, and an app record created by hand in App Store Connect.
    publishing:
      app_store_connect:
        auth: integration
        submit_to_testflight: false

Only let people you trust push v* tags, because a tag starts a build that holds your signing and publishing credentials.

Android works the same way. Upload your keystore under Code signing identities, reference it with android_signing and build the bundle. Codemagic sets the keystore variables that your Gradle signing config reads. See Signing Android apps.

The sample repository ships these settings as commented blocks, so it runs without any accounts. Switch them on once your app is registered with App Store Connect or Google Play.

Using a coding agent

The sample repository includes two skills for coding agents such as Claude Code, in its skills folder. One adds e2e and the Codemagic workflows to your app. The other starts a build through the Codemagic REST API, waits for it, finds the failing step and reads its log, so the agent can fix errors until the workflows pass. Copy the folders into your project’s .claude/skills folder. The build skill needs an API token in CM_API_TOKEN and the app’s id in CM_APP_ID.

Troubleshooting

The emulator shows as unauthorized. An adb server is already running on the machine and ignores a key created later. Create the key with adb keygen and restart the server with adb kill-server and adb start-server before starting the emulator, as in the workflow above.

The Android build fails with a Gradle version error. With flutter: stable, a project can fall behind the Gradle version the newest Flutter requires. Pin the Flutter version in environment. The samples use flutter: 3.44.6.

An agent step runs live instead of replaying. The recording belongs to one test, instruction, target and app identity. Renaming the test, rewording the instruction or changing the bundle ID makes it miss. Record it again. To see why a step missed, open the trace page from the results artifact.

No tests appear on the Tests tab. Check that test_report points at e2e/.e2e/junit.xml and that the --reporter list includes junit.