Skip to content

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

feat(test): expose runner process.argv as FullConfig.argv (#40850)

Yury Semikhatsky5422254d35ce
diff --git a/docs/src/test-api/class-fullconfig.md b/docs/src/test-api/class-fullconfig.md
index 7ce3da974b..627218db7b 100644
--- a/docs/src/test-api/class-fullconfig.md
+++ b/docs/src/test-api/class-fullconfig.md
@@ -4,6 +4,17 @@
Resolved configuration which is accessible via [`property: TestInfo.config`] and is passed to the test reporters. To see the format of Playwright configuration file, please see [TestConfig] instead.
+## property: FullConfig.argv
+* since: v1.61
+- type: <[Array]<[string]>>
+
+Snapshot of [`process.argv`](https://nodejs.org/api/process.html#processargv)
+captured in the runner process. Useful for reading custom command-line
+arguments — for example, args supplied after the `--` separator
+(`npx playwright test -- --build-path=./out`). Playwright does not parse
+these; consumers are responsible for slicing and interpreting them with
+any argument-parsing library.
+
## property: FullConfig.configFile
* since: v1.20
- type: ?<[string]>
diff --git a/packages/playwright/src/cli/testActions.ts b/packages/playwright/src/cli/testActions.ts
index 9e7e954ad8..c97ed6dcf8 100644
--- a/packages/playwright/src/cli/testActions.ts
+++ b/packages/playwright/src/cli/testActions.ts
@@ -28,6 +28,7 @@ import type { TestRunOptions } from '../runner/tasks';
export async function runTests(args: string[], opts: { [key: string]: any }) {
await startProfiling();
const cliOverrides = overridesFromOptions(opts);
+ cliOverrides.argv = process.argv;
const config = await configLoader.loadConfigFromFile(opts.config, cliOverrides, opts.deps === false);
const options: TestRunOptions = {
diff --git a/packages/playwright/src/common/config.ts b/packages/playwright/src/common/config.ts
index fed3cd4131..a1098ef099 100644
--- a/packages/playwright/src/common/config.ts
+++ b/packages/playwright/src/common/config.ts
@@ -84,6 +84,7 @@ export class FullConfigInternal {
}
this.config = {
+ argv: configCLIOverrides.argv ?? [],
configFile: resolvedConfigFile,
rootDir: pathResolve(configDir, userConfig.testDir) || configDir,
forbidOnly: takeFirst(configCLIOverrides.forbidOnly, userConfig.forbidOnly, false),
diff --git a/packages/playwright/src/common/ipc.ts b/packages/playwright/src/common/ipc.ts
index 93c3300581..3fdaeccfcb 100644
--- a/packages/playwright/src/common/ipc.ts
+++ b/packages/playwright/src/common/ipc.ts
@@ -23,6 +23,7 @@ import type { ReporterDescription, TestInfoError, TestStatus } from '../../types
import type { SerializedCompilationCache } from '../transform/compilationCache';
export type ConfigCLIOverrides = {
+ argv?: string[];
debug?: 'inspector' | 'cli';
failOnFlakyTests?: boolean;
forbidOnly?: boolean;
diff --git a/packages/playwright/src/isomorphic/teleReceiver.ts b/packages/playwright/src/isomorphic/teleReceiver.ts
index 9e78368b89..bd534c9216 100644
--- a/packages/playwright/src/isomorphic/teleReceiver.ts
+++ b/packages/playwright/src/isomorphic/teleReceiver.ts
@@ -779,6 +779,7 @@ export class TeleTestResult implements reporterTypes.TestResult {
export type TeleFullProject = reporterTypes.FullProject;
export const baseFullConfig: reporterTypes.FullConfig = {
+ argv: [],
forbidOnly: false,
fullyParallel: false,
globalSetup: null,
diff --git a/packages/playwright/src/program.ts b/packages/playwright/src/program.ts
index 0fd53b4856..1366d686d0 100644
--- a/packages/playwright/src/program.ts
+++ b/packages/playwright/src/program.ts
@@ -51,8 +51,14 @@ function addTestCommand(program: Command) {
return command;
});
command.action(async (args, opts) => {
+ // Args supplied after `--` are appended to the variadic [test-filter...]
+ // by commander. Strip them so they aren't used as filter regexes; users
+ // who need to read them go through FullConfig.argv (full process.argv).
+ const dashDashIndex = process.argv.indexOf('--');
+ const postDashCount = dashDashIndex >= 0 ? process.argv.length - 1 - dashDashIndex : 0;
+ const testFilters = args.slice(0, args.length - postDashCount);
try {
- await runTests(args, opts);
+ await runTests(testFilters, opts);
} catch (e) {
console.error(e);
gracefullyProcessExitDoNotHang(1);
diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts
index dc5bf13625..ce67a74db6 100644
--- a/packages/playwright/types/test.d.ts
+++ b/packages/playwright/types/test.d.ts
@@ -2018,6 +2018,14 @@ export interface FullConfig<TestArgs = {}, WorkerArgs = {}> {
* See [testConfig.webServer](https://playwright.dev/docs/api/class-testconfig#test-config-web-server).
*/
webServer: TestConfigWebServer | null;
+ /**
+ * Snapshot of [`process.argv`](https://nodejs.org/api/process.html#processargv) captured in the runner process.
+ * Useful for reading custom command-line arguments — for example, args supplied after the `--` separator (`npx
+ * playwright test -- --build-path=./out`). Playwright does not parse these; consumers are responsible for slicing and
+ * interpreting them with any argument-parsing library.
+ */
+ argv: Array<string>;
+
/**
* Path to the configuration file used to run the tests. The value is an empty string if no config file was used.
*/
diff --git a/tests/playwright-test/config.spec.ts b/tests/playwright-test/config.spec.ts
index e7cefd2cd7..fc83a7eb52 100644
--- a/tests/playwright-test/config.spec.ts
+++ b/tests/playwright-test/config.spec.ts
@@ -788,3 +788,55 @@ test('should throw on invalid --tsconfig', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(1);
expect(result.output).toContain(`--tsconfig "does-not-exist.json" does not exist`);
});
+
+test('should expose process.argv as config.argv and not pollute test discovery', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'playwright.config.ts': `
+ module.exports = {};
+ `,
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('pass', async ({}, testInfo) => {
+ expect(testInfo.timeout).toBe(7777);
+ expect(testInfo.config.argv).toContain('--build-path=/foo');
+ expect(testInfo.config.argv).toContain('--env=staging');
+ expect(testInfo.config.argv).toContain('--');
+ });
+ `
+ }, { timeout: '7777' }, {}, { additionalArgs: ['--', '--build-path=/foo', '--env=staging'] });
+
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
+});
+
+test('config.argv should be visible in globalSetup and reporter', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'playwright.config.ts': `
+ module.exports = {
+ globalSetup: './global-setup.ts',
+ reporter: './my-reporter.ts',
+ };
+ `,
+ 'global-setup.ts': `
+ module.exports = async (config) => {
+ console.log('GLOBAL_SETUP_HAS_ARG=' + config.argv.includes('--build-path=/foo'));
+ };
+ `,
+ 'my-reporter.ts': `
+ class MyReporter {
+ onBegin(config) {
+ console.log('REPORTER_HAS_ARG=' + config.argv.includes('--build-path=/foo'));
+ }
+ }
+ module.exports = MyReporter;
+ `,
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('pass', async () => {});
+ `
+ }, { reporter: '' }, {}, { additionalArgs: ['--', '--build-path=/foo'] });
+
+ expect(result.exitCode).toBe(0);
+ expect(result.output).toContain(`GLOBAL_SETUP_HAS_ARG=true`);
+ expect(result.output).toContain(`REPORTER_HAS_ARG=true`);
+});