Skip to content

Commit 17e7881

Browse files
committed
refactor(webpage-observer): let Playwright Server choose random free port and use that
1 parent bb391c3 commit 17e7881

3 files changed

Lines changed: 25 additions & 27 deletions

File tree

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { config } from '#pkg/config.js';
22

3-
export const playwrightBrowserConfig =
3+
export const playwrightServerPortEnvVarName = 'PLAYWRIGHT_SERVER_PORT';
4+
5+
export const playwrightBrowser =
46
/*
57
* `--debug` does not correctly work when Playwright is running browsers in the Playwright Server Docker container --> run Playwright browsers locally instead
68
*/
7-
config.isPlaywrightStartedWithDebug
8-
? ({ browser: 'local' } as const)
9-
: ({ browser: 'docker', port: 36_719 } as const);
9+
config.isPlaywrightStartedWithDebug ? 'local' : 'docker';

packages/webpage-observer/src/playwright.config.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defineConfig, devices, type ReporterDescription } from '@playwright/tes
22
import os from 'node:os';
33

44
import { config } from '#pkg/config.js';
5-
import { playwrightBrowserConfig } from '#pkg/constants.js';
5+
import { playwrightBrowser, playwrightServerPortEnvVarName } from '#pkg/constants.js';
66

77
const countOfCpus = os.cpus().length;
88
const workers = countOfCpus
@@ -41,7 +41,7 @@ export default defineConfig({
4141
// fail a Playwright run in CI if some test.only is in the source code
4242
forbidOnly: !!config.CI,
4343

44-
snapshotPathTemplate: `{testDir}/../snapshots/{testFilePath}/{arg}-{projectName}-${playwrightBrowserConfig.browser === 'docker' ? 'docker' : '{platform}'}{ext}`,
44+
snapshotPathTemplate: `{testDir}/../snapshots/{testFilePath}/{arg}-{projectName}-${playwrightBrowser === 'docker' ? 'docker' : '{platform}'}{ext}`,
4545

4646
expect: {
4747
toHaveScreenshot: {
@@ -59,14 +59,30 @@ export default defineConfig({
5959

6060
// always capture video (seems to not have any performance impact)
6161
video: 'on',
62+
63+
connectOptions:
64+
playwrightBrowser === 'docker'
65+
? {
66+
wsEndpoint: `ws://127.0.0.1:${
67+
// eslint-disable-next-line n/no-process-env -- port provided by Playwright server stdout via regex (see webServer.wait.stdout)
68+
process.env[playwrightServerPortEnvVarName]
69+
}/`,
70+
}
71+
: undefined,
6272
},
6373

6474
webServer:
65-
playwrightBrowserConfig.browser === 'docker'
75+
playwrightBrowser === 'docker'
6676
? {
6777
// start the Playwright server in a docker container
68-
command: createDockerRunCommand(playwrightBrowserConfig.port),
69-
url: `http://127.0.0.1:${playwrightBrowserConfig.port}/`,
78+
command: `docker run --rm --init --workdir /home/pwuser --user pwuser --network host mcr.microsoft.com/playwright:v1.57.0-noble /bin/sh -c "npx -y playwright@1.57.0 run-server --host 0.0.0.0"`,
79+
wait: {
80+
// Capture the Playwright Server port from stdout via regex (https://playwright.dev/docs/api/class-testconfig#test-config-web-server)
81+
// eslint-disable-next-line prefer-regex-literals
82+
stdout: new RegExp(
83+
String.raw`Listening on ws:\/\/0\.0\.0\.0:(?<${playwrightServerPortEnvVarName}>\d+)`,
84+
),
85+
},
7086
stdout: 'pipe',
7187
stderr: 'pipe',
7288
timeout: 30_000,
@@ -78,14 +94,3 @@ export default defineConfig({
7894
}
7995
: undefined,
8096
});
81-
82-
function createDockerRunCommand(port: number) {
83-
let dockerRunCommand = `docker run --rm --init --workdir /home/pwuser --user pwuser --network host`;
84-
if (!config.CI) {
85-
// on development machines, we forward the X11 socket to the host system to allow GUI applications to run from within the container
86-
dockerRunCommand += ` -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix`;
87-
}
88-
dockerRunCommand += ` mcr.microsoft.com/playwright:v1.57.0-noble /bin/sh -c "npx -y playwright@1.57.0 run-server --port ${port} --host 0.0.0.0"`;
89-
90-
return dockerRunCommand;
91-
}

packages/webpage-observer/src/set-env.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)