Skip to content

Commit 209f34c

Browse files
committed
rewrite script, now scripts/remote_playwright.sh
1 parent d5a851e commit 209f34c

File tree

4 files changed

+98
-56
lines changed

4 files changed

+98
-56
lines changed

docs/contributing/frontend.md

+17
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,23 @@ to test if the page is being rendered correctly, you should consider using the
217217
> ℹ️ For scenarios where you need to be authenticated, you can use
218218
> `test.use({ storageState: getStatePath("authState") })`.
219219
220+
For ease of debugging, it's possible to run a Playwright test in headful mode
221+
running a Playwright server on your local machine, and executing the test inside
222+
your workspace.
223+
224+
You can either run `scripts/remote_playwright.sh` from `coder/coder` on your
225+
local machine, or execute the following command if you don't have the repo
226+
available:
227+
228+
```bash
229+
bash <(curl -sSL https://raw.githubusercontent.com/coder/coder/main/scripts/remote_playwright.sh) [workspace]
230+
```
231+
232+
The `scripts/remote_playwright.sh` script will start a Playwright server on your
233+
local machine and forward the necessary ports to your workspace. At the end of
234+
the script, you will land _inside_ your workspace with environment variables set
235+
so you can simply execute the test (`pnpm run playwright:test`).
236+
220237
### Integration
221238

222239
Test user interactions like "Click in a button shows a dialog", "Submit the form

scripts/remote_playwright.sh

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
workspace=${1:-}
5+
coder_repo=${2:-.}
6+
port=${3:-3000}
7+
8+
if [[ -z "${workspace}" ]]; then
9+
echo "Usage: $0 <workspace> [workspace coder/coder dir] [e2e port]"
10+
exit 1
11+
fi
12+
13+
main() {
14+
# Check the Playwright version from the workspace so we have a 1-to-1 match
15+
# between the current branch and what we're going to run locally. This is
16+
# necessary because Playwright uses their own protocol for communicating
17+
# between the server and client, and the protocol changes between versions.
18+
echo "Checking Playwright version from \"${workspace}\"..."
19+
playwright_version="$(ssh "coder.${workspace}" "cat '${coder_repo}'/site/pnpm-lock.yaml | grep '^ /@playwright/test@' | cut -d '@' -f 3 | tr -d ':'")"
20+
21+
echo "Found Playwright version ${playwright_version}..."
22+
23+
# Let's store it in cache because, why not, this is ephemeral.
24+
dest=~/.cache/coder-remote-playwright
25+
echo "Initializing Playwright server in ${dest}..."
26+
mkdir -p "${dest}"
27+
cd "${dest}"
28+
echo '{"dependencies":{"@playwright/test":"'"${playwright_version}"'"}}' >package.json
29+
cat <<-EOF >server.mjs
30+
import { chromium } from "@playwright/test";
31+
32+
const server = await chromium.launchServer({ headless: false });
33+
console.log(server.wsEndpoint());
34+
EOF
35+
36+
npm_cmd=npm
37+
if command -v pnpm >/dev/null; then
38+
npm_cmd=pnpm
39+
fi
40+
echo "Running \"${npm_cmd} install\" to ensure local and remote are up-to-date..."
41+
"${npm_cmd}" install
42+
43+
echo "Running \"${npm_cmd} exec playwright install\" for browser binaries..."
44+
"${npm_cmd}" exec playwright install
45+
46+
playwright_out="$(mktemp -t playwright_server_out.XXXXXX)"
47+
48+
rm "${playwright_out}"
49+
mkfifo "${playwright_out}"
50+
exec 3<>"${playwright_out}"
51+
52+
echo "Starting Playwright server..."
53+
${npm_cmd} exec node server.mjs 1>&3 &
54+
playwright_pid=$!
55+
56+
trap '
57+
kill -INT ${playwright_pid}
58+
exec 3>&-
59+
rm "${playwright_out}"
60+
wait ${playwright_pid}
61+
' EXIT
62+
63+
echo "Waiting for Playwright to start..."
64+
read -r ws_endpoint <&3
65+
if [[ ${ws_endpoint} != ws://* ]]; then
66+
echo "Playwright failed to start."
67+
echo "${ws_endpoint}"
68+
cat "${playwright_out}"
69+
exit 1
70+
fi
71+
echo "Playwright started at ${ws_endpoint}"
72+
73+
ws_port=${ws_endpoint##*:}
74+
ws_port=${ws_port%/*}
75+
76+
echo
77+
echo "Starting SSH tunnel, run test via \"pnpm run playwright:test\"..."
78+
ssh -t -R "${ws_port}:127.0.0.1:${ws_port}" -L "${port}:127.0.0.1:${port}" coder."${workspace}" "export CODER_E2E_PORT='${port}'; export CODER_E2E_WS_ENDPOINT='${ws_endpoint}'; [[ -d '${coder_repo}/site' ]] && cd '${coder_repo}/site'; exec \"\$(grep \"\${USER}\": /etc/passwd | cut -d: -f7)\" -i -l"
79+
}
80+
81+
main

site/e2e/server/run_with_forward.sh

-52
This file was deleted.

site/e2e/server/server.mjs

-4
This file was deleted.

0 commit comments

Comments
 (0)