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 runner): add --trace cli option (#16277)

Dmitry Gozmancb30cb4eb711
diff --git a/packages/playwright-test/src/cli.ts b/packages/playwright-test/src/cli.ts
index dd4e45fd25..14056a0d39 100644
--- a/packages/playwright-test/src/cli.ts
+++ b/packages/playwright-test/src/cli.ts
@@ -26,6 +26,7 @@ import { stopProfiling, startProfiling } from './profiler';
import type { TestFileFilter } from './util';
import { showHTMLReport } from './reporters/html';
import { baseFullConfig, defaultTimeout, fileIsModule } from './loader';
+import type { TraceMode } from './types';
export function addTestCommands(program: Command) {
addTestCommand(program);
@@ -56,6 +57,7 @@ function addTestCommand(program: Command) {
command.option('--shard <shard>', `Shard tests and execute only the selected shard, specify in the form "current/all", 1-based, for example "3/5"`);
command.option('--project <project-name...>', `Only run tests from the specified list of projects (default: run all projects)`);
command.option('--timeout <timeout>', `Specify test timeout threshold in milliseconds, zero for unlimited (default: ${defaultTimeout})`);
+ command.option('--trace <mode>', `Force tracing mode, can be ${kTraceModes.map(mode => `"${mode}"`).join(', ')}`);
command.option('-u, --update-snapshots', `Update snapshots with actual results (default: only create missing snapshots)`);
command.option('-x', `Stop after the first failure`);
command.action(async (args, opts) => {
@@ -130,6 +132,12 @@ async function runTests(args: string[], opts: { [key: string]: any }) {
overrides.workers = 1;
process.env.PWDEBUG = '1';
}
+ if (opts.trace) {
+ if (!kTraceModes.includes(opts.trace))
+ throw new Error(`Unsupported trace mode "${opts.trace}", must be one of ${kTraceModes.map(mode => `"${mode}"`).join(', ')}`);
+ overrides.use = overrides.use || {};
+ overrides.use.trace = opts.trace;
+ }
// When no --config option is passed, let's look for the config file in the current directory.
const configFileOrDirectory = opts.config ? path.resolve(process.cwd(), opts.config) : process.cwd();
@@ -250,3 +258,5 @@ function restartWithExperimentalTsEsm(configFile: string | null): boolean {
});
return true;
}
+
+const kTraceModes: TraceMode[] = ['on', 'off', 'on-first-retry', 'retain-on-failure'];
diff --git a/tests/playwright-test/playwright.trace.spec.ts b/tests/playwright-test/playwright.trace.spec.ts
index 5d57398b97..db5255aef6 100644
--- a/tests/playwright-test/playwright.trace.spec.ts
+++ b/tests/playwright-test/playwright.trace.spec.ts
@@ -267,6 +267,20 @@ test('should retain traces for interrupted tests', async ({ runInlineTest }, tes
expect(fs.existsSync(testInfo.outputPath('test-results', 'b-test-2', 'trace.zip'))).toBeTruthy();
});
+test('should respect --trace', async ({ runInlineTest }, testInfo) => {
+ const result = await runInlineTest({
+ 'a.spec.ts': `
+ pwt.test('test 1', async ({ page }) => {
+ await page.goto('about:blank');
+ });
+ `,
+ }, { trace: 'on' });
+
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
+ expect(fs.existsSync(testInfo.outputPath('test-results', 'a-test-1', 'trace.zip'))).toBeTruthy();
+});
+
async function parseTrace(file: string): Promise<Map<string, Buffer>> {
const zipFS = new ZipFile(file);
const resources = new Map<string, Buffer>();