Playwright + TS · Lesson 31 of 36
Module 31: Screenshots, Videos & Trace Viewer
Try it yourself
Implement formatTracePath:
The editor has a worked example. Read it, tweak it, then press Mark as complete.
In this lesson
// 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" });playwright.config.tsThe industry best practice is `on-first-retry` or `retain-on-failure`:
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",
},
});When a test fails, Playwright produces a trace.zip file. View it via CLI:
npx playwright show-trace test-results/my-test-chromium/trace.zipOr drop it on https://trace.playwright.dev in any browser. You can inspect:
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.
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.
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.