Trace Viewer
Relevant source files
The following files were used as context for generating this wiki page:
- tests/library/trace-viewer.spec.ts
- packages/trace-viewer/src/ui/workbench.tsx
- tests/library/tracing.spec.ts
- packages/playwright/src/reporters/html.ts
- packages/trace-viewer/index.html
- tests/playwright-test/reporter-html.spec.ts
- tests/playwright-test/playwright.trace.spec.ts
- packages/web/src/components/codeMirrorWrapper.spec.tsx
The Playwright Trace Viewer is a powerful GUI tool that provides a comprehensive, post-mortem view of test executions. It allows developers to time-travel through each action of a test, inspecting DOM snapshots, network requests, console logs, and source code. This capability is crucial for debugging test failures and eliminating flakiness by providing a complete and interactive record of what happened during the run.
The Trace Viewer can be opened via the npx playwright show-trace command, or directly from the HTML report generated by the html reporter. It is designed to work with trace files (.zip) generated by Playwright's tracing API. The viewer is a web application served by a local server, ensuring it can handle large trace files and render complex DOM snapshots efficiently.
Architecture
The Trace Viewer operates on a client-server model. When a user initiates a command like show-trace or show-report, a local HTTP server is started. This server is responsible for serving the Trace Viewer's static assets (HTML, CSS, JS) and the trace data itself. The frontend is a React-based single-page application that loads and renders the trace information.
Sources: packages/playwright/src/reporters/html.ts:200-213, packages/trace-viewer/index.html:1-20
System Flow
The following diagram illustrates the process of launching and using the Trace Viewer.
This diagram shows the flow from the user running the CLI command to the Trace Viewer UI being rendered in the browser.
Sources: packages/playwright/src/reporters/html.ts:200-213, tests/library/trace-viewer.spec.ts:101-104
Trace Generation
Traces are generated using the tracing API on a BrowserContext. The process involves starting the trace recording, performing actions, and then stopping the recording, which saves the trace to a .zip file.
Key options for tracing.start():
| Option | Description |
|---|---|
name | A name for the trace, used for trace chunks. |
screenshots | A boolean to enable capturing screenshots for each action. |
snapshots | A boolean to enable capturing a full DOM snapshot for each action. |
sources | A boolean to include the source code of the test scripts in the trace. |
Sources: tests/library/tracing.spec.ts:20-31, tests/playwright-test/playwright.trace.spec.ts:103-110
A typical tracing sequence in a test looks like this:
// tests/library/trace-viewer.spec.ts:40-77
test.beforeAll(async function recordTrace({ browser, browserName, browserType, server }, workerInfo) {
const context = await browser.newContext({
baseURL: 'https://example.com',
});
await context.tracing.start({ name: 'test', screenshots: true, snapshots: true, sources: true });
const page = await context.newPage();
// ... perform actions ...
await page.goto(`data:text/html,<!DOCTYPE html><html><title>Hello</title><body>Hello world</body></html>`);
await page.getByText('Click').click();
// ...
traceFile = path.join(workerInfo.project.outputDir, String(workerInfo.workerIndex), browserName, 'trace.zip');
await context.tracing.stop({ path: traceFile });
});
Sources: tests/library/trace-viewer.spec.ts:40-77
User Interface Components
The Trace Viewer UI is built with React and organized into a Workbench component that houses several distinct panels for different types of information.
Sources: packages/trace-viewer/src/ui/workbench.tsx
The main UI components are:
- Actions List: A timeline of all actions performed during the test.
- Snapshot Viewer: Renders the DOM before and after each action.
- Tabbed Views: A set of tabs for viewing Metadata, Logs, Console, Network, and Source code.
This diagram shows the primary components of the Trace Viewer's workbench UI.
Sources: packages/trace-viewer/src/ui/workbench.tsx
Actions Panel
This panel displays a hierarchical list of all actions executed during the test, such as page.goto(), locator.click(), and expect(). Selecting an action updates all other panels to show the state of the application at that specific moment. It also shows the duration of each action.
- Action Groups: The viewer can group related actions, such as those within a
tracing.group()block. - Action Highlighting: Hovering over an action in the list can highlight the corresponding source code line.
Sources: tests/library/trace-viewer.spec.ts:130-150, tests/library/trace-viewer.spec.ts:172-191
Snapshot Viewer
The Snapshot Viewer is the main visual component, rendering an interactive DOM snapshot for each step.
- Before/After Snapshots: For each action, you can view the state of the DOM immediately before and after the action was performed.
- Target Highlighting: When an action is selected, the element it targeted is highlighted with a colored overlay.
- Locator Picking: Users can click on any element in the snapshot, and the Trace Viewer will generate a recommended Playwright locator for it. This is a powerful feature for writing and debugging selectors.
- Pop-out: Snapshots can be opened in a separate browser tab for a larger view.
Sources: tests/library/trace-viewer.spec.ts:1072-1080, tests/library/trace-viewer.spec.ts:1473-1480
Tabbed Views
Metadata
This tab shows metadata about the entire trace, including:
- Start time and duration
- Browser engine and platform
- Viewport dimensions
- Number of pages, actions, and events
baseURLif configured
Sources: tests/library/trace-viewer.spec.ts:1229-1240, tests/library/trace-viewer.spec.ts:2009-2014
Console
Displays all console messages from the browser, categorized by type (Info, Warning, Error). It also shows the source location of the console message if available. Unhandled exceptions are also captured and displayed here.
Sources: tests/library/trace-viewer.spec.ts:285-322
Network
The Network tab provides a detailed log of all network requests made during the test.
- Filtering: Requests can be filtered by resource type (HTML, CSS, JS, Fetch, etc.) and by URL.
- Request Details: Selecting a request shows its headers, body, and response. For certain types like fonts, a preview is rendered.
- Status Highlighting: Failed requests (e.g., 404) or aborted requests are highlighted for easy identification.
Sources: tests/library/trace-viewer.spec.ts:404-486
Source
When an action is selected, the Source tab displays the test's source code.
- Line Highlighting: The specific line of code that triggered the action is highlighted.
- Stack Trace: A call stack is provided, showing the path from the test function to the Playwright API call.
- Code Highlighting: The code is syntax-highlighted for readability, supporting JavaScript, Python, Java, and C#.
Sources: tests/library/trace-viewer.spec.ts:1181-1193, packages/web/src/components/codeMirrorWrapper.spec.tsx:23-78
Advanced Features
Multi-Trace Viewing
The Trace Viewer can open and merge multiple trace files from the same or different test runs. This is useful for comparing behaviors or analyzing distributed tests. When multiple traces are loaded, the actions from all traces are merged into a single, chronological timeline.
Sources: tests/library/trace-viewer.spec.ts:1259-1300
Settings
A settings dialog allows users to customize the viewer experience:
- Theme: Toggle between light and dark mode.
- Canvas Rendering: Enable or disable rendering of
<canvas>elements in snapshots. This is off by default for performance reasons.
Sources: tests/library/trace-viewer.spec.ts:1920-1940, tests/library/trace-viewer.spec.ts:1942-1964
HTML Report Integration
The Trace Viewer is seamlessly integrated into the Playwright HTML reporter. Each test in the report that has a trace includes a "View Trace" button or a clickable screenshot that opens the viewer directly, pre-loaded with the corresponding trace file. This integration provides a powerful one-click debugging workflow.
Sources: packages/playwright/src/reporters/html.ts:413-424, tests/playwright-test/reporter-html.spec.ts:327-341
A warning is displayed if a user attempts to open the trace viewer from a local file:// URL, as it requires an HTTP server to function correctly. The warning provides the correct npx playwright show-report command to use.
Sources: packages/trace-viewer/index.html:43-59, tests/playwright-test/reporter-html.spec.ts:608-622