2022-08-10 14:34:27 +02:00
---
id: trace-viewer-intro
2023-03-06 20:52:58 +01:00
title: "Trace viewer"
2022-08-10 14:34:27 +02:00
---
2023-09-22 16:57:02 +02:00
## Introduction
2022-08-10 14:34:27 +02:00
Playwright Trace Viewer is a GUI tool that lets you explore recorded Playwright traces of your tests meaning you can go back and forward though each action of your test and visually see what was happening during each action.
**You will learn**
- How to record a trace
- How to open the HTML report
- How to open the trace viewer
## Recording a trace
2023-09-22 16:57:02 +02:00
* langs: python
2022-08-10 14:34:27 +02:00
2023-09-22 16:57:02 +02:00
Traces can be recorded by running your tests with the `--tracing` flag.
2022-08-10 14:34:27 +02:00
2023-09-22 16:57:02 +02:00
```bash
pytest --tracing on
```
Options for tracing are:
- `on` : Record trace for each test
- `off` : Do not record trace. (default)
- `retain-on-failure` : Record trace for each test, but remove all traces from successful test runs.
2022-08-10 14:34:27 +02:00
2023-09-22 16:57:02 +02:00
This will record the trace and place it into the file named `trace.zip` in your `test-results` directory.
2022-08-10 14:34:27 +02:00
2023-11-22 19:40:23 +01:00
< details >
< summary > If you are not using Pytest, click here to learn how to record traces.< / summary >
2022-08-10 14:34:27 +02:00
```python async
browser = await chromium.launch()
context = await browser.new_context()
# Start tracing before creating / navigating a page.
await context.tracing.start(screenshots=True, snapshots=True, sources=True)
2022-11-08 14:22:14 -08:00
page = await context.new_page()
2022-08-10 14:34:27 +02:00
await page.goto("https://playwright.dev")
# Stop tracing and export it into a zip archive.
await context.tracing.stop(path = "trace.zip")
```
```python sync
browser = chromium.launch()
context = browser.new_context()
# Start tracing before creating / navigating a page.
context.tracing.start(screenshots=True, snapshots=True, sources=True)
2022-11-08 14:22:14 -08:00
page = context.new_page()
2022-08-10 14:34:27 +02:00
page.goto("https://playwright.dev")
# Stop tracing and export it into a zip archive.
context.tracing.stop(path = "trace.zip")
```
2023-09-22 16:57:02 +02:00
< / details >
## Recording a trace
* langs: csharp, java
Traces can be recorded using the [`property: BrowserContext.tracing` ] API as follows:
```java
Browser browser = browserType.launch();
BrowserContext context = browser.newContext();
// Start tracing before creating / navigating a page.
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true)
.setSnapshots(true)
.setSources(true));
Page page = context.newPage();
page.navigate("https://playwright.dev");
// Stop tracing and export it into a zip archive.
context.tracing().stop(new Tracing.StopOptions()
.setPath(Paths.get("trace.zip")));
```
2022-08-10 14:34:27 +02:00
```csharp
2023-12-05 19:19:12 +02:00
using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync();
2022-08-10 14:34:27 +02:00
await using var context = await browser.NewContextAsync();
// Start tracing before creating / navigating a page.
await context.Tracing.StartAsync(new()
{
2023-04-26 22:20:45 +02:00
Screenshots = true,
Snapshots = true,
Sources = true
2022-08-10 14:34:27 +02:00
});
2023-04-26 22:20:45 +02:00
var page = await context.NewPageAsync();
2022-08-10 14:34:27 +02:00
await page.GotoAsync("https://playwright.dev");
// Stop tracing and export it into a zip archive.
await context.Tracing.StopAsync(new()
{
2023-04-26 22:20:45 +02:00
Path = "trace.zip"
2022-08-10 14:34:27 +02:00
});
```
This will record the trace and place it into the file named `trace.zip` .
## Opening the trace
2023-09-22 16:57:02 +02:00
You can open the saved trace using the Playwright CLI or in your browser on [`trace.playwright.dev` ](https://trace.playwright.dev ). Make sure to add the full path to where your `trace.zip` file is located. This should include the `test-results` directory followed by the test name and then `trace.zip` .
2022-08-10 14:34:27 +02:00
```bash java
2023-01-05 19:55:07 +01:00
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="show-trace trace.zip"
2022-08-10 14:34:27 +02:00
```
```bash python
playwright show-trace trace.zip
```
```bash csharp
2022-08-14 20:01:00 +02:00
pwsh bin/Debug/netX/playwright.ps1 show-trace trace.zip
2022-08-10 14:34:27 +02:00
```
## Viewing the trace
View traces of your test by clicking through each action or hovering using the timeline and see the state of the page before and after the action. Inspect the log, source and network during each step of the test. The trace viewer creates a DOM snapshot so you can fully interact with it, open devtools etc.
2023-09-22 16:57:02 +02:00

2022-08-10 14:34:27 +02:00
To learn more check out our detailed guide on [Trace Viewer ](/trace-viewer.md ).
2023-09-22 16:57:02 +02:00
## What's next
- [Run tests on CI with GitHub Actions ](/ci-intro.md )
- [Learn more about Trace Viewer ](/trace-viewer.md )