Skip to content
On this page
[Integrations]

CI/CD integration

A pipeline starts an xtest suite with one HTTP call, waits for it, and fails the build unless every test passed. A run takes at most 200 tests; a larger suite runs as several runs, each carrying on from the last, and the script waits for all of them. If a deploy of xtest cuts a run, a run reaches six hours, or the Cloud browser stops part way, xtest starts the rest itself and the script waits for it. Nothing is installed on the build machine: the browser runs on our side.

The script every platform calls

Save this as scripts/xtest-run.sh. It needs bash, curl and jq, an API key from Setup, xtest API keys in XTEST_API_KEY, and the id of the suite to run in SUITE_ID. It waits up to 30 minutes for the suite to be over, every run of it included; set XTEST_TIMEOUT_MINUTES for a longer suite. When it stops waiting, the build fails and the runs go on in xtest.

bash
#!/usr/bin/env bash
set -euo pipefail

XTEST="https://xtest.io/api/v1"
TIMEOUT_MINUTES="${XTEST_TIMEOUT_MINUTES:-30}"

# The worse of two verdicts: failed, then error, blocked, stopped, interrupted.
worse() {
  for v in failed error blocked stopped interrupted; do
    if [ "$1" = "$v" ] || [ "$2" = "$v" ]; then echo "$v"; return; fi
  done
  if [ "$1" = "passed" ]; then echo "$2"; else echo "$1"; fi
}

