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 trace viewer
## Recording a trace
2023-12-13 21:03:29 -08:00
Traces can be recorded using the [`property: BrowserContext.tracing` ] API as follows:
< Tabs
groupId="test-runners"
2024-06-11 15:06:03 +02:00
defaultValue="mstest"
2023-12-13 21:03:29 -08:00
values={[
2024-06-11 15:06:03 +02:00
{label: 'MSTest', value: 'mstest'},
2023-12-13 21:03:29 -08:00
{label: 'NUnit', value: 'nunit'},
2024-11-25 10:09:35 +01:00
{label: 'xUnit', value: 'xunit'},
2023-12-13 21:03:29 -08:00
]
}>
< TabItem value = "nunit" >
2022-08-10 14:34:27 +02:00
```csharp
2023-12-13 21:03:29 -08:00
namespace PlaywrightTests;
2022-08-10 14:34:27 +02:00
2023-12-13 21:03:29 -08:00
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class Tests : PageTest
2022-08-10 14:34:27 +02:00
{
2023-12-13 21:03:29 -08:00
[SetUp]
public async Task Setup()
{
await Context.Tracing.StartAsync(new()
{
2024-05-08 21:01:47 +02:00
Title = $"{TestContext.CurrentContext.Test.ClassName}.{TestContext.CurrentContext.Test.Name}",
2023-12-13 21:03:29 -08:00
Screenshots = true,
Snapshots = true,
Sources = true
});
}
[TearDown]
public async Task TearDown()
{
await Context.Tracing.StopAsync(new()
{
Path = Path.Combine(
TestContext.CurrentContext.WorkDirectory,
"playwright-traces",
$"{TestContext.CurrentContext.Test.ClassName}.{TestContext.CurrentContext.Test.Name}.zip"
)
});
}
[Test]
2024-05-09 20:34:01 +02:00
public async Task GetStartedLink()
2023-12-13 21:03:29 -08:00
{
// ..
}
}
```
2022-08-10 14:34:27 +02:00
2023-12-13 21:03:29 -08:00
< / TabItem >
< TabItem value = "mstest" >
2022-08-10 14:34:27 +02:00
2023-12-13 21:03:29 -08:00
```csharp
using System.Text.RegularExpressions;
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;
2024-05-02 12:06:39 +02:00
namespace PlaywrightTests;
2023-12-13 21:03:29 -08:00
[TestClass]
2024-05-02 12:06:39 +02:00
public class ExampleTest : PageTest
2022-08-10 14:34:27 +02:00
{
2023-12-13 21:03:29 -08:00
[TestInitialize]
public async Task TestInitialize()
{
await Context.Tracing.StartAsync(new()
{
2024-05-08 21:01:47 +02:00
Title = $"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}",
2023-12-13 21:03:29 -08:00
Screenshots = true,
Snapshots = true,
Sources = true
});
}
[TestCleanup]
public async Task TestCleanup()
{
await Context.Tracing.StopAsync(new()
{
Path = Path.Combine(
Environment.CurrentDirectory,
"playwright-traces",
2024-05-08 21:01:47 +02:00
$"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}.zip"
2023-12-13 21:03:29 -08:00
)
});
}
[TestMethod]
2024-05-02 12:06:39 +02:00
public async Task GetStartedLink()
2023-12-13 21:03:29 -08:00
{
// ...
}
}
2022-08-10 14:34:27 +02:00
```
2024-11-25 10:09:35 +01:00
< / TabItem >
< TabItem value = "xunit" >
```csharp
using System.Reflection;
using Microsoft.Playwright;
using Microsoft.Playwright.Xunit;
using Xunit.Sdk;
namespace PlaywrightTests;
[WithTestName]
public class UnitTest1 : PageTest
{
public override async Task InitializeAsync()
{
await base.InitializeAsync().ConfigureAwait(false);
await Context.Tracing.StartAsync(new()
{
Title = $"{WithTestNameAttribute.CurrentClassName}.{WithTestNameAttribute.CurrentTestName}",
Screenshots = true,
Snapshots = true,
Sources = true
});
}
public override async Task DisposeAsync()
{
await Context.Tracing.StopAsync(new()
{
Path = Path.Combine(
Environment.CurrentDirectory,
"playwright-traces",
$"{WithTestNameAttribute.CurrentClassName}.{WithTestNameAttribute.CurrentTestName}.zip"
)
});
await base.DisposeAsync().ConfigureAwait(false);
}
[Fact]
public async Task GetStartedLink()
{
// ...
await Page.GotoAsync("https://playwright.dev/dotnet/docs/intro");
}
}
public class WithTestNameAttribute : BeforeAfterTestAttribute
{
public static string CurrentTestName = string.Empty;
public static string CurrentClassName = string.Empty;
public override void Before(MethodInfo methodInfo)
{
CurrentTestName = methodInfo.Name;
CurrentClassName = methodInfo.DeclaringType!.Name;
}
public override void After(MethodInfo methodInfo)
{
}
}
```
2023-12-13 21:03:29 -08:00
< / TabItem >
< / Tabs >
2024-05-02 12:06:39 +02:00
This will record a zip file for each test, e.g. `PlaywrightTests.ExampleTest.GetStartedLink.zip` and place it into the `bin/Debug/net8.0/playwright-traces/` directory.
2022-08-10 14:34:27 +02:00
## Opening the trace
2024-05-02 12:06:39 +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's zip file is located. Once opened you can click on each action or use the timeline to see the state of the page before and after each action. You can also 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.
2022-08-10 14:34:27 +02:00
```bash csharp
2024-05-02 12:06:39 +02:00
pwsh bin/Debug/net8.0/playwright.ps1 show-trace bin/Debug/net8.0/playwright-traces/PlaywrightTests.ExampleTest.GetStartedLink.zip
2022-08-10 14:34:27 +02:00
```
2024-05-08 21:01:47 +02:00

2022-08-10 14:34:27 +02:00
2024-05-09 20:34:01 +02:00
Check out our detailed guide on [Trace Viewer ](/trace-viewer.md ) to learn more about the trace viewer and how to setup your tests to record a trace only when the test fails.
2023-09-22 16:57:02 +02:00
## What's next
- [Run tests on CI with GitHub Actions ](/ci-intro.md )
2024-11-25 10:09:35 +01:00
- [Learn more about the MSTest, NUnit, and xUnit base classes ](./test-runners.md )