|
| 1 | +import '#pkg/set-env.js'; |
| 2 | + |
| 3 | +import { devices, type PlaywrightTestConfig, type ReporterDescription } from '@playwright/test'; |
| 4 | +import os from 'node:os'; |
| 5 | + |
| 6 | +import { config } from '#pkg/config.js'; |
| 7 | +import { playwrightBrowserConfig } from '#pkg/constants.js'; |
| 8 | + |
| 9 | +const countOfCpus = os.cpus().length; |
| 10 | +const workers = countOfCpus !== 0 ? (countOfCpus <= 4 ? countOfCpus : 4) : undefined; |
| 11 | + |
| 12 | +const htmlReporter: ReporterDescription = [ |
| 13 | + 'html', |
| 14 | + { open: 'never', outputFolder: '../playwright-html-report' }, |
| 15 | +]; |
| 16 | + |
| 17 | +const playwrightConfig: PlaywrightTestConfig = { |
| 18 | + fullyParallel: true, |
| 19 | + reporter: config.CI ? [htmlReporter, ['github']] : [htmlReporter], |
| 20 | + testMatch: ['*.spec.js'], |
| 21 | + globalTimeout: 1000 * 8 * 60, // 8 minutes |
| 22 | + projects: [ |
| 23 | + { |
| 24 | + name: 'chromium', |
| 25 | + use: { |
| 26 | + ...devices['Desktop Chrome'], |
| 27 | + // opt into "New Headless" chromium (https://playwright.dev/docs/browsers#chromium-new-headless-mode, https://developer.chrome.com/docs/chromium/headless) |
| 28 | + channel: 'chromium', |
| 29 | + }, |
| 30 | + }, |
| 31 | + ], |
| 32 | + |
| 33 | + /** |
| 34 | + * increase timeout to 30 minutes and set workers count to 1 if we are in a debugging session |
| 35 | + */ |
| 36 | + timeout: config.isDebuggingSession ? 1000 * 60 * 30 : undefined, |
| 37 | + workers: config.isDebuggingSession ? 1 : workers, |
| 38 | + |
| 39 | + // fail a Playwright run in CI if some test.only is in the source code |
| 40 | + forbidOnly: !!config.CI, |
| 41 | + |
| 42 | + snapshotPathTemplate: `{testDir}/../snapshots/{testFilePath}/{arg}-{projectName}-${playwrightBrowserConfig.browser === 'docker' ? 'docker' : '{platform}'}{ext}`, |
| 43 | + |
| 44 | + expect: { |
| 45 | + toHaveScreenshot: { |
| 46 | + maxDiffPixelRatio: 0.03, |
| 47 | + }, |
| 48 | + }, |
| 49 | + |
| 50 | + use: { |
| 51 | + /* have consistent timezone and locale */ |
| 52 | + timezoneId: 'Europe/Vienna', |
| 53 | + locale: 'de-AT', |
| 54 | + |
| 55 | + // always capture trace (seems to not have any performance impact) |
| 56 | + trace: 'on', |
| 57 | + |
| 58 | + // always capture video (seems to not have any performance impact) |
| 59 | + video: 'on', |
| 60 | + }, |
| 61 | + |
| 62 | + webServer: |
| 63 | + playwrightBrowserConfig.browser === 'docker' |
| 64 | + ? { |
| 65 | + // start the Playwright server in a docker container |
| 66 | + command: createDockerRunCommand(playwrightBrowserConfig.port), |
| 67 | + url: `http://127.0.0.1:${playwrightBrowserConfig.port}/`, |
| 68 | + stdout: 'pipe', |
| 69 | + stderr: 'pipe', |
| 70 | + timeout: 30_000, |
| 71 | + gracefulShutdown: { |
| 72 | + signal: 'SIGTERM', |
| 73 | + timeout: 10_000, |
| 74 | + }, |
| 75 | + reuseExistingServer: !config.CI, |
| 76 | + } |
| 77 | + : undefined, |
| 78 | +}; |
| 79 | + |
| 80 | +// eslint-disable-next-line import/no-default-export -- needs to be default export for Playwright |
| 81 | +export default playwrightConfig; |
| 82 | + |
| 83 | +function createDockerRunCommand(port: number) { |
| 84 | + let dockerRunCommand = `docker run --rm --init --workdir /home/pwuser --user pwuser --network host`; |
| 85 | + if (!config.CI) { |
| 86 | + // on development machines, we forward the X11 socket to the host system to allow GUI applications to run from within the container |
| 87 | + dockerRunCommand += ` -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix`; |
| 88 | + } |
| 89 | + 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"`; |
| 90 | + |
| 91 | + return dockerRunCommand; |
| 92 | +} |
0 commit comments