88 / 168 · 01 Agent Skills for Browser Automation · CI/CD Integration for AI-Driven Browser Tests← prev⊞ allnext →☰ Read as one page
12.3Test Runner Script
run-tests.sh
#!/bin/bash
set -euo pipefail
TEST_GROUP="${1:-all}"
BASE_URL="${TEST_BASE_URL:-http://localhost:3000}"
FAILURES_DIR="failures"
SCREENSHOTS_DIR="screenshots"
mkdir -p "$FAILURES_DIR" "$SCREENSHOTS_DIR"
PASSED=0
FAILED=0
TOTAL=0
# pw: run a CLI command inside this test's named session
pw() { playwright-cli -s="$CURRENT_TEST" "$@"; }
export -f pw
export BASE_URL
run_test() {
local test_name="$1"
local test_script="$2"
TOTAL=$((TOTAL + 1))
export CURRENT_TEST="$test_name"
echo -n " $test_name ... "
# Execute test in its own session, capture output
if output=$(bash -c "$test_script" 2>&1); then
echo "PASS"
PASSED=$((PASSED + 1))
else
echo "FAIL"
FAILED=$((FAILED + 1))
# Capture failure artifacts
mkdir -p "$FAILURES_DIR/$test_name"
echo "$output" > "$FAILURES_DIR/$test_name/output.txt"
pw screenshot 2>/dev/null || true # PNG lands in .playwright-cli/
pw snapshot 2>/dev/null || true # YAML a11y state
pw eval "location.href" > "$FAILURES_DIR/$test_name/current_url.txt" 2>/dev/null || true
cp -r .playwright-cli/ "$FAILURES_DIR/$test_name/workspace/" 2>/dev/null || true
fi
# Fresh session per test: always tear down
pw close 2>/dev/null || true
}
echo "Running $TEST_GROUP tests against $BASE_URL"
echo "=========================================="
# Load and run tests for the group
case $TEST_GROUP in
auth)
run_test "login_valid" '
pw open "$BASE_URL/login" &&
pw snapshot &&
pw fill e3 "test@example.com" &&
pw fill e4 "password123" &&
pw click e5 &&
pw eval "document.querySelector(\"h1\").textContent" | grep -q "Dashboard"
'
run_test "login_invalid" '
pw open "$BASE_URL/login" &&
pw snapshot &&
pw fill e3 "wrong@example.com" &&
pw fill e4 "wrongpass" &&
pw click e5 &&
pw eval "document.querySelector(\".error\").textContent" | grep -qi "invalid"
'
;;
dashboard)
run_test "dashboard_loads" '
pw open "$BASE_URL/dashboard" &&
pw eval "document.querySelector(\".metric-count\").textContent" | grep -qE "[0-9]+"
'
;;
*)
echo "Unknown test group: $TEST_GROUP"
exit 1
;;
esac
echo ""
echo "=========================================="
echo "Results: $PASSED passed, $FAILED failed, $TOTAL total"
# Exit with failure if any tests failed
[ "$FAILED" -eq 0 ]
A candid note on this script: hard-coding refs (e3, e4) in a shell script works only for pages whose snapshot layout is stable. That's fine for smoke tests. The moment refs drift, you want either the agent in the loop (it re-snapshots and re-resolves) or promoted, generated .spec.ts tests from the generator agent — scripted ref sequences are the least self-healing artifact in this stack. Use them deliberately.