|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Script to run Playwright visual regression tests in Docker. |
| 5 | +# Storybook should be running on the host system at port 6006. |
| 6 | +# |
| 7 | +# Environment variables (set by bin/index.js): |
| 8 | +# PACKAGE_DIR - absolute path to the consuming package (e.g. /repo/packages/fuselage) |
| 9 | + |
| 10 | +PACKAGE_DIR="${PACKAGE_DIR:-$(pwd)}" |
| 11 | + |
| 12 | +# Derive monorepo root from git |
| 13 | +MONOREPO_ROOT="$(git -C "${PACKAGE_DIR}" rev-parse --show-toplevel)" |
| 14 | + |
| 15 | +# Relative path of consuming package inside the monorepo (e.g. packages/fuselage) |
| 16 | +REL_PACKAGE_PATH="$(node -e 'console.log(require("path").relative(process.argv[1], process.argv[2]))' "${MONOREPO_ROOT}" "${PACKAGE_DIR}")" |
| 17 | + |
| 18 | +TOOL_DIR="$(cd "$(dirname "$0")/.." && pwd)" |
| 19 | +REL_TOOL_PATH="$(node -e 'console.log(require("path").relative(process.argv[1], process.argv[2]))' "${MONOREPO_ROOT}" "${TOOL_DIR}")" |
| 20 | + |
| 21 | +BASE_URL="http://host.docker.internal:6006" |
| 22 | + |
| 23 | +# Check if update-snapshots flag is passed |
| 24 | +UPDATE_SNAPSHOTS="" |
| 25 | +case "$*" in |
| 26 | + *--update-snapshots*) UPDATE_SNAPSHOTS="--update-snapshots" ;; |
| 27 | +esac |
| 28 | + |
| 29 | +# Read the Playwright version from this tool's package.json. |
| 30 | +PLAYWRIGHT_VERSION="$(node -e 'console.log(require(process.argv[1]).dependencies["@playwright/test"].replace(/^[^0-9]*/, ""))' "${TOOL_DIR}/package.json")" |
| 31 | + |
| 32 | +DOCKER_PW_IMAGE="mcr.microsoft.com/playwright:v${PLAYWRIGHT_VERSION}-noble" |
| 33 | + |
| 34 | +echo "Running Playwright tests in Docker container for ${REL_PACKAGE_PATH} with env:" |
| 35 | +echo "PACKAGE_DIR: ${PACKAGE_DIR}" |
| 36 | +echo "MONOREPO_ROOT: ${MONOREPO_ROOT}" |
| 37 | +echo "REL_PACKAGE_PATH: ${REL_PACKAGE_PATH}" |
| 38 | +echo "TEST_BASE_URL: ${BASE_URL}" |
| 39 | +echo "CI: ${CI:-false}" |
| 40 | +echo "UPDATE_SNAPSHOTS: ${UPDATE_SNAPSHOTS}" |
| 41 | +echo "DOCKER_PW_IMAGE: ${DOCKER_PW_IMAGE}" |
| 42 | + |
| 43 | +docker run --rm \ |
| 44 | + --init \ |
| 45 | + --ipc=host \ |
| 46 | + --add-host=host.docker.internal:host-gateway \ |
| 47 | + -e TEST_BASE_URL="${BASE_URL}" \ |
| 48 | + -e CI \ |
| 49 | + -e PACKAGE_DIR="/app/${REL_PACKAGE_PATH}" \ |
| 50 | + -v "${MONOREPO_ROOT}:/app" \ |
| 51 | + -w "/app/${REL_PACKAGE_PATH}" \ |
| 52 | + "${DOCKER_PW_IMAGE}" \ |
| 53 | + npx -y playwright@${PLAYWRIGHT_VERSION} test --config="/app/${REL_TOOL_PATH}/playwright.config.ts" ${UPDATE_SNAPSHOTS} |
| 54 | + |
| 55 | +echo "Playwright tests completed!" |
0 commit comments