</>

Technology

Playwright

Difficulty

Beginner

Interview Question

What is the Playwright Trace Viewer?

Answer

Playwright Trace Viewer

The Trace Viewer is a powerful debugging tool that records a complete timeline of your test execution and lets you replay it step by step in the browser.

What a Trace Captures

  • DOM snapshot for every action (before and after)
  • Screenshots at each step
  • Network requests and responses
  • Console logs and errors
  • Action timing (how long each step took)
  • Source code for each step

How to Capture a Trace

Option 1: On Failure Only (Recommended for CI)

TypeScript
// playwright.config.ts
export default defineConfig({
  use: {
    trace: 'retain-on-failure', // Only keep trace if test fails
  },
});

Option 2: Always

TypeScript
use: { trace: 'on' }

Option 3: Via CLI

Bash
npx playwright test --trace on

Option 4: Manually in a Test

TypeScript
test('debug this test', async ({ page, context }) => {
  await context.tracing.start({ screenshots: true, snapshots: true });

  await page.goto('/login');
  await page.getByLabel('Email').fill('user@test.com');

  await context.tracing.stop({ path: 'trace.zip' });
});

Viewing the Trace

After Test Run

Bash
npx playwright show-report

Open the HTML report, click on a failed test → "Trace" tab shows the trace viewer.

Open a Trace File Directly

Bash
npx playwright show-trace trace.zip

Online Viewer

Upload trace.zip to: https://trace.playwright.dev

Trace Viewer UI

The trace viewer shows:

  • Timeline bar — each action as a segment
  • DOM panel — rendered HTML snapshot at the selected step
  • Source panel — test code with the current line highlighted
  • Network panel — all HTTP requests made
  • Console panel — browser console output
  • Action list — all recorded actions with timing

When to Use Traces

SituationTrace Setting
CI (production)retain-on-failure
Local debuggingon
Initial developmenton-first-retry
Performance testingoff

Pro Tip: When a test passes locally but fails in CI, enable traces in CI (retain-on-failure) and download the trace.zip artifact to debug exactly what happened.

Follow AutomateQA

Related Topics