# 1. Start the run. The answer is 202 with the run id, or an error saying why not.
#    It answers a note as well when a suite has more than 200 tests (a run takes
#    at most 200, and xtest runs the rest as further runs that carry on from it),
#    and when the run leaves out cases a test agent added, which wait for a
#    person to press Looks good on the test's page: the note says how many, and
#    the build does not fail for them.
#    The start may first wait up to 40 seconds for a verify run on the same
#    device to give it back, so it gets 90 seconds; each poll gets 30.
START=$(curl -sS --max-time 90 -X POST "$XTEST/suites/$SUITE_ID/runs" \
  -H "X-API-Key: $XTEST_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"ai":"repair"}')
RUN=$(echo "$START" | jq -r '.runId // empty')
if [ -z "$RUN" ]; then
  echo "xtest did not start the run: $(echo "$START" | jq -r '.error // .')" >&2
  exit 1
fi
echo "xtest run $RUN"
echo "$START" | jq -r '.note // empty'

# 2. Poll until the run is over, then until every run that carries on from it
#    is. The answer is { "run": { ... } }. A run with a test agent has "ai": true
#    and is over when "finished" is true; queued, running, waiting and paused are
#    not over. A suite with a Playwright script is over when its status is no
#    longer "running". A 429, a 5xx or no answer at all (curl's 000) is asked
#    again until the deadline; any other answer with no run in it stops with
#    xtest's reason.
DEADLINE=$(( $(date +%s) + TIMEOUT_MINUTES * 60 ))
LAST=$(echo "$START" | jq -r .status)
VERDICT=passed
RUNS=1
SEEN=""
WAITED=0
UNFINISHED=0
NOT_CHECKED=0
while :; do
  ANSWER=$(curl -sS --max-time 30 -w '\n%{http_code}' "$XTEST/runs/$RUN" -H "X-API-Key: $XTEST_API_KEY" || true)
  CODE=${ANSWER##*$'\n'}
  BODY=${ANSWER%$'\n'*}
  case "$CODE" in
    000|429|5??) echo "xtest could not answer for run $RUN (HTTP $CODE). Asking again." >&2 ;;
    *)
      if ! echo "$BODY" | jq -e .run > /dev/null; then
        echo "xtest could not read run $RUN: $(echo "$BODY" | jq -r '.error // .')" >&2
        exit 1
      fi
      LAST=$(echo "$BODY" | jq -r .run.status)
      OVER=$(echo "$BODY" | jq -r '.run | if .ai then .finished else .status != "running" end')
      if [ "$OVER" = "true" ]; then
        # 3. Print this run's verdict, its summary and where to see it, once, and
        #    each test it skipped or that could not finish, with why: neither is a
        #    pass, so either fails the build (step 5). So does a step its test agent
        #    marked not checked (notChecked), one the device cannot do, unless
        #    XTEST_ALLOW_NOT_CHECKED is true.
        SUMMARY=$(echo "$BODY" | jq -r '.run.summary // empty')
        if [ "$SEEN" != "$RUN" ]; then
          SEEN=$RUN
          SAID=$SUMMARY
          echo "$SUMMARY"
          echo "$BODY" | jq -r '.run.tests[]? | select(.status == "skipped" or .status == "blocked")
            | "  " + .statusWord + ": " + .title + (if .summary then "  <- " + .summary else "" end)'
          UNFINISHED=$((UNFINISHED + $(echo "$BODY" | jq '[.run.tests // [] | group_by(.testCaseId)[] | select(any(.status == "skipped" or .status == "blocked"))] | length')))
          NOT_CHECKED=$((NOT_CHECKED + $(echo "$BODY" | jq '.run.notChecked // 0')))
          echo "$BODY" | jq -r '.run | if .ai
            then (.repeats // 1) as $times | (.tests // [] | group_by(.testCaseId)) as $tests
              | "\(.statusWord): \([$tests[] | select(length >= $times and all(.status == "passed"))] | length)/\(.testsTotal // ($tests | length)) tests passed\(if $times > 1 then " every time" else "" end). \(.link)"
            else "\(.status): \(.passedSteps)/\(.totalSteps) steps passed" end'
        elif [ "$SUMMARY" != "$SAID" ]; then
          # Its summary grew while the rest was waited for: the refusal for good is
          # written on it minutes after the run ended. Print what was added.
          echo "${SUMMARY#"$SAID"}" | sed 's/^ //'
          SAID=$SUMMARY
        fi
        # 4. A run takes at most 200 tests. When this one left some (testsLeft), xtest
        #    starts a run of the rest that carries on from it and names it in nextRunId:
        #    wait for that run and poll it too. xtest starts the rest after a verdict,
        #    blocked or an error with a result written, after the six-hour stop, and
        #    after its own restart cut the run, which the next server starts. After a
        #    person's Stop, or a run that wrote no result, nobody starts the rest, and
        #    the tests left fail the build.
        LEFT=$(echo "$BODY" | jq -r '.run.testsLeft // 0')
        NEXT=$(echo "$BODY" | jq -r '.run.nextRunId // empty')
        if [ -n "$NEXT" ]; then
          echo "xtest run $NEXT carries on from $RUN"
          # xtest carried this run on, so its own ending is not the verdict, whatever
          # it was (its browser stopped, xtest restarted, the six-hour stop): the test
          # it was cut at runs in the runs after it. The results it wrote are, a test
          # it skipped included.
          TESTS=$(echo "$BODY" | jq -r '[.run.tests[]?.status] | if any(. == "failed") then "failed" elif any(. == "error") then "error" elif any(. == "blocked") then "blocked" elif any(. == "skipped") then "incomplete" else "passed" end')
          VERDICT=$(worse "$VERDICT" "$TESTS")
          RUN=$NEXT
          RUNS=$((RUNS + 1))
          WAITED=0
        elif [ "$LEFT" -gt 0 ] && [ "$WAITED" -lt 48 ] \
          && echo "$BODY" | jq -e '.run
            | (((.status | IN("passed","failed","blocked","error")) or (.status == "stopped" and .error == "This run reached six hours and stopped. Run the remaining tests to carry on."))
                and any(.tests[]?; .testRunId))
              or (.status == "interrupted" and .error == "xtest restarted while this run was in progress. Run it again.")' > /dev/null; then
          WAITED=$((WAITED + 1))
        else
          # The last run's own ending is the suite's, and a test it skipped, or one
          # that could not finish, is not a pass whatever it ended as.
          VERDICT=$(worse "$VERDICT" "$LAST")
          if [ "$VERDICT" = "passed" ] && [ "$UNFINISHED" -gt 0 ]; then VERDICT=incomplete; fi
          if [ "$VERDICT" = "passed" ] && [ "$NOT_CHECKED" -gt 0 ] && [ "${XTEST_ALLOW_NOT_CHECKED:-}" != "true" ]; then VERDICT=incomplete; fi
          if [ "$LEFT" -gt 0 ]; then
            echo "$LEFT tests of the suite were in no run." >&2
            if [ "$VERDICT" = "passed" ]; then VERDICT=incomplete; fi
          fi
          break
        fi
      fi ;;
  esac
  if [ "$(date +%s)" -ge "$DEADLINE" ]; then
    echo "xtest run $RUN was not over after $TIMEOUT_MINUTES minutes ($LAST). It goes on in xtest." >&2
    exit 1
  fi
  sleep 5
done

# 5. Fail the build on anything but a pass of every test, with every test in a run.
if [ "$RUNS" -gt 1 ]; then echo "Suite: $VERDICT across $RUNS runs"; fi
if [ "$UNFINISHED" -eq 1 ]; then echo "1 test was skipped or could not finish, so not every test passed." >&2; fi
if [ "$UNFINISHED" -gt 1 ]; then echo "$UNFINISHED tests were skipped or could not finish, so not every test passed." >&2; fi
if [ "$NOT_CHECKED" -gt 0 ] && [ "${XTEST_ALLOW_NOT_CHECKED:-}" != "true" ]; then
  echo "Steps not checked: $NOT_CHECKED, so not every step passed. Set XTEST_ALLOW_NOT_CHECKED=true to let such a run pass." >&2
fi
[ "$VERDICT" = "passed" ]

Wiring it up

yaml
name: xtest
on:
  pull_request:
    branches: [ main ]

jobs:
  e2e:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Run the suite on xtest
        env:
          XTEST_API_KEY: ${{ secrets.XTEST_API_KEY }}
          SUITE_ID: ${{ vars.XTEST_SUITE_ID }}
        run: bash scripts/xtest-run.sh

Worth knowing

Cost

A replay makes no model call. Only a run where the AI has to work something out, or a step that needs a value read off the screen, spends your Claude budget, so a pipeline on every pull request stays cheap.

ai: "off"

Replay the saved steps and nothing else. A test with no saved steps, one whose steps were edited after they were saved, or one whose saved steps stop part way is skipped; one whose saved step types a value read off the screen replays up to that step and stops there, as one that could not finish; a test stops at the first step that no longer fits and is marked as one that could not finish; and a test whose saved check fails is still marked failed. The script prints each test that was skipped or could not finish, with why, and fails the build on every one of them, which is the strict setting for a build gate. A suite whose own strict mode is on runs this way from a key, whatever the body says.

Device

Runs started with an API key go to the Cloud browser. A run on one of your own computers has to start from the dashboard.

Workspace

A key sees only the workspace it was made for. To run a company's suites, make the key for that company; a key for your personal workspace is refused if it names the company in X-Organization-Id, rather than finding nothing.

Evidence

The run page holds every step and whether it passed, and the AI summary, and the screenshots a test agent took are kept with it for 30 days. Put the link in the build log and nobody has to reproduce a failure by hand.

200 tests

A run takes at most 200 tests. A larger suite runs as several runs that carry on from one another: the start answers a note saying so, xtest starts each next run itself when the one before it has ended, the ended run names it in nextRunId, and testsLeft counts the tests no run has taken yet. The script waits for all of them and fails the build unless every test passed and no test was left in no run.

Added cases

A case a test agent added waits for a person to press Looks good on the test's page; until then a run started with a key leaves it out, the start's note says how many, and the build does not fail for it. If every case of the test waits, the start is refused with xtest's reason and the build fails.

Steps not checked

A test agent marks a step not checked when the device cannot do it, such as one that needs someone speaking into the microphone, and the run says how many in notChecked. The script prints how many and fails the build as incomplete, unless XTEST_ALLOW_NOT_CHECKED is true.

A cut run

A deploy of xtest ends every run in flight as interrupted, a run stops when it reaches six hours, and a Cloud browser can stop part way through a run. None of them is your suite's doing: xtest starts the rest itself, the cut run names it in nextRunId, and the script waits for that run instead of failing the build. A run that names the next one counts for its tests' results, not for how it ended, so a suite whose every test passed passes. Only a stop you pressed, a run that wrote no result, or a rest xtest could not start ends a suite with tests left.

[Academy courses]