Playwright Trace Viewer: Complete Guide to Recording, Opening and Debugging Tests
Your Playwright test passed yesterday.
Today it fails.
You look at the error:
TimeoutError
But that message does not always tell you the complete story.
Did the page load slowly?
Was the button hidden?
Did the page navigate somewhere unexpected?
Did the DOM change?
Did an API request fail?
Was an element covered by another element?
This is where Playwright Trace Viewer becomes extremely useful.
Playwright Trace Viewer is a visual debugging tool that lets you inspect what happened during a Playwright test. You can move through the test actions and inspect information such as snapshots, source code, logs, errors, console messages and network activity.
What Is Playwright Trace Viewer?
Playwright Trace Viewer is a GUI tool for examining recorded Playwright traces.
A trace is like a detailed replay of a test.
Instead of seeing only:
Test failed
you can inspect what happened around the failure.
Think of it like a security camera for your automated test.
The camera does not simply tell you:
“Something went wrong.”
It helps you see what happened before, during and after the problem.
Why Use Playwright Trace Viewer?
Trace Viewer is especially useful when:
A test fails only sometimes.
A test passes locally but fails in CI.
A page behaves differently during automation.
An element cannot be found.
A click times out.
Navigation behaves unexpectedly.
You need to inspect the DOM at a particular moment.
You need more information than the terminal error provides.
Playwright's documentation specifically recommends traces as a debugging tool, particularly for failures in CI.
What Can You See in Trace Viewer?
Depending on what was captured, Trace Viewer can help you inspect:
Test actions
Action timing
DOM snapshots
Screenshots
Source code
Console messages
Network activity
Errors
Test logs
This combination is what makes a trace much more useful than a simple text error.
How to Record a Playwright Trace
For local debugging, you can run:
npx playwright test --trace on
This tells Playwright to record a trace for the test run.
After the test finishes, open the HTML report:
npx playwright show-report
From the report, you can open the trace for the test.
How to Open a Trace File Directly
If you already have a trace file such as:
trace.zip
you can open it with:
npx playwright show-trace path/to/trace.zip
For example:
npx playwright show-trace test-results/example-test/trace.zip
The official Trace Viewer can also be accessed through the browser.
How to Use Trace Viewer in the Browser
Playwright provides a web-based Trace Viewer.
You can open:
Then load your trace file.
The official documentation states that the trace is loaded in the browser and is not transmitted externally by the Trace Viewer web application.
This can be useful when you want to inspect a trace without opening the local HTML report.
The Actions Tab
The Actions area is one of the most useful parts of Trace Viewer.
You can see actions performed during the test.
For example:
goto()
fill()
click()
expect()
Selecting an action can show more information about what happened at that point.
You can move through the timeline to understand where the test started behaving differently.
DOM Snapshots
Trace Viewer can provide DOM snapshots around actions.
Imagine your test says:
await page.getByRole('button', {
name: 'Submit'
}).click();
The click fails.
Instead of guessing, you can inspect the page state around that action.
Was the button present?
Was the page still loading?
Did the application render a different component?
Snapshots can help answer these questions.
Screenshots in Trace Viewer
Screenshots provide visual evidence of the page state.
This is extremely useful when debugging:
overlays
popups
unexpected redirects
loading screens
responsive layouts
missing buttons
incorrect page states
The timeline lets you move through actions and inspect what the browser looked like during the test.
Source Code in Trace Viewer
Trace Viewer can also connect an action to its source code.
Suppose your test contains:
await page.getByRole('button', {
name: 'Login'
}).click();
If that action fails, you can inspect the corresponding source location.
This saves time because you do not have to manually search through a large test suite.
The Log
The log helps explain what Playwright was doing during an action.
For example, when performing an interaction, Playwright may need to:
wait for an element
scroll it into view
check whether it is actionable
perform the action
Trace information can help you understand this sequence.
Console Information
Browser console errors can sometimes explain why a test failed.
For example, your test may fail because the application itself produced a JavaScript error.
Trace Viewer allows you to inspect console information captured in the trace.
This can help you separate:
Test automation problem
from:
Application problem
Network Information
Sometimes the UI looks fine, but an API request fails.
For example:
GET /api/products
→ 500 Internal Server Error
Your Playwright test may then fail because the products never appear.
Network information in the trace can help you investigate this type of problem.
BrowserStack also highlights network information as an important part of diagnosing Playwright failures.
How to Enable Trace on First Retry
For CI environments, recording every trace can create unnecessary overhead.
A common configuration is:
import { defineConfig } from '@playwright/test';
export default defineConfig({
retries: 1,
use: {
trace: 'on-first-retry',
},
});
This records a trace when a test is retried for the first time.
Playwright supports trace modes including:
on
off
on-first-retry
on-all-retries
retain-on-failure
The official documentation recommends on-first-retry as a useful CI debugging strategy.
Which Trace Mode Should You Use?
Local development
Try:
--trace on
when you specifically need to investigate a failure.
CI
A good starting point is:
trace: 'on-first-retry'
This gives you debugging information without recording a trace for every successful test.
Every test
You can use:
trace: 'on'
but this can add overhead and produce many trace files.
The official documentation warns that recording traces for every test can be performance-heavy.
Playwright Trace Viewer vs Screenshot
A screenshot answers:
“What did the page look like?”
A trace can answer much more:
“What happened during the test?”
A trace can connect the visual state with actions, DOM snapshots, source code, logs and other debugging information.
That makes Trace Viewer much more useful for complicated automation failures.
Playwright Trace Viewer vs HTML Report
They are different tools.
The HTML report gives you an overview of test results.
Trace Viewer gives you a deeper view into an individual test execution.
A useful workflow is:
Run tests
↓
Test fails
↓
Open HTML report
↓
Open failed test
↓
Open trace
↓
Investigate failure
Example: Debugging a Failed Login Test
Suppose you have:
test('user can login', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email').fill('user@example.com');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', {
name: 'Login'
}).click();
await expect(page).toHaveURL(/dashboard/);
});
The test fails at:
await expect(page).toHaveURL(/dashboard/);
Do not immediately increase the timeout.
Open the trace.
Check:
What URL did the page actually have?
Did the Login button receive the click?
Was an error shown?
Did the network request fail?
Was there a validation message?
Did the application redirect somewhere else?
Did the DOM change?
Now you have evidence instead of guessing.
How to Debug a Flaky Playwright Test
Suppose a test passes:
9 times
but fails:
1 time
This is a classic flaky-test situation.
Trace Viewer can help you compare the successful and failed runs.
Look for:
different network timing
different page state
missing elements
unexpected redirects
race conditions
application errors
different browser states
This is one reason traces are valuable in CI.
Important: Do Not Use Traces as a Replacement for Good Tests
Trace Viewer is a debugging tool.
It should not be used to hide bad test design.
For example, if your test constantly depends on arbitrary waits such as:
await page.waitForTimeout(5000);
the correct solution may be to improve the test rather than simply inspect more traces.
Use reliable locators, web-first assertions and Playwright's built-in waiting behavior wherever appropriate.
Trace Viewer Best Practices
1. Use traces mainly when debugging
Do not automatically record every trace unless you have a reason.
2. Prefer on-first-retry in CI
This gives useful debugging information when failures happen.
3. Inspect the timeline
Start from the failure and move backward.
4. Check the DOM snapshot
Ask:
“Was the element actually available?”
5. Check network activity
The UI may fail because an API failed.
6. Check console errors
Application JavaScript errors can create misleading test failures.
7. Compare successful and failed runs
This is particularly useful for flaky tests.
Common Playwright Trace Viewer Questions
What is Playwright Trace Viewer?
It is a visual debugging tool for inspecting recorded Playwright test traces.
How do I open a Playwright trace?
Run:
npx playwright show-trace path/to/trace.zip
How do I record a trace?
Run:
npx playwright test --trace on
How do I enable traces in CI?
A common configuration is:
use: {
trace: 'on-first-retry'
}
What does a Playwright trace contain?
Depending on the configuration, it can contain screenshots, DOM snapshots, actions, source information, logs, console data, network information and errors.
Can I view Playwright traces in a browser?
Yes. Playwright provides a browser-based Trace Viewer at trace.playwright.dev.
Final Takeaway
When a Playwright test fails, the terminal error is only the beginning.
Trace Viewer lets you investigate the test execution visually.
The basic workflow is:
Record trace
↓
Open trace
↓
Find failed action
↓
Inspect snapshot
↓
Check source
↓
Check logs
↓
Check console/network
↓
Fix the real problem
If you regularly work with Playwright in CI/CD, learning Trace Viewer is not optional knowledge.
It is one of the most useful skills for finding out why an automated test failed instead of simply knowing that it failed.
Comments
Post a Comment