Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS MATH

Playwright Trace Viewer


Share

Playwright Trace Viewer

The Trace Viewer is a Playwright tool that lets you replay a test after it has run, step by step.

It reads a trace: a recording of everything that happened during the test.

A trace is saved as one trace.zip file.

It holds every action, a snapshot of the page before and after each action, the network requests, the console messages and your test code.

Traces are the best way to find out why a test failed, especially a test that failed on another computer, like a CI server (a computer that runs your tests automatically).


Record a Trace

The quickest way to get a trace is the --trace on option, which records a trace for every test:

Example

npx playwright test --trace on

We use this test from the First Test chapter:

Example

tests/todo.spec.js

// @ts-check
import { test, expect } from '@playwright/test';

test('can add a todo', async ({ page }) => {
  await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
  await page.getByPlaceholder('What needs to be done?').fill('Learn Playwright');
  await page.getByRole('button', { name: 'Add' }).click();
  await expect(page.getByText('Learn Playwright')).toBeVisible();
  await expect(page.getByTestId('todo-count')).toHaveText('1 item left');
});

Output:

Running 1 test using 1 worker

[1/1] [chromium] › tests/todo.spec.js:4:1 › can add a todo
  1 passed (2.1s)

The trace is saved in the test-results folder, in a folder named after the test:

test-results/
  todo-can-add-a-todo-chromium/
    trace.zip


Trace Modes in the Config

Recording a trace for every test makes runs slower and uses disk space.

That is why you usually set the trace option in playwright.config.js to record only when it helps:

Example

playwright.config.js

export default defineConfig({
  use: {
    trace: 'on-first-retry',
  },
});
ValueWhat it records
'off'No traces. This is the default
'on'A trace for every test
'on-first-retry'A trace only when a failed test is retried for the first time
'retain-on-failure'A trace for every test, but keeps it only when the test fails

The config from npm init playwright uses 'on-first-retry'.

It works together with retries, which you will learn about in the Retries chapter.

When you run tests without retries, use 'retain-on-failure' to get a trace for every failed test.

To see this, change the last line of the todo test so that it fails:

Example

tests/todo.spec.js

  await expect(page.getByTestId('todo-count')).toHaveText('2 items left');

Run it again, and Playwright prints where the trace is and how to open it:

Example

npx playwright test --trace retain-on-failure

Output:

Running 1 test using 1 worker

[1/1] [chromium] › tests/todo.spec.js:4:1 › can add a todo
  1) [chromium] › tests/todo.spec.js:4:1 › can add a todo ──────────────────────────────────────────

    Error: expect(locator).toHaveText(expected) failed

    Locator:  getByTestId('todo-count')
    Expected: "2 items left"
    Received: "1 item left"
    Timeout:  5000ms

    attachment #2: trace (application/zip) ─────────────────────────────────────────────────────────
    test-results/todo-can-add-a-todo-chromium/trace.zip
    Usage:

        npx playwright show-trace test-results/todo-can-add-a-todo-chromium/trace.zip

    ────────────────────────────────────────────────────────────────────────────────────────────────


  1 failed
    [chromium] › tests/todo.spec.js:4:1 › can add a todo ───────────────────────────────────────────

Open the Trace Viewer

Open a trace file with show-trace:

Example

npx playwright show-trace test-results/todo-can-add-a-todo-chromium/trace.zip

The Trace Viewer opens in its own window.

You can also open a trace from the HTML report: click a test, then click its trace.

Tip: Add --port 9322 to serve the Trace Viewer at http://localhost:9322 in your normal browser instead.

You can also drop a trace.zip file on trace.playwright.dev. The trace is opened in your browser and is not uploaded.


Inside the Trace Viewer

This is the trace of the todo test, with the Click action selected:

The Playwright Trace Viewer with the action list on the left, a snapshot of the todo page after the click in the middle, and the Call tab at the bottom
  • Timeline (top): small screenshots over time, and you can move the mouse over it to see the page at any moment
  • Actions (left): every step of the test, with the locator and how long it took
  • Snapshot (middle): the page at the selected action, with the tabs Before, Action and After
  • Tabs (bottom): details about the selected action

The snapshots are not images.

They are copies of the real page and its HTML (DOM snapshots).

You can open a snapshot in a new window and inspect it with the browser's developer tools.

The After snapshot above shows the new todo and the text "1 item left".


The Detail Tabs

The tabs at the bottom show everything about the selected action:

TabWhat it shows
LocatorTry out locators against the snapshot
CallThe action, its locator, parameters and timing
LogWhat Playwright did step by step, like waiting for the element
ErrorsThe error message, if the action failed
ConsoleMessages the page wrote to the browser console
NetworkEvery request the page made, with status, type, size and time
SourceYour test file, with the current line highlighted
AttachmentsScreenshots and other files attached to the test

Here is the same Click action with the Before snapshot and the Log tab:

The Trace Viewer showing the page before the click, with the Log tab listing how Playwright waited for the Add button and clicked it

The red dot on the Add button shows where Playwright clicked.

The log shows the auto-waiting from the Auto-Waiting chapter: Playwright waited until the button was visible, enabled and stable, and then clicked.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.