Skip to content
On this page
[Integrations]

GitHub Actions

Run an end-to-end test suite from a workflow and fail the job unless every test passed. One step starts the run in xtest's Cloud browser through the public API, waits for it to be over, and prints the verdict. It needs curl and jq, which GitHub's Ubuntu runners already have, and no npm install and no browser on the runner.

There is no published action yet

xtest has no action on the GitHub Marketplace and none a workflow can name in a uses: line. Until there is one, the script below is how a workflow runs a suite.

The whole workflow

yaml
name: Tests
on: [push]

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

Save the script below as scripts/xtest-run.sh in the repository. It is the same script the CI/CD page gives GitLab, Jenkins and CircleCI.

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" ]

Create the key under Setup, xtest API keys. It is shown once and starts with xtest_live_.

What the script reads

XTEST_API_KEY

required

A platform API key from Setup, xtest API keys, made for the workspace the suite is in: a company's suite needs a key made for that company. Keep it in a repository secret.

SUITE_ID

required

The suite to run. Its id is in the Test library URL.

XTEST_TIMEOUT_MINUTES

optional

How long to wait for the suite to be over, every run of it included. 30 when it is not set. A replayed check waits up to 15 s for what it looks for, so raise it for a large suite.

XTEST_ALLOW_NOT_CHECKED

optional

true lets a run pass the step when its test agent marked steps not checked because the device cannot do them, such as one that needs someone speaking into the microphone. Unset, such a run fails it as incomplete.

What the step does

  • Starts one run of the suite and prints its id, or fails with the reason xtest gave.
  • Asks every five seconds until the run is over. Starting, running, a question waiting for an answer and a person in control are not over.
  • Prints the result and, for a run with a test agent, how many tests passed, the run's summary, each test the run skipped or that could not finish with why, and a link to the run, then fails the step unless every test passed.
  • A run takes at most 200 tests; a larger suite runs as several runs and the step waits for all of them. When a run is over with tests left, xtest starts a run of the rest that carries on from it and names it in nextRunId; the step waits for that run too, and fails unless every test passed and no test was left in no run.
  • 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 step fails.
  • 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 step waits for that run, and the cut run counts for its tests' results, not for how it ended. Only a stop you pressed, a run that wrote no result, or a rest xtest could not start ends a suite with tests left.
  • A step a test agent marked not checked, because the device cannot do it, is no pass: the step prints how many and fails as incomplete, unless XTEST_ALLOW_NOT_CHECKED is true.
  • Fails the step when the suite is not over after the time limit. The runs themselves go on in xtest.

Where it runs, and what it costs

Always the Cloud browser: runs on your own computer are started from the dashboard, where you can watch them, never from a key. A suite with saved steps replays them for no AI cost, and the AI only works out a case that has none, repairs a step that broke, or types a value read off the screen. Your monthly AI allowance and the limit of three AI runs at once apply here exactly as in the app, and the Cloud browser takes one AI or saved-step run at a time from each person: while a run of yours is going on it, started from the dashboard or with a key, a second is refused with 409 and DEVICE_BUSY. Run the jobs that call xtest one after another, not as a parallel matrix. While a bug bash of the suite is running on a test environment with a seed or a clean-up, a start is refused with 409 and BUG_BASH_EXPLORING until the bug bash ends, and the step fails with that reason.

xtest also starts a verify run by itself to check an issue one of your runs reported, and it runs as you. It makes way for yours: a run you start on the Cloud browser while one is going stops it and starts, and xtest verifies that issue again once the Cloud browser is free.

[Academy courses]