mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
227 lines
6.0 KiB
Markdown
227 lines
6.0 KiB
Markdown
---
|
|
id: trace-viewer-intro
|
|
title: "Trace viewer"
|
|
---
|
|
|
|
## Introduction
|
|
|
|
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
|
|
* langs: python
|
|
|
|
Traces can be recorded by running your tests with the `--tracing` flag.
|
|
|
|
```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.
|
|
|
|
This will record the trace and place it into the file named `trace.zip` in your `test-results` directory.
|
|
|
|
<details>
|
|
<summary>If you are not using Pytest, click here to learn how to record traces.</summary>
|
|
|
|
```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)
|
|
|
|
page = await context.new_page()
|
|
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)
|
|
|
|
page = context.new_page()
|
|
page.goto("https://playwright.dev")
|
|
|
|
# Stop tracing and export it into a zip archive.
|
|
context.tracing.stop(path = "trace.zip")
|
|
```
|
|
|
|
</details>
|
|
|
|
## Recording a trace
|
|
* langs: 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")));
|
|
```
|
|
|
|
|
|
This will record the trace and place it into the file named `trace.zip`.
|
|
|
|
## Recording a trace
|
|
* langs: csharp
|
|
|
|
Traces can be recorded using the [`property: BrowserContext.tracing`] API as follows:
|
|
|
|
<Tabs
|
|
groupId="test-runners"
|
|
defaultValue="nunit"
|
|
values={[
|
|
{label: 'NUnit', value: 'nunit'},
|
|
{label: 'MSTest', value: 'mstest'}
|
|
]
|
|
}>
|
|
<TabItem value="nunit">
|
|
|
|
```csharp
|
|
namespace PlaywrightTests;
|
|
|
|
[Parallelizable(ParallelScope.Self)]
|
|
[TestFixture]
|
|
public class Tests : PageTest
|
|
{
|
|
[SetUp]
|
|
public async Task Setup()
|
|
{
|
|
await Context.Tracing.StartAsync(new()
|
|
{
|
|
Title = TestContext.CurrentContext.Test.ClassName + "." + TestContext.CurrentContext.Test.Name,
|
|
Screenshots = true,
|
|
Snapshots = true,
|
|
Sources = true
|
|
});
|
|
}
|
|
|
|
[TearDown]
|
|
public async Task TearDown()
|
|
{
|
|
// This will produce e.g.:
|
|
// bin/Debug/net8.0/playwright-traces/PlaywrightTests.Tests.Test1.zip
|
|
await Context.Tracing.StopAsync(new()
|
|
{
|
|
Path = Path.Combine(
|
|
TestContext.CurrentContext.WorkDirectory,
|
|
"playwright-traces",
|
|
$"{TestContext.CurrentContext.Test.ClassName}.{TestContext.CurrentContext.Test.Name}.zip"
|
|
)
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task TestYourOnlineShop()
|
|
{
|
|
// ..
|
|
}
|
|
}
|
|
```
|
|
|
|
</TabItem>
|
|
<TabItem value="mstest">
|
|
|
|
```csharp
|
|
using System.Text.RegularExpressions;
|
|
using Microsoft.Playwright;
|
|
using Microsoft.Playwright.MSTest;
|
|
|
|
namespace PlaywrightTestsMSTest;
|
|
|
|
[TestClass]
|
|
public class UnitTest1 : PageTest
|
|
{
|
|
[TestInitialize]
|
|
public async Task TestInitialize()
|
|
{
|
|
await Context.Tracing.StartAsync(new()
|
|
{
|
|
Title = TestContext.TestName,
|
|
Screenshots = true,
|
|
Snapshots = true,
|
|
Sources = true
|
|
});
|
|
}
|
|
|
|
[TestCleanup]
|
|
public async Task TestCleanup()
|
|
{
|
|
// This will produce e.g.:
|
|
// bin/Debug/net8.0/playwright-traces/PlaywrightTests.UnitTest1.zip
|
|
await Context.Tracing.StopAsync(new()
|
|
{
|
|
Path = Path.Combine(
|
|
Environment.CurrentDirectory,
|
|
"playwright-traces",
|
|
$"{TestContext.FullyQualifiedTestClassName}.zip"
|
|
)
|
|
});
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TestYourOnlineShop()
|
|
{
|
|
// ...
|
|
}
|
|
}
|
|
```
|
|
|
|
</TabItem>
|
|
</Tabs>
|
|
|
|
This will record the trace and place it into the `bin/Debug/net8.0/playwright-traces/` directory.
|
|
|
|
## Opening the trace
|
|
|
|
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`.
|
|
|
|
```bash java
|
|
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="show-trace trace.zip"
|
|
```
|
|
|
|
```bash python
|
|
playwright show-trace trace.zip
|
|
```
|
|
|
|
```bash csharp
|
|
pwsh bin/Debug/netX/playwright.ps1 show-trace trace.zip
|
|
```
|
|
## 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.
|
|
|
|

|
|
|
|
To learn more check out our detailed guide on [Trace Viewer](/trace-viewer.md).
|
|
|
|
## What's next
|
|
|
|
- [Run tests on CI with GitHub Actions](/ci-intro.md)
|
|
- [Learn more about Trace Viewer](/trace-viewer.md)
|