Playwright + TS · Lesson 31 of 36

0/36 done

Module 31: Screenshots, Videos & Trace Viewer

31. Diagnostic Artifacts & Time-Travel Trace Viewer

18 minlesson

Try it yourself

Implement formatTracePath:

  1. Accept testName and attempt number.
  2. Return traces/<sanitized-name>-attempt-<attempt>.zip.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

1. Capturing Screenshots

typescript
// Viewport screenshot
await page.screenshot({ path: "screenshots/home.png" });

// Full page screenshot (scrolls and stitches full document)
await page.screenshot({ path: "screenshots/full-page.png", fullPage: true });

// Specific element screenshot
await page.getByTestId("invoice-card").screenshot({ path: "screenshots/invoice.png" });

2. Enabling Traces in playwright.config.ts

The industry best practice is `on-first-retry` or `retain-on-failure`:

typescript
export default defineConfig({
  use: {
    // Only capture full traces when a test fails, saving disk space:
    trace: "retain-on-failure",
    video: "retain-on-failure",
    screenshot: "only-on-failure",
  },
});

3. Opening Trace Viewer

When a test fails, Playwright produces a trace.zip file. View it via CLI:

bash
npx playwright show-trace test-results/my-test-chromium/trace.zip

Or drop it on https://trace.playwright.dev in any browser. You can inspect:

  • Action filmstrip
  • Before/After DOM snapshots
  • Exact console errors
  • Every single network request and header

Key Takeaways & Walkthrough Summary

Playwright's Trace Viewer is widely regarded as one of the best debugging tools in the industry. It records a full timeline of DOM snapshots, network calls, console logs, and video so you can step through failed tests with ease. In this lesson, we will configure screenshots, videos, and trace capture for seamless failure investigation. Let's inspect our traces!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why not enable video and trace for every test run?

Recording video and DOM traces for 1000 passing tests takes substantial CPU and disk space. 'retain-on-failure' keeps CI builds fast while giving you rich artifacts when failures occur.

Can I view traces inside VS Code?

Yes! The official Playwright VS Code extension opens traces directly inside your editor.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

TypeScript
Skip