Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: set PLAYWRIGHT_TEST=1 when running tests (#37380)

Yury Semikhatsky7330126bdb66
diff --git a/docs/src/test-webserver-js.md b/docs/src/test-webserver-js.md
index 8c0802c450..65e1eecfda 100644
--- a/docs/src/test-webserver-js.md
+++ b/docs/src/test-webserver-js.md
@@ -31,7 +31,7 @@ export default defineConfig({
| [`property: TestConfig.webServer`] | Launch a development web server (or multiple) during the tests. |
| `command`| Shell command to start the local dev server of your app. |
| `cwd` | Current working directory of the spawned process, defaults to the directory of the configuration file. |
-| `env` | Environment variables to set for the command, `process.env` by default. |
+| `env` | Environment variables for the command. Defaults to inheriting `process.env` with `PLAYWRIGHT_TEST=1` added. |
| `gracefulShutdown` | How to shut down the process. If unspecified, the process group is forcefully `SIGKILL`ed. If set to `{ signal: 'SIGTERM', timeout: 500 }`, the process group is sent a `SIGTERM` signal, followed by `SIGKILL` if it doesn't exit within 500ms. You can also use `SIGINT` as the signal instead. A `0` timeout means no `SIGKILL` will be sent. Windows doesn't support `SIGTERM` and `SIGINT` signals, so this option is ignored on Windows. Note that shutting down a Docker container requires `SIGTERM`. |
| `ignoreHTTPSErrors` | Whether to ignore HTTPS errors when fetching the `url`. Defaults to `false`. |
| `name` | Specifies a custom name for the web server. This name will be prefixed to log messages. Defaults to `[WebServer]`. |
diff --git a/packages/playwright-core/src/server/utils/env.ts b/packages/playwright-core/src/server/utils/env.ts
index 500c180f56..840add7d98 100644
--- a/packages/playwright-core/src/server/utils/env.ts
+++ b/packages/playwright-core/src/server/utils/env.ts
@@ -51,3 +51,8 @@ export function getPackageManagerExecCommand() {
export function isLikelyNpxGlobal() {
return process.argv.length >= 2 && process.argv[1].includes('_npx');
}
+
+// Indicate that this and child processes are running under Playwright Test.
+export function setPlaywrightTestProcessEnv() {
+ return process.env['PLAYWRIGHT_TEST'] = '1';
+}
diff --git a/packages/playwright/src/runner/testRunner.ts b/packages/playwright/src/runner/testRunner.ts
index bb06ff33f7..dee47ba7ef 100644
--- a/packages/playwright/src/runner/testRunner.ts
+++ b/packages/playwright/src/runner/testRunner.ts
@@ -19,7 +19,7 @@ import fs from 'fs';
import path from 'path';
import { registry } from 'playwright-core/lib/server';
-import { ManualPromise, gracefullyProcessExitDoNotHang } from 'playwright-core/lib/utils';
+import { ManualPromise, gracefullyProcessExitDoNotHang, setPlaywrightTestProcessEnv } from 'playwright-core/lib/utils';
import { loadConfig } from '../common/configLoader';
import { Watcher } from '../fsWatcher';
@@ -108,6 +108,7 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
watchTestDirs?: boolean;
populateDependenciesOnList?: boolean;
}) {
+ setPlaywrightTestProcessEnv();
this._watchTestDirs = !!params.watchTestDirs;
this._populateDependenciesOnList = !!params.populateDependenciesOnList;
}
@@ -432,6 +433,8 @@ async function resolveCtDirs(config: FullConfigInternal) {
}
export async function runAllTestsWithConfig(config: FullConfigInternal): Promise<FullResultStatus> {
+ setPlaywrightTestProcessEnv();
+
const listOnly = config.cliListOnly;
addGitCommitInfoPlugin(config);
diff --git a/tests/playwright-test/global-setup.spec.ts b/tests/playwright-test/global-setup.spec.ts
index 419fb8753b..aa508f5ef9 100644
--- a/tests/playwright-test/global-setup.spec.ts
+++ b/tests/playwright-test/global-setup.spec.ts
@@ -454,3 +454,38 @@ test('globalTeardown runs even if callback failed', async ({ runInlineTest }) =>
]);
expect(result.output).toContain('Error: kaboom');
});
+
+test('globalSetup and globalTeardown should have PLAYWRIGHT_TEST=1', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'playwright.config.ts': `
+ import * as path from 'path';
+ module.exports = {
+ globalSetup: './globalSetup',
+ globalTeardown: './globalTeardown',
+ };
+ `,
+ 'globalSetup.ts': `
+ export default () => {
+ console.log('\\n%%setup env=' + process.env.PLAYWRIGHT_TEST);
+ };
+ `,
+ 'globalTeardown.ts': `
+ export default () => {
+ console.log('\\n%%teardown env=' + process.env.PLAYWRIGHT_TEST);
+ };
+ `,
+ 'a.test.js': `
+ import { test, expect } from '@playwright/test';
+ test('should work', async ({}, testInfo) => {
+ console.log('\\n%%test env=' + process.env.PLAYWRIGHT_TEST);
+ });
+ `,
+ });
+ expect(result.passed).toBe(1);
+ expect(result.failed).toBe(0);
+ expect(result.outputLines).toEqual([
+ 'setup env=1',
+ 'test env=1',
+ 'teardown env=1',
+ ]);
+});
diff --git a/tests/playwright-test/test-server.spec.ts b/tests/playwright-test/test-server.spec.ts
index 5e0c3f030c..e333827fe6 100644
--- a/tests/playwright-test/test-server.spec.ts
+++ b/tests/playwright-test/test-server.spec.ts
@@ -242,3 +242,17 @@ test('timeout override', async ({ startTestServer, writeFiles }) => {
expect(await testServerConnection.runTests({ timeout: 42 })).toEqual({ status: 'passed' });
});
+
+test('PLAYWRIGHT_TEST environment variable', async ({ startTestServer, writeFiles }) => {
+ const testServerConnection = await startTestServer();
+ await testServerConnection.initialize({});
+ await writeFiles({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('foo', () => {
+ expect(process.env.PLAYWRIGHT_TEST).toBe('1');
+ });
+ `,
+ });
+ expect(await testServerConnection.runTests({})).toEqual({ status: 'passed' });
+});
diff --git a/tests/playwright-test/web-server.spec.ts b/tests/playwright-test/web-server.spec.ts
index 52c541de45..1d79c0ba1a 100644
--- a/tests/playwright-test/web-server.spec.ts
+++ b/tests/playwright-test/web-server.spec.ts
@@ -123,6 +123,67 @@ test('should create a server with environment variables', async ({ runInlineTest
delete process.env['FOOEXTERNAL'];
});
+test('should run web server with PLAYWRIGHT_TEST=1 environment variable', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/37377' }
+}, async ({ runInlineTest }, { workerIndex }) => {
+ const port = workerIndex * 2 + 10500;
+ const result = await runInlineTest({
+ 'test.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ test('connect to the server', async ({baseURL, page}) => {
+ expect(baseURL).toBe('http://localhost:${port}');
+ await page.goto(baseURL + '/env-PLAYWRIGHT_TEST');
+ expect(await page.textContent('body')).toBe('1');
+ });
+ `,
+ 'playwright.config.ts': `
+ module.exports = {
+ webServer: {
+ command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${port}',
+ port: ${port},
+ }
+ };
+ `,
+ }, {}, { DEBUG: 'pw:webserver' });
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
+ expect(result.output).toContain('[WebServer] listening');
+ expect(result.output).toContain('[WebServer] error from server');
+ expect(result.report.suites[0].specs[0].tests[0].results[0].status).toContain('passed');
+});
+
+test('should allow to unset PLAYWRIGHT_TEST environment variable', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/37377' }
+}, async ({ runInlineTest }, { workerIndex }) => {
+ const port = workerIndex * 2 + 10500;
+ const result = await runInlineTest({
+ 'test.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ test('connect to the server', async ({baseURL, page}) => {
+ expect(baseURL).toBe('http://localhost:${port}');
+ await page.goto(baseURL + '/env-PLAYWRIGHT_TEST');
+ expect(await page.textContent('body')).toBe('');
+ });
+ `,
+ 'playwright.config.ts': `
+ module.exports = {
+ webServer: {
+ command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${port}',
+ port: ${port},
+ env: {
+ 'PLAYWRIGHT_TEST': undefined,
+ }
+ }
+ };
+ `,
+ }, {}, { DEBUG: 'pw:webserver' });
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
+ expect(result.output).toContain('[WebServer] listening');
+ expect(result.output).toContain('[WebServer] error from server');
+ expect(result.report.suites[0].specs[0].tests[0].results[0].status).toContain('passed');
+});
+
test('should default cwd to config directory', async ({ runInlineTest }, testInfo) => {
const port = testInfo.workerIndex * 2 + 10500;
const configDir = testInfo.outputPath('foo');