日本語

How to Review Failed Playwright Tests with the Trace Viewer

​​Playwright​E2E Testing,Troubleshooting,TypeScript

When a Playwright test fails, the terminal error only says that the expected text didn't match. The Trace Viewer lets you review afterward what the page looked like at each step, the browser console, and the requests sent and responses received. It helps when you cannot open a browser and check things directly, such as with an E2E test that failed in CI.

In my test environment, I wrote a test that fails on purpose, recorded a trace, and checked whether I could trace the cause in the Trace Viewer.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, and @playwright/test 1.63.0. I wrote the tests in TypeScript and ran them only with Chromium (headless). The page under test was served by a small Node.js server I wrote for this check, and I did not connect to any external sites. Because the environment has no display, I opened the Trace Viewer in a browser by passing --host and --port. I did not test the window that opens with a normal launch, opening trace.playwright.dev in a browser, or recording on CI.

Preparing a failing test

For this check, I used a page where you enter a title, press "Save," and the result appears in the field below. The save API (/api/save) was set to return a 500 error on purpose. When the API fails, the page writes an error to the console and displays a "could not save" message (the page text is in Japanese).

The test expects the Japanese text meaning "Saved" to appear, so it fails.

ts
// tests/save.spec.ts
import { test, expect } from '@playwright/test';

test('タイトルを入れて保存できる', async ({ page }) => {
  await page.goto('/');
  await page.getByLabel('タイトル').fill('トレースビューアーの使い方');
  await page.getByRole('button', { name: '保存' }).click();
  await expect(page.locator('#status')).toHaveText('保存しました');
});

test('ページの見出しが出る', async ({ page }) => {
  await page.goto('/');
  await expect(page.getByRole('heading', { level: 1 })).toHaveText('下書きの編集');
});

Configuring traces to keep only for failed tests

You control whether traces are recorded with the trace option under use in playwright.config.ts. If you want to keep only the traces of failed tests, set it to 'retain-on-failure'.

ts
// playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  reporter: [['list'], ['html', { open: 'never' }]],
  use: {
    baseURL: 'http://localhost:3000',
    trace: 'retain-on-failure',
  },
  webServer: {
    command: 'node server.mjs',
    url: 'http://localhost:3000',
  },
});

When I ran npx playwright test, the first test failed. Below the error, the output shows the location of the trace file and the command to open it. (I replaced the working directory path with /path/to/app, and omitted the leading Running 2 tests using 1 worker line, the summary lines at the end, and the Call log and other lines in the middle.)

text
  ✘  1 tests/save.spec.ts:3:1 › タイトルを入れて保存できる (5.2s)
  ✓  2 tests/save.spec.ts:10:1 › ページの見出しが出る (143ms)


  1) tests/save.spec.ts:3:1 › タイトルを入れて保存できる ────────────────────────────────────────────────────────

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

    Locator:  locator('#status')
    Expected: "保存しました"
    Received: "保存できませんでした"
    Timeout:  5000ms
(中略)
        at /path/to/app/tests/save.spec.ts:7:41

    Error Context: test-results/save-タイトルを入れて保存できる/error-context.md

    attachment #2: trace (application/zip) ─────────────────────────────────────────────────────────
    test-results/save-タイトルを入れて保存できる/trace.zip
    Usage:

        npx playwright show-trace test-results/save-タイトルを入れて保存できる/trace.zip

When I listed the files in the test-results folder, trace.zip existed only in the folder for the failed test. The trace for the passing test had been deleted.

text
test-results/.last-run.json
test-results/save-タイトルを入れて保存できる/error-context.md
test-results/save-タイトルを入れて保存できる/trace.zip

The terminal error only tells you that the "could not save" message was shown in the field. It doesn't tell you why the save failed.

Other recording modes

According to the official documentation's table comparing trace modes, 'on' records and keeps the trace for every test, and 'on-first-retry' records only the first retry. For CI, the Tracing on CI section of the official documentation describes setting retries together with 'on-first-retry'.

You can also change the recording mode for a single run without editing the config file. When I ran with --trace on, a trace.zip was also created in the folder for the passing test.

