Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat(web-server): allow waiting for given output (#38055)
Pavel Feldman23588a7c5168
diff --git a/docs/src/test-api/class-testconfig.md b/docs/src/test-api/class-testconfig.mdindex 3061ff18b3..d321cf6621 100644--- a/docs/src/test-api/class-testconfig.md+++ b/docs/src/test-api/class-testconfig.md@@ -704,6 +704,10 @@ export default defineConfig({- `reuseExistingServer` ?<[boolean]> If true, it will re-use an existing server on the `port` or `url` when available. If no server is running on that `port` or `url`, it will run the command to start a new server. If `false`, it will throw if an existing process is listening on the `port` or `url`. This should be commonly set to `!process.env.CI` to allow the local dev server when running tests locally.- `stderr` ?<["pipe"|"ignore"]> Whether to pipe the stderr of the command to the process stderr or ignore it. Defaults to `"pipe"`.- `stdout` ?<["pipe"|"ignore"]> If `"pipe"`, it will pipe the stdout of the command to the process stdout. If `"ignore"`, it will ignore the stdout of the command. Default to `"ignore"`.+ - `wait` ?<[Object]> Consider command started only when given output has been produced or a time in milliseconds has passed.+ - `stdout` ?<[RegExp]>+ - `stderr` ?<[RegExp]>+ - `time` ?<[int]>- `timeout` ?<[int]> How long to wait for the process to start up and be available in milliseconds. Defaults to 60000.- `url` ?<[string]> The url on your http server that is expected to return a 2xx, 3xx, 400, 401, 402, or 403 status code when the server is ready to accept connections. Redirects (3xx status codes) are being followed and the new location is checked. Either `port` or `url` should be specified.diff --git a/packages/playwright/src/plugins/webServerPlugin.ts b/packages/playwright/src/plugins/webServerPlugin.tsindex bdbcfe3d0c..c99ee37261 100644--- a/packages/playwright/src/plugins/webServerPlugin.ts+++ b/packages/playwright/src/plugins/webServerPlugin.ts@@ -16,7 +16,7 @@import net from 'net';import path from 'path';-import { launchProcess, isURLAvailable, monotonicTime, raceAgainstDeadline } from 'playwright-core/lib/utils';+import { launchProcess, isURLAvailable, monotonicTime, raceAgainstDeadline, ManualPromise } from 'playwright-core/lib/utils';import { colors } from 'playwright-core/lib/utils';import { debug } from 'playwright-core/lib/utilsBundle';@@ -29,6 +29,7 @@ import type { ReporterV2 } from '../reporters/reporterV2';export type WebServerPluginOptions = {command: string;url?: string;+ wait?: { stdout?: RegExp, stderr?: RegExp, time?: number };ignoreHTTPSErrors?: boolean;timeout?: number;gracefulShutdown?: { signal: 'SIGINT' | 'SIGTERM', timeout?: number };@@ -55,6 +56,8 @@ export class WebServerPlugin implements TestRunnerPlugin {private _options: WebServerPluginOptions;private _checkPortOnly: boolean;private _reporter?: ReporterV2;+ private _waitForStdioPromise: ManualPromise | undefined;+name = 'playwright:webserver';constructor(options: WebServerPluginOptions, checkPortOnly: boolean) {@@ -138,28 +141,62 @@ export class WebServerPlugin implements TestRunnerPlugin {debugWebServer(`Process started`);+ if (this._options.wait?.stdout || this._options.wait?.stderr)+ this._waitForStdioPromise = new ManualPromise();+ let stdoutWaitCollector = this._options.wait?.stdout ? '' : undefined;+ let stderrWaitCollector = this._options.wait?.stderr ? '' : undefined;++ const resolveStdioPromise = () => {+ stderrWaitCollector = undefined;+ stdoutWaitCollector = undefined;+ this._waitForStdioPromise?.resolve();+ };+launchedProcess.stderr!.on('data', data => {+ if (stderrWaitCollector !== undefined) {+ stderrWaitCollector += data.toString();+ if (this._options.wait?.stderr?.test(stderrWaitCollector))+ resolveStdioPromise();+ }+if (debugWebServer.enabled || (this._options.stderr === 'pipe' || !this._options.stderr))this._reporter!.onStdErr?.(prefixOutputLines(data.toString(), this._options.name));});+launchedProcess.stdout!.on('data', data => {+ if (stdoutWaitCollector !== undefined) {+ stdoutWaitCollector += data.toString();+ if (this._options.wait?.stdout?.test(stdoutWaitCollector))+ resolveStdioPromise();+ }+if (debugWebServer.enabled || this._options.stdout === 'pipe')this._reporter!.onStdOut?.(prefixOutputLines(data.toString(), this._options.name));});}private async _waitForProcess() {- if (!this._isAvailableCallback) {+ // options.time is immune to the timeout.+ if (this._options.wait?.time)+ await new Promise(resolve => setTimeout(resolve, this._options.wait!.time));++ if (!this._isAvailableCallback && !this._waitForStdioPromise) {this._processExitedPromise.catch(() => {});return;}+debugWebServer(`Waiting for availability...`);const launchTimeout = this._options.timeout || 60 * 1000;const cancellationToken = { canceled: false };- const { timedOut } = (await Promise.race([- raceAgainstDeadline(() => waitFor(this._isAvailableCallback!, cancellationToken), monotonicTime() + launchTimeout),- this._processExitedPromise,- ]));+ const deadline = monotonicTime() + launchTimeout;++ const racingPromises = [this._processExitedPromise];+ if (this._isAvailableCallback)+ racingPromises.push(raceAgainstDeadline(() => waitFor(this._isAvailableCallback!, cancellationToken), deadline));+ if (this._waitForStdioPromise)+ racingPromises.push(raceAgainstDeadline(() => this._waitForStdioPromise!, deadline));++ const { timedOut } = await Promise.race(racingPromises);cancellationToken.canceled = true;if (timedOut)throw new Error(`Timed out waiting ${launchTimeout}ms from config.webServer.`);diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.tsindex 01a63a836f..0dcb5d4c60 100644--- a/packages/playwright/types/test.d.ts+++ b/packages/playwright/types/test.d.ts@@ -10170,6 +10170,17 @@ interface TestConfigWebServer {*/stdout?: "pipe"|"ignore";+ /**+ * Consider command started only when given output has been produced or a time in milliseconds has passed.+ */+ wait?: {+ stdout?: RegExp;++ stderr?: RegExp;++ time?: number;+ };+/*** How long to wait for the process to start up and be available in milliseconds. Defaults to 60000.*/diff --git a/tests/playwright-test/web-server.spec.ts b/tests/playwright-test/web-server.spec.tsindex 1d79c0ba1a..3cb120e148 100644--- a/tests/playwright-test/web-server.spec.ts+++ b/tests/playwright-test/web-server.spec.ts@@ -946,3 +946,81 @@ test('should throw helpful error when command is empty', async ({ runInlineTestexpect(result.exitCode).toBe(1);expect(result.output).toContain('config.webServer.command cannot be empty');});++test('should wait for stdout', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ 'test.spec.ts': `+ import { test, expect } from '@playwright/test';+ test('pass', async ({}) => {});+ `,+ 'server.js': `+ setTimeout(() => { console.log('server started'); }, 1000);+ setTimeout(() => {}, 100000);+ `,+ 'playwright.config.ts': `+ module.exports = {+ webServer: [+ {+ command: 'node server.js',+ stdout: 'pipe',+ wait: { stdout: /started/ },+ }+ ],+ };+ `,+ }, undefined);+ expect(result.exitCode).toBe(0);+ expect(result.output).toContain('server started');+});++test('should wait for stderr', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ 'test.spec.ts': `+ import { test, expect } from '@playwright/test';+ test('pass', async ({}) => {});+ `,+ 'server.js': `+ setTimeout(() => { console.error('server started'); }, 1000);+ setTimeout(() => {}, 100000);+ `,+ 'playwright.config.ts': `+ module.exports = {+ webServer: [{+ command: 'node server.js',+ stdout: 'pipe',+ wait: { stderr: /started/ },+ }],+ };+ `,+ }, undefined);+ expect(result.exitCode).toBe(0);+ expect(result.output).toContain('server started');+});++test('should wait for time', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ 'test.spec.ts': `+ import { test, expect } from '@playwright/test';+ test('pass', async ({}) => {+ console.log('TEST: ' + Date.now());+ });+ `,+ 'server.js': `+ console.log('SETUP: ' + Date.now());+ setTimeout(() => {}, 100000);+ `,+ 'playwright.config.ts': `+ module.exports = {+ webServer: [{+ command: 'node server.js',+ stdout: 'pipe',+ wait: { time: 2000 },+ }],+ };+ `,+ }, undefined);+ const [, setupTime] = /SETUP: (\d+)/.exec(result.output)!;+ const [, testTime] = /TEST: (\d+)/.exec(result.output)!;+ expect(+testTime - +setupTime).toBeGreaterThan(2000);+ expect(result.exitCode).toBe(0);+});