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(trace): add 'trace close' command (#39903)
Pavel Feldman807c75967227
diff --git a/packages/playwright-core/src/tools/trace/SKILL.md b/packages/playwright-core/src/tools/trace/SKILL.mdindex 8e4dd44332..6cf0af85ab 100644--- a/packages/playwright-core/src/tools/trace/SKILL.md+++ b/packages/playwright-core/src/tools/trace/SKILL.md@@ -15,6 +15,7 @@ Inspect `.zip` trace files produced by Playwright tests without opening a browse3. Use `trace action <action-id>` to drill into a specific action — see parameters, logs, source location, and available snapshots.4. Use `trace requests`, `trace console`, or `trace errors` for cross-cutting views.5. Use `trace snapshot <action-id>` to get the DOM snapshot, or run a browser command against it.+6. Use `trace close` to remove the extracted trace data when done.All commands after `open` operate on the currently opened trace — no need to pass the trace file again. Opening a new trace replaces the previous one.@@ -27,6 +28,13 @@ All commands after `open` operate on the currently opened trace — no need to pnpx playwright trace open <trace.zip>```+### Close a trace++```bash+# Remove extracted trace data+npx playwright trace close+```+### Actions```bashdiff --git a/packages/playwright-core/src/tools/trace/traceCli.ts b/packages/playwright-core/src/tools/trace/traceCli.tsindex 79cc46f6a0..5bb76135c2 100644--- a/packages/playwright-core/src/tools/trace/traceCli.ts+++ b/packages/playwright-core/src/tools/trace/traceCli.ts@@ -29,6 +29,14 @@ export function addTraceCommands(program: Command, logErrorAndExit: (e: Error) =traceOpen(trace).catch(logErrorAndExit);});+ traceCommand+ .command('close')+ .description('remove extracted trace data')+ .action(async () => {+ const { closeTrace } = await import('./traceUtils');+ closeTrace().catch(logErrorAndExit);+ });+traceCommand.command('actions').description('list actions in the trace')diff --git a/packages/playwright-core/src/tools/trace/traceUtils.ts b/packages/playwright-core/src/tools/trace/traceUtils.tsindex 9dae3b2e4f..a066d76773 100644--- a/packages/playwright-core/src/tools/trace/traceUtils.ts+++ b/packages/playwright-core/src/tools/trace/traceUtils.ts@@ -57,12 +57,16 @@ function ensureTraceOpen(): string {return traceDir;}+export async function closeTrace() {+ if (fs.existsSync(traceDir))+ await fs.promises.rm(traceDir, { recursive: true });+}+export async function openTrace(traceFile: string) {const filePath = path.resolve(traceFile);if (!fs.existsSync(filePath))throw new Error(`Trace file not found: ${filePath}`);- if (fs.existsSync(traceDir))- await fs.promises.rm(traceDir, { recursive: true });+ await closeTrace();await fs.promises.mkdir(traceDir, { recursive: true });await extractTrace(filePath, traceDir);}diff --git a/tests/mcp/trace-cli.spec.ts b/tests/mcp/trace-cli.spec.tsindex 9f6d12e4c0..329d98c6f5 100644--- a/tests/mcp/trace-cli.spec.ts+++ b/tests/mcp/trace-cli.spec.ts@@ -170,6 +170,20 @@ test('trace screenshot saves image file', async ({ runTraceCli }, testInfo) => {}});+test('trace close removes extracted trace', async ({ traceFile, runTraceCli }) => {+ const { exitCode } = await runTraceCli(['close']);+ expect(exitCode).toBe(0);++ // Subsequent commands should fail because trace is closed.+ const { stderr, exitCode: exitCode2 } = await runTraceCli(['actions']);+ expect(exitCode2).toBe(1);+ expect(stderr).toContain('No trace opened');++ // Re-open for remaining tests.+ const { exitCode: exitCode3 } = await runTraceCli(['open', traceFile]);+ expect(exitCode3).toBe(0);+});+test('trace attachments lists attachments', async ({ runTraceCli }) => {const { stdout, exitCode } = await runTraceCli(['attachments']);expect(exitCode).toBe(0);