On this page
Jenkins
There is no xtest plugin for Jenkins. There does not need to be: a stage that calls the public REST API starts an end-to-end suite in the Cloud browser, waits for it, and fails the build unless every test passed. The Jenkins agent needs curl and jq, nothing else, and no browser is installed on it.
A declarative pipeline
pipeline {
agent any
environment {
XTEST_API_KEY = credentials('xtest-api-key')
SUITE_ID = 'cmta7w9po0003rc5wd5jf4jyr'
XTEST = 'https://xtest.io/api/v1'
}
stages {
stage('End-to-end tests') {
steps {
sh '''
set -eu
# Start the run. The answer is 202 with the run id, or xtest's reason
# why not, such as a 402 when the workspace has no plan or a 409
# while the cloud browser is busy: the stage prints it and fails.
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"}' || true)
run=$(echo "$start" | jq -r '.runId // empty' 2>/dev/null || true)
if [ -z "$run" ]; then
reason=$(echo "$start" | jq -r '.error // empty' 2>/dev/null || echo "$start" | head -n 1)
echo "xtest did not start the run: ${reason:-it did not answer}" >&2
exit 1
fi
echo "xtest run $run"
echo "$start" | jq -r '.note // empty'
# Read the run into body. A 429, a 5xx or no answer at all is asked
# again; any other answer without a run in it is xtest's reason, and
# the stage prints it and fails.
read_run() {
answer=$(curl -sS --max-time 30 -w '\n%{http_code}' "$XTEST/runs/$run" -H "X-API-Key: $XTEST_API_KEY" || true)
code=$(printf '%s\n' "$answer" | tail -n 1)
case "$code" in
000|429|5??) echo "xtest could not answer for run $run (HTTP $code). Asking again." >&2; return 1 ;;
esac
body=$(printf '%s\n' "$answer" | sed '$d')
if ! echo "$body" | jq -e .run > /dev/null 2>&1; then
echo "xtest could not read run $run: $(echo "$body" | jq -r '.error // empty' 2>/dev/null || echo "$body" | head -n 1)" >&2
exit 1
fi
}
# A run takes at most 200 tests. A larger suite runs as several runs
# that carry on from one another: xtest starts each next run itself,
# the ended run names it in nextRunId, and testsLeft counts the
# tests no run has taken yet. Every run is waited for. xtest also
# starts the rest after its own restart cut a run, or a run reached
# six hours, both told by the run's sentence. A run it carried on
# counts for its tests, not its ending. Only a stop you pressed,
# a run that wrote no result, or a rest xtest could not start
# ends a suite early. A test a run skipped, or one that could not
# finish, is not a pass: each is printed with why, and fails it.
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
}
verdict=passed
unfinished=0
# A step its test agent marked not checked (notChecked), one the device
# cannot do, is not a pass either, unless XTEST_ALLOW_NOT_CHECKED is true.
unchecked=0
while :; do
status=queued
deadline=$(( $(date +%s) + 60 * ${XTEST_TIMEOUT_MINUTES:-10} ))
while :; do
if read_run; then
status=$(echo "$body" | jq -r '.run.status')
case "$status" in
queued|running|waiting|paused) ;;
*) break ;;
esac
fi
if [ "$(date +%s)" -ge "$deadline" ]; then
echo "xtest run $run was not over after ${XTEST_TIMEOUT_MINUTES:-10} minutes ($status). It goes on in xtest." >&2
exit 1
fi
sleep 5
done
echo "Run $run: $status"
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')))
unchecked=$((unchecked + $(echo "$body" | jq '.run.notChecked // 0')))
left=$(echo "$body" | jq -r '.run.testsLeft // 0')
# A run that already names the next one was carried on: follow it, even when
# the next run has taken every test that was left (testsLeft is then 0).
next=$(echo "$body" | jq -r '.run.nextRunId // empty')
if [ -z "$next" ] && [ "$left" -eq 0 ]; then
verdict=$(worse "$verdict" "$status")
break
fi
if [ -z "$next" ] && ! 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.")
or (.status == "interrupted" and .error == "xtest restarted while this run was in progress. Run it again.")' > /dev/null; then
verdict=$(worse "$verdict" "$status")
break
fi
for _ in $(seq 48); do
next=$(echo "$body" | jq -r '.run.nextRunId // empty')
if [ -n "$next" ]; then break; fi
sleep 5
read_run || true
done
if [ -z "$next" ]; then
echo "$left tests of the suite were in no run"
verdict=$(worse "$verdict" "$status")
if [ "$verdict" = "passed" ]; then verdict=incomplete; fi
break
fi
# Carried on: its ending is not the verdict, the results it wrote are.
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
done
if [ "$unfinished" -eq 1 ]; then echo "1 test was skipped or could not finish, so not every test passed"; fi
if [ "$unfinished" -gt 1 ]; then echo "$unfinished tests were skipped or could not finish, so not every test passed"; fi
if [ "$unfinished" -gt 0 ] && [ "$verdict" = "passed" ]; then verdict=incomplete; fi
if [ "$unchecked" -gt 0 ] && [ "${XTEST_ALLOW_NOT_CHECKED:-}" != "true" ]; then
echo "Steps not checked: $unchecked, so not every step passed. Set XTEST_ALLOW_NOT_CHECKED=true to let such a run pass"
if [ "$verdict" = "passed" ]; then verdict=incomplete; fi
fi
echo "Result: $verdict"
test "$verdict" = "passed"
'''
}
}
}
}Store the key as a secret text credential called xtest-api-key. Keys are made under Setup, xtest API keys, shown once, and scoped to the account that owns the suite.
Reading the verdict
A run is over when its status is none of queued, running, waiting or paused. The final words are passed, failed, done, stopped, interrupted, “couldn't finish” as blocked, and error. The loop above waits ten minutes for each run, or as many minutes as an XTEST_TIMEOUT_MINUTES in the environment block says, which is long enough for most suites and short enough that a stuck build does not hold an executor all afternoon.
While it waits, a 429, a 5xx or no answer at all is asked again. Any other answer without a run in it fails the build with xtest's own reason, and so does a start xtest refused, such as a 402 when the workspace has no plan or a 409 while the Cloud browser is busy.
Two endings are xtest's doing and not your suite's: interrupted, when a deploy of xtest cut the run, and stopped with the sentence that the run reached six hours. After either, and after a run that ended blocked or in an error with a result written, such as a Cloud browser that stopped part way, xtest starts the rest itself and the stage waits for that run. A run that names the next one in nextRunId counts for its tests' results, not for how it ended: only the last run's ending is the suite's.
A run takes at most 200 tests; a larger suite runs as several runs and the stage waits for all of them. When a run is over with tests left, testsLeft says how many, xtest starts a run of the rest that carries on from it, and the ended run names that run in nextRunId. The stage fails unless every test passed and none was left in no run; tests left in no run it prints as incomplete. A test a run skipped, or one that could not finish, is no pass either, whatever the run ended as: the stage prints each with its title and why, and fails, as incomplete when nothing worse happened.
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 stage prints how many and fails as incomplete, unless XTEST_ALLOW_NOT_CHECKED is true.
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.
An AI run answers with more than a verdict: each test, the issues it reported and links to its screenshots, so a build that wants detail can print them.
Things to know before you wire it up
- API runs use the Cloud browser. Runs on your own Mac start from the dashboard, where you can watch them.
- Send
{"ai":"off"}for saved steps only, and the build costs no AI at all. A case with no saved steps, one whose steps were edited after they were saved, or one whose saved steps stop part way, is then skipped rather than explored, and one whose saved step types a value read off the screen replays its saved steps up to that step and stops there, as one that could not finish. A suite whose own strict mode is on runs this way from a key, whatever the body says. The stage prints each such case with why, and fails on it. - If the AI needs an answer and nobody is there to give one, the run ends rather than waiting for ever.
- The Cloud browser takes one AI or saved-step run at a time from each person, whether it was started from the dashboard or with a key you made. A second started while the first is going is refused with
409andDEVICE_BUSY, so run the stages that call xtest one after another, not in parallel. Across all your devices, a fourth AI run at once is refused withTOO_MANY_RUNS. - 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 your stage starts while one is going stops it and starts, and xtest verifies that issue again once the Cloud browser is free.