In this example, we will explore what tracing is in Playwright, how to set it up, and the benefits it offers. Playwright Trace Viewer is a powerful tool provided by Microsoft's Playwright that allows you to visually inspect and debug your end-to-end test runs.
It provides a graphical interface to go through each step of a test execution, including:
- Snapshots of the page at each action
- Network requests and responses
- Console logs
- DOM state and action timeline
- Source code with the exact step highlighted
What is Tracing?
In the context of Playwright, tracing refers to the process of recording a detailed log of a test’s execution. This log captures various elements such as:
- User actions (e.g., clicks, navigation)
- Screenshots at specific points during the test
- Console messages generated throughout the execution
This comprehensive logging is particularly useful for debugging, especially when tests are run in headless mode on Continuous Integration (CI) platforms.
Setting Up Tracing in Playwright
To enable tracing in your Playwright tests, you need to configure your project appropriately. Here’s how to do it:
Modify the Configuration Add the following code snippet into playwright.config.js to set up tracing options:
import { defineConfig } from '@playwright/test'; export default defineConfig({ use: { trace: 'on-first-retry', // Options include 'off', 'on', 'retain-on-failure', etc. }, });
This setup records a trace only on the first retry of a failed test, which helps manage storage while providing valuable debugging information when necessary.
Available options to record a trace:
- 'on-first-retry' - Record a trace only when retrying a test for the first time.
- 'on-all-retries' - Record traces for all test retries.
- 'off' - Do not record a trace.
- 'on' - Record a trace for each test. (not recommended as it's performance heavy)
- 'retain-on-failure' - Record a trace for each test, but remove it from successful test runs.
Running Tests with Tracing: To run your tests with tracing enabled, you can use the command:
npx playwright test --trace on OR npx playwright test hooks.spec.js --project=chromium --headed --workers 1 --trace on
Using Trace Viewer
Once you have recorded traces during your tests, you can utilize the Trace Viewer, a GUI tool that helps visualize these traces. Here’s how to access and use it:
1. Generate an HTML Report: After running your tests, generate an HTML report using:
npx playwright show-report2. Open Traces: In the HTML report, click on the trace icon next to a test name to open its trace. Alternatively, navigate to the detailed view of a test and find the ‘Traces’ tab. Please take a look in below example.
Trace Viewer features

import { test } from '@playwright/test'; test('my test with tracing', async ({ page, context }) => { await context.tracing.start({ screenshots: true, snapshots: true }); await page.goto('https://example.com'); await page.click('text=More information'); await context.tracing.stop({ path: 'trace.zip' }); });
npx playwright show-trace trace.zip
This will launch a local viewer in your browser.
- Enable Tracing in CI: Use trace: 'on-first-retry' to capture traces only for flaky or failed tests, saving storage.
- Robust Locators: Use data-testid, role-based, or text-based locators for reliability, as Trace Viewer highlights selector issues.
- Screenshots and Snapshots: Always enable both for visual debugging (default in most setups).
- UI Mode: For local development, use npx playwright test --ui to automatically trace tests and view them interactively.
- Remote Traces: Store traces in CI (e.g., S3) and access via URL for team collaboration.
- Storage: Traces with screenshots and snapshots can be large; use on-first-retry or retain-on-failure to manage space.
- CORS: Remote trace URLs on trace.playwright.dev may face CORS restrictions.
- Local Requirement: Must serve traces over http:// or https:// for trace.playwright.dev.
No comments:
Post a Comment