#!/usr/bin/env bash set -euo pipefail # ----------------------------------------------------------------------------- # playwright-recorder: Replay Playwright tests (headless) # # Purpose: # - Replay previously generated Playwright tests # - Run fully headless # - Use Docker + official Playwright image # - No GUI, no X11, no xhost # # Usage: # ./scripts/replay.sh # ./scripts/replay.sh recordings/login.spec.ts # # Optional env vars: # PLAYWRIGHT_VERSION=1.58.1 # PLAYWRIGHT_IMAGE=mcr.microsoft.com/playwright:v1.58.1-jammy # RECORDINGS_DIR=recordings # ----------------------------------------------------------------------------- PLAYWRIGHT_VERSION="${PLAYWRIGHT_VERSION:-1.58.1}" PLAYWRIGHT_IMAGE="${PLAYWRIGHT_IMAGE:-mcr.microsoft.com/playwright:v${PLAYWRIGHT_VERSION}-jammy}" RECORDINGS_DIR="${RECORDINGS_DIR:-recordings}" TEST_FILE="${1:-}" die() { echo "ERROR: $*" >&2; exit 1; } require_cmd() { command -v "$1" >/dev/null 2>&1 || die "Missing command: $1" } repo_root() { git rev-parse --show-toplevel 2>/dev/null || pwd } require_cmd docker ROOT="$(repo_root)" REC_DIR="${ROOT}/${RECORDINGS_DIR}" [[ -d "${REC_DIR}" ]] || die "Recordings dir not found: ${REC_DIR}" # Build ephemeral workspace WORK_DIR="${REC_DIR}/.replay-work" rm -rf "${WORK_DIR}" mkdir -p "${WORK_DIR}/tests" cleanup() { rm -rf "${WORK_DIR}" >/dev/null 2>&1 || true } trap cleanup EXIT # Copy recorded tests into workspace if [[ -n "${TEST_FILE}" ]]; then [[ -f "${ROOT}/${TEST_FILE}" ]] || die "Test file not found: ${TEST_FILE}" cp "${ROOT}/${TEST_FILE}" "${WORK_DIR}/tests/" else shopt -s nullglob cp "${REC_DIR}"/*.spec.ts "${WORK_DIR}/tests/" || die "No *.spec.ts found in ${REC_DIR}" fi # Minimal Playwright project cat > "${WORK_DIR}/package.json" < "${WORK_DIR}/playwright.config.ts" <<'EOF' import { defineConfig } from '@playwright/test'; export default defineConfig({ testDir: './tests', use: { headless: true, trace: 'retain-on-failure', }, }); EOF echo "▶ Replaying Playwright tests" echo "Image : ${PLAYWRIGHT_IMAGE}" echo "Workspace : ${WORK_DIR}" echo "Tests : ${TEST_FILE:-all recordings}" echo docker run --rm \ -v "${WORK_DIR}:/work" \ -w /work \ "${PLAYWRIGHT_IMAGE}" \ bash -lc "npm install --no-audit --no-fund && npx playwright test" echo echo "✔ Replay finished"