2021-05-12 12:21:54 -07:00
# class: Tracing
2022-07-05 16:24:50 -08:00
* since: v1.12
2021-05-12 12:21:54 -07:00
2022-03-25 19:30:45 +01:00
API for collecting and saving Playwright traces. Playwright traces can be opened in [Trace Viewer ](../trace-viewer.md ) after Playwright script runs.
2021-05-12 16:16:36 -07:00
2021-08-31 17:03:31 -07:00
Start recording a trace before performing actions. At the end, stop tracing and save it to a file.
2021-05-12 16:16:36 -07:00
```js
2021-06-02 22:00:34 -07:00
const browser = await chromium.launch();
2021-05-12 16:16:36 -07:00
const context = await browser.newContext();
2021-06-02 22:00:34 -07:00
await context.tracing.start({ screenshots: true, snapshots: true });
2021-05-12 16:16:36 -07:00
const page = await context.newPage();
await page.goto('https://playwright.dev');
2024-09-17 15:32:30 +02:00
await context.tracing.stop({ path: 'trace.zip' });
2021-05-12 16:16:36 -07:00
```
```java
2021-06-02 22:00:34 -07:00
Browser browser = chromium.launch();
2021-05-12 16:16:36 -07:00
BrowserContext context = browser.newContext();
2021-06-15 11:14:09 -07:00
context.tracing().start(new Tracing.StartOptions()
2021-05-12 16:16:36 -07:00
.setScreenshots(true)
2021-06-15 11:14:09 -07:00
.setSnapshots(true));
2021-05-12 16:16:36 -07:00
Page page = context.newPage();
2021-06-15 11:14:09 -07:00
page.navigate("https://playwright.dev");
context.tracing().stop(new Tracing.StopOptions()
2024-09-17 15:32:30 +02:00
.setPath(Paths.get("trace.zip")));
2021-05-12 16:16:36 -07:00
```
```python async
2021-06-02 22:00:34 -07:00
browser = await chromium.launch()
2021-05-12 16:16:36 -07:00
context = await browser.new_context()
2021-06-02 22:00:34 -07:00
await context.tracing.start(screenshots=True, snapshots=True)
2021-08-31 17:03:31 -07:00
page = await context.new_page()
2021-05-12 16:16:36 -07:00
await page.goto("https://playwright.dev")
2024-09-17 15:32:30 +02:00
await context.tracing.stop(path = "trace.zip")
2021-05-12 16:16:36 -07:00
```
```python sync
2021-06-02 22:00:34 -07:00
browser = chromium.launch()
2021-05-12 16:16:36 -07:00
context = browser.new_context()
2021-06-02 22:00:34 -07:00
context.tracing.start(screenshots=True, snapshots=True)
2021-08-31 17:03:31 -07:00
page = context.new_page()
2021-05-12 16:16:36 -07:00
page.goto("https://playwright.dev")
2024-09-17 15:32:30 +02:00
context.tracing.stop(path = "trace.zip")
2021-05-12 16:16:36 -07:00
```
2021-05-12 12:21:54 -07:00
2021-06-03 08:08:05 -07:00
```csharp
2023-12-05 19:19:12 +02:00
using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync();
2021-06-03 08:08:05 -07:00
await using var context = await browser.NewContextAsync();
2022-07-13 11:50:18 +02:00
await context.Tracing.StartAsync(new()
2021-06-03 08:08:05 -07:00
{
2023-06-26 18:21:14 +02:00
Screenshots = true,
Snapshots = true
2021-06-03 08:08:05 -07:00
});
2023-12-05 19:19:12 +02:00
var page = await context.NewPageAsync();
2021-06-03 08:08:05 -07:00
await page.GotoAsync("https://playwright.dev");
2022-07-13 11:50:18 +02:00
await context.Tracing.StopAsync(new()
2021-06-03 08:08:05 -07:00
{
2024-09-17 15:32:30 +02:00
Path = "trace.zip"
2021-06-03 08:08:05 -07:00
});
```
2021-05-12 12:21:54 -07:00
## async method: Tracing.start
2022-07-05 16:24:50 -08:00
* since: v1.12
2021-05-12 12:21:54 -07:00
Start tracing.
2022-11-21 10:40:21 -08:00
**Usage**
2021-05-12 12:21:54 -07:00
```js
2021-06-02 22:00:34 -07:00
await context.tracing.start({ screenshots: true, snapshots: true });
2021-05-12 12:21:54 -07:00
const page = await context.newPage();
await page.goto('https://playwright.dev');
2024-09-17 15:32:30 +02:00
await context.tracing.stop({ path: 'trace.zip' });
2021-05-12 12:21:54 -07:00
```
```java
2021-06-15 11:14:09 -07:00
context.tracing().start(new Tracing.StartOptions()
2021-05-12 12:21:54 -07:00
.setScreenshots(true)
2021-06-15 11:14:09 -07:00
.setSnapshots(true));
2021-05-12 12:21:54 -07:00
Page page = context.newPage();
2021-06-15 11:14:09 -07:00
page.navigate("https://playwright.dev");
context.tracing().stop(new Tracing.StopOptions()
2024-09-17 15:32:30 +02:00
.setPath(Paths.get("trace.zip")));
2021-05-12 12:21:54 -07:00
```
```python async
2023-12-21 15:24:54 -08:00
await context.tracing.start(screenshots=True, snapshots=True)
2021-08-31 17:03:31 -07:00
page = await context.new_page()
2021-05-12 12:21:54 -07:00
await page.goto("https://playwright.dev")
2024-09-17 15:32:30 +02:00
await context.tracing.stop(path = "trace.zip")
2021-05-12 12:21:54 -07:00
```
```python sync
2023-12-21 15:24:54 -08:00
context.tracing.start(screenshots=True, snapshots=True)
2021-08-31 17:03:31 -07:00
page = context.new_page()
2021-05-12 12:21:54 -07:00
page.goto("https://playwright.dev")
2024-09-17 15:32:30 +02:00
context.tracing.stop(path = "trace.zip")
2021-05-12 12:21:54 -07:00
```
2021-06-03 08:08:05 -07:00
```csharp
2023-12-05 19:19:12 +02:00
using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync();
2021-06-03 08:08:05 -07:00
await using var context = await browser.NewContextAsync();
2022-07-13 11:50:18 +02:00
await context.Tracing.StartAsync(new()
2021-06-03 08:08:05 -07:00
{
2023-06-26 18:21:14 +02:00
Screenshots = true,
Snapshots = true
2021-06-03 08:08:05 -07:00
});
2023-12-05 19:19:12 +02:00
var page = await context.NewPageAsync();
2021-06-03 08:08:05 -07:00
await page.GotoAsync("https://playwright.dev");
2022-07-13 11:50:18 +02:00
await context.Tracing.StopAsync(new()
2021-06-03 08:08:05 -07:00
{
2024-09-17 15:32:30 +02:00
Path = "trace.zip"
2021-06-03 08:08:05 -07:00
});
```
2021-05-12 12:21:54 -07:00
### option: Tracing.start.name
2022-07-05 16:24:50 -08:00
* since: v1.12
2021-05-12 12:21:54 -07:00
- `name` < [string]>
2023-12-21 15:24:54 -08:00
If specified, intermediate trace files are going to be saved into the files with the
2024-09-26 01:08:16 -07:00
given name prefix inside the [`option: BrowserType.launch.tracesDir` ] directory specified in [`method: BrowserType.launch` ].
2023-12-21 15:24:54 -08:00
To specify the final trace zip file name, you need to pass `path` option to
[`method: Tracing.stop` ] instead.
2021-05-12 12:21:54 -07:00
### option: Tracing.start.screenshots
2022-07-05 16:24:50 -08:00
* since: v1.12
2021-05-12 12:21:54 -07:00
- `screenshots` < [boolean]>
Whether to capture screenshots during tracing. Screenshots are used to build
a timeline preview.
### option: Tracing.start.snapshots
2022-07-05 16:24:50 -08:00
* since: v1.12
2021-05-12 12:21:54 -07:00
- `snapshots` < [boolean]>
2022-01-31 11:44:10 -08:00
If this option is true tracing will
* capture DOM snapshot on every action
* record network activity
2021-05-12 12:21:54 -07:00
2021-11-01 18:57:29 -08:00
### option: Tracing.start.sources
2022-07-05 16:24:50 -08:00
* since: v1.17
2022-01-18 13:05:59 +01:00
* langs: js, csharp, python
2021-11-01 18:57:29 -08:00
- `sources` < [boolean]>
2021-08-31 17:03:31 -07:00
2021-11-01 18:57:29 -08:00
Whether to include source files for trace actions.
2021-08-31 17:03:31 -07:00
2021-12-17 10:50:47 -08:00
### option: Tracing.start.sources
2022-07-05 16:24:50 -08:00
* since: v1.17
2021-12-17 10:50:47 -08:00
* langs: java
- `sources` < [boolean]>
Whether to include source files for trace actions. List of the directories with source code for the application
2022-04-08 10:44:48 -07:00
must be provided via `PLAYWRIGHT_JAVA_SRC` environment variable (the paths should be separated by ';' on Windows
and by ':' on other platforms).
2021-12-17 10:50:47 -08:00
2021-11-01 20:23:35 -08:00
### option: Tracing.start.title
2022-07-05 16:24:50 -08:00
* since: v1.17
2021-11-01 20:23:35 -08:00
- `title` < [string]>
Trace name to be shown in the Trace Viewer.
2021-08-31 17:03:31 -07:00
## async method: Tracing.startChunk
2022-07-05 16:24:50 -08:00
* since: v1.15
2021-08-31 17:03:31 -07:00
Start a new trace chunk. If you'd like to record multiple traces on the same [BrowserContext], use [`method: Tracing.start` ] once, and then create multiple trace chunks with [`method: Tracing.startChunk` ] and [`method: Tracing.stopChunk` ].
2022-11-21 10:40:21 -08:00
**Usage**
2021-08-31 17:03:31 -07:00
```js
await context.tracing.start({ screenshots: true, snapshots: true });
const page = await context.newPage();
await page.goto('https://playwright.dev');
await context.tracing.startChunk();
2022-10-03 17:02:46 -07:00
await page.getByText('Get Started').click();
2021-08-31 17:03:31 -07:00
// Everything between startChunk and stopChunk will be recorded in the trace.
2024-09-17 15:32:30 +02:00
await context.tracing.stopChunk({ path: 'trace1.zip' });
2021-08-31 17:03:31 -07:00
await context.tracing.startChunk();
await page.goto('http://example.com');
// Save a second trace file with different actions.
2024-09-17 15:32:30 +02:00
await context.tracing.stopChunk({ path: 'trace2.zip' });
2021-08-31 17:03:31 -07:00
```
```java
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true)
.setSnapshots(true));
Page page = context.newPage();
page.navigate("https://playwright.dev");
context.tracing().startChunk();
2022-10-03 17:02:46 -07:00
page.getByText("Get Started").click();
2021-08-31 17:03:31 -07:00
// Everything between startChunk and stopChunk will be recorded in the trace.
context.tracing().stopChunk(new Tracing.StopChunkOptions()
2024-09-17 15:32:30 +02:00
.setPath(Paths.get("trace1.zip")));
2021-08-31 17:03:31 -07:00
context.tracing().startChunk();
page.navigate("http://example.com");
// Save a second trace file with different actions.
context.tracing().stopChunk(new Tracing.StopChunkOptions()
2024-09-17 15:32:30 +02:00
.setPath(Paths.get("trace2.zip")));
2021-08-31 17:03:31 -07:00
```
```python async
2023-12-21 15:24:54 -08:00
await context.tracing.start(screenshots=True, snapshots=True)
2021-08-31 17:03:31 -07:00
page = await context.new_page()
await page.goto("https://playwright.dev")
await context.tracing.start_chunk()
2022-10-03 17:02:46 -07:00
await page.get_by_text("Get Started").click()
2021-08-31 17:03:31 -07:00
# Everything between start_chunk and stop_chunk will be recorded in the trace.
2024-09-17 15:32:30 +02:00
await context.tracing.stop_chunk(path = "trace1.zip")
2021-08-31 17:03:31 -07:00
await context.tracing.start_chunk()
await page.goto("http://example.com")
# Save a second trace file with different actions.
2024-09-17 15:32:30 +02:00
await context.tracing.stop_chunk(path = "trace2.zip")
2021-08-31 17:03:31 -07:00
```
```python sync
2023-12-21 15:24:54 -08:00
context.tracing.start(screenshots=True, snapshots=True)
2021-08-31 17:03:31 -07:00
page = context.new_page()
page.goto("https://playwright.dev")
context.tracing.start_chunk()
2022-10-03 17:02:46 -07:00
page.get_by_text("Get Started").click()
2021-08-31 17:03:31 -07:00
# Everything between start_chunk and stop_chunk will be recorded in the trace.
2024-09-17 15:32:30 +02:00
context.tracing.stop_chunk(path = "trace1.zip")
2021-08-31 17:03:31 -07:00
context.tracing.start_chunk()
page.goto("http://example.com")
# Save a second trace file with different actions.
2024-09-17 15:32:30 +02:00
context.tracing.stop_chunk(path = "trace2.zip")
2021-08-31 17:03:31 -07:00
```
```csharp
2023-12-05 19:19:12 +02:00
using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync();
2021-08-31 17:03:31 -07:00
await using var context = await browser.NewContextAsync();
2022-07-13 11:50:18 +02:00
await context.Tracing.StartAsync(new()
2021-08-31 17:03:31 -07:00
{
2023-06-26 18:21:14 +02:00
Screenshots = true,
Snapshots = true
2021-08-31 17:03:31 -07:00
});
2023-12-05 19:19:12 +02:00
var page = await context.NewPageAsync();
2021-08-31 17:03:31 -07:00
await page.GotoAsync("https://playwright.dev");
await context.Tracing.StartChunkAsync();
2022-10-03 17:02:46 -07:00
await page.GetByText("Get Started").ClickAsync();
2021-08-31 17:03:31 -07:00
// Everything between StartChunkAsync and StopChunkAsync will be recorded in the trace.
2022-07-13 11:50:18 +02:00
await context.Tracing.StopChunkAsync(new()
2021-08-31 17:03:31 -07:00
{
2024-09-17 15:32:30 +02:00
Path = "trace1.zip"
2021-08-31 17:03:31 -07:00
});
await context.Tracing.StartChunkAsync();
await page.GotoAsync("http://example.com");
// Save a second trace file with different actions.
2022-07-13 11:50:18 +02:00
await context.Tracing.StopChunkAsync(new()
2021-08-31 17:03:31 -07:00
{
2024-09-17 15:32:30 +02:00
Path = "trace2.zip"
2021-08-31 17:03:31 -07:00
});
```
2021-11-01 20:23:35 -08:00
### option: Tracing.startChunk.title
2022-07-05 16:24:50 -08:00
* since: v1.17
2021-11-01 20:23:35 -08:00
- `title` < [string]>
Trace name to be shown in the Trace Viewer.
2023-03-15 17:34:56 -07:00
### option: Tracing.startChunk.name
* since: v1.32
- `name` < [string]>
2023-12-21 15:24:54 -08:00
If specified, intermediate trace files are going to be saved into the files with the
2024-09-26 01:08:16 -07:00
given name prefix inside the [`option: BrowserType.launch.tracesDir` ] directory specified in [`method: BrowserType.launch` ].
2023-12-21 15:24:54 -08:00
To specify the final trace zip file name, you need to pass `path` option to
[`method: Tracing.stopChunk` ] instead.
2023-03-15 17:34:56 -07:00
2024-11-05 12:45:37 +01:00
## async method: Tracing.group
* since: v1.49
2024-11-05 04:45:54 -08:00
:::caution
2024-11-07 03:57:18 -08:00
Use `test.step` instead when available.
2024-11-05 12:45:37 +01:00
:::
2024-11-07 03:57:18 -08:00
Creates a new group within the trace, assigning any subsequent API calls to this group, until [`method: Tracing.groupEnd` ] is called. Groups can be nested and will be visible in the trace viewer.
2024-11-05 12:45:37 +01:00
**Usage**
```js
2024-11-07 03:57:18 -08:00
// use test.step instead
await test.step('Log in', async () => {
// ...
});
2024-11-05 12:45:37 +01:00
```
2024-11-05 04:45:54 -08:00
```java
2024-11-07 03:57:18 -08:00
// All actions between group and groupEnd
// will be shown in the trace viewer as a group.
2024-11-21 13:45:00 +00:00
page.context().tracing().group("Open Playwright.dev > API");
2024-11-05 04:45:54 -08:00
page.navigate("https://playwright.dev/");
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("API")).click();
2024-11-21 13:45:00 +00:00
page.context().tracing().groupEnd();
2024-11-05 04:45:54 -08:00
```
```python sync
2024-11-07 03:57:18 -08:00
# All actions between group and group_end
# will be shown in the trace viewer as a group.
2024-11-05 04:45:54 -08:00
page.context.tracing.group("Open Playwright.dev > API")
page.goto("https://playwright.dev/")
page.get_by_role("link", name="API").click()
page.context.tracing.group_end()
```
```python async
2024-11-07 03:57:18 -08:00
# All actions between group and group_end
# will be shown in the trace viewer as a group.
2024-11-05 04:45:54 -08:00
await page.context.tracing.group("Open Playwright.dev > API")
await page.goto("https://playwright.dev/")
await page.get_by_role("link", name="API").click()
await page.context.tracing.group_end()
```
```csharp
2024-11-07 03:57:18 -08:00
// All actions between GroupAsync and GroupEndAsync
// will be shown in the trace viewer as a group.
2024-11-21 13:45:00 +00:00
await Page.Context.Tracing.GroupAsync("Open Playwright.dev > API");
2024-11-05 04:45:54 -08:00
await Page.GotoAsync("https://playwright.dev/");
await Page.GetByRole(AriaRole.Link, new() { Name = "API" }).ClickAsync();
2024-11-21 13:45:00 +00:00
await Page.Context.Tracing.GroupEndAsync();
2024-11-05 04:45:54 -08:00
```
2024-11-05 12:45:37 +01:00
### param: Tracing.group.name
* since: v1.49
- `name` < [string]>
2024-11-07 03:57:18 -08:00
Group name shown in the trace viewer.
2024-11-05 12:45:37 +01:00
### option: Tracing.group.location
* since: v1.49
- `location` ?< [Object]>
2024-11-07 03:57:18 -08:00
- `file` < [string]>
- `line` ?< [int]>
- `column` ?< [int]>
2024-11-05 12:45:37 +01:00
2024-11-07 03:57:18 -08:00
Specifies a custom location for the group to be shown in the trace viewer. Defaults to the location of the [`method: Tracing.group` ] call.
2024-11-05 12:45:37 +01:00
## async method: Tracing.groupEnd
* since: v1.49
2024-11-05 04:45:54 -08:00
Closes the last group created by [`method: Tracing.group` ].
2024-11-05 12:45:37 +01:00
2021-05-12 12:21:54 -07:00
## async method: Tracing.stop
2022-07-05 16:24:50 -08:00
* since: v1.12
2021-05-12 12:21:54 -07:00
Stop tracing.
2021-06-02 10:04:25 -07:00
### option: Tracing.stop.path
2022-07-05 16:24:50 -08:00
* since: v1.12
2021-06-02 10:04:25 -07:00
- `path` < [path]>
2021-08-31 17:03:31 -07:00
Export trace into the file with the given path.
## async method: Tracing.stopChunk
2022-07-05 16:24:50 -08:00
* since: v1.15
2021-08-31 17:03:31 -07:00
Stop the trace chunk. See [`method: Tracing.startChunk` ] for more details about multiple trace chunks.
### option: Tracing.stopChunk.path
2022-07-05 16:24:50 -08:00
* since: v1.15
2021-08-31 17:03:31 -07:00
- `path` < [path]>
Export trace collected since the last [`method: Tracing.startChunk` ] call into the file with the given path.