text
test-results/.last-run.json
test-results/save-タイトルを入れて保存できる/error-context.md
test-results/save-タイトルを入れて保存できる/trace.zip
test-results/save-ページの見出しが出る/trace.zip

Conversely, when I ran with --trace off, no trace.zip was created even for the failed test, and only error-context.md remained.

Opening trace.zip in the Trace Viewer

As the test output showed, you open a trace by passing the path of trace.zip to npx playwright show-trace.

bash
npx playwright show-trace test-results/save-タイトルを入れて保存できる/trace.zip

This command opens the Trace Viewer window. In my environment, which has no display, it ended with the following error.

text
Error: Protocol error (Browser.getVersion): Internal server error, session closed.

When you pass --host and --port, the Trace Viewer starts as a small server so you can open it in a browser tab (according to the description in npx playwright show-trace --help). In my environment, I started it like this and opened http://127.0.0.1:9323 in Chromium.

bash
npx playwright show-trace --host 127.0.0.1 --port 9323 test-results/save-タイトルを入れて保存できる/trace.zip
text
Listening on http://127.0.0.1:9323

You can also open a trace from the HTML report. In my environment, I opened the report with npx playwright show-report --host 127.0.0.1 --port 9324. When I selected the failed test, the detail view had a "Traces" section, and its link opened the Trace Viewer.

Looking at the failed action and the page at that time

When the viewer opened, the failed action (here, Expect "toHaveText") was already selected. The list of Actions on the left shows the steps in the test in order. Opening the Errors tab at the bottom shows the same error as the terminal.

Trace Viewer screen. In the list on the left, the failed Expect "toHaveText" is selected in red. The center shows a snapshot of the page, with the "could not save" field outlined in blue. The Errors tab at the bottom shows the error with Expected: "Saved" and Received: "Could not save". The expected and received texts, and the page text, are in Japanese.
The failed action selected. The center shows the page at that time, and the bottom shows the error

The center shows a snapshot of the page at the time of that action. With the Action, Before, and After tabs at the top, you can switch between the page just before the action (Before) and just after it (After). According to the Snapshots section of the official documentation, an action snapshot marks the element that was acted on and the position where the click happened.

If I selected the Click action just before it in the list on the left and switched to After, the field right after the button press showed "Saving…" (the text is in Japanese). This shows that the save was still in progress when the button was pressed, and the field changed to the "could not save" message afterward.

Tracing the cause from the console and network

The Console and Network tabs at the bottom show what happened on the page.

The Console tab listed the 500 error message from the browser and the message the page script output with console.error().

Trace Viewer screen after selecting Click and switching to After. The field on the page in the center shows "Saving…" (the text is in Japanese). The Console tab at the bottom shows two red lines: "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" and "Save API error: 500" (the message is in Japanese).
The page right after Click, and the errors shown in the Console tab

The Network tab lists the requests the page sent during the test. The save row was red, with POST and 500 returned. Selecting the row and opening Response showed the response body {"error": "title is too long"}.

Trace Viewer Network tab with the save request selected. The list shows two rows, localhost:3000 and save. On the right, the Response shows {"error": "title is too long"}.
In the Network tab, the response returned by the save API

The terminal error only told me that the text in the field was different. The trace showed the full flow: the save API returned 500, and the page received that and displayed the "could not save" message. You can use this to decide whether to fix the test code or investigate the API.

The Call tab shows the time each action took and the locator it used. The Log tab shows the waits and operations that Playwright performed internally. The Trace Viewer features section of the official documentation describes each tab.

What's inside trace.zip

The Trace Viewer can show the contents of the communication because trace.zip contains that data. When I looked inside the trace.zip I created for this check, it contained the test source code (under src/), the screen recording (JPEG files under screencast/), and the request and response bodies (under resources/).

text
--- resources/1e9bcd81b2cb2867d976bdd00c03b44795d069d4.json
{"title":"トレースビューアーの使い方"}
--- resources/a133af0a64f472e4d14ae34838fe70b135b60ae4.json
{"error":"title is too long"}
Warning

Values entered in forms and API responses are saved in trace.zip as they are. If a test enters a password or token, anyone you give the trace.zip file to, or anyone who can access the location where you store it, can see those values too.

​Posted by 86