Skip to main content

Tracing

用于收集和保存 Playwright 痕迹的 API。Playwright 脚本运行后,可以在 跟踪查看器 打开 Playwright 痕迹。

¥API for collecting and saving Playwright traces. Playwright traces can be opened in Trace Viewer after Playwright script runs.

在执行操作之前开始记录跟踪。最后,停止跟踪并将其保存到文件中。

¥Start recording a trace before performing actions. At the end, stop tracing and save it to a file.

using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync();
await using var context = await browser.NewContextAsync();
await context.Tracing.StartAsync(new()
{
Screenshots = true,
Snapshots = true
});
var page = await context.NewPageAsync();
await page.GotoAsync("https://playwright.nodejs.cn");
await context.Tracing.StopAsync(new()
{
Path = "trace.zip"
});

方法

¥Methods

GroupAsync

Added in: v1.49 tracing.GroupAsync
提醒

在可用时改用 test.step

¥Use test.step instead when available.

在跟踪中创建一个新组,将任何后续 API 调用分配给该组,直到调用 Tracing.GroupEndAsync()。组可以嵌套,并将在跟踪查看器中可见。

¥Creates a new group within the trace, assigning any subsequent API calls to this group, until Tracing.GroupEndAsync() is called. Groups can be nested and will be visible in the trace viewer.

用法

¥Usage

// All actions between GroupAsync and GroupEndAsync
// will be shown in the trace viewer as a group.
await Page.Context.Tracing.GroupAsync("Open Playwright.dev > API");
await Page.GotoAsync("https://playwright.nodejs.cn/");
await Page.GetByRole(AriaRole.Link, new() { Name = "API" }).ClickAsync();
await Page.Context.Tracing.GroupEndAsync();

参数

¥Arguments

跟踪查看器中显示的组名称。

¥Group name shown in the trace viewer.

  • options TracingGroupOptions?(可选)

    ¥options TracingGroupOptions? (optional)

    • Location 位置?(可选)#

      ¥Location Location? (optional)#

指定要在跟踪查看器中显示的组的自定义位置。默认为 Tracing.GroupAsync() 调用的位置。

¥Specifies a custom location for the group to be shown in the trace viewer. Defaults to the location of the Tracing.GroupAsync() call.

返回

¥Returns


GroupEndAsync

Added in: v1.49 tracing.GroupEndAsync

关闭 Tracing.GroupAsync() 创建的最后一个组。

¥Closes the last group created by Tracing.GroupAsync().

用法

¥Usage

await Tracing.GroupEndAsync();

返回

¥Returns


StartAsync

Added in: v1.12 tracing.StartAsync

开始追踪。

¥Start tracing.

用法

¥Usage

using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync();
await using var context = await browser.NewContextAsync();
await context.Tracing.StartAsync(new()
{
Screenshots = true,
Snapshots = true
});
var page = await context.NewPageAsync();
await page.GotoAsync("https://playwright.nodejs.cn");
await context.Tracing.StopAsync(new()
{
Path = "trace.zip"
});

参数

¥Arguments

  • options TracingStartOptions?(可选)

    ¥options TracingStartOptions? (optional)

如果指定,中间跟踪文件将保存到 BrowserType.LaunchAsync() 中指定的 TracesDir 目录中具有给定名称前缀的文件中。要指定最终跟踪 zip 文件名,你需要将 path 选项传递给 Tracing.StopAsync()

¥If specified, intermediate trace files are going to be saved into the files with the given name prefix inside the TracesDir directory specified in BrowserType.LaunchAsync(). To specify the final trace zip file name, you need to pass path option to Tracing.StopAsync() instead.

  • Screenshots bool? (optional)#

追踪过程中是否截屏。屏幕截图用于构建时间线预览。

¥Whether to capture screenshots during tracing. Screenshots are used to build a timeline preview.

  • Snapshots bool? (optional)#

如果此选项为 true,则跟踪将

¥If this option is true tracing will

  • 捕获每个动作的 DOM 快照

    ¥capture DOM snapshot on every action

  • 记录网络活动

    ¥record network activity

    • Sources bool? (optional) Added in: v1.17#

是否包含跟踪操作的源文件。

¥Whether to include source files for trace actions.

  • Title string? (optional) Added in: v1.17#

要在跟踪查看器中显示的跟踪名称。

¥Trace name to be shown in the Trace Viewer.

返回

¥Returns


StartChunkAsync

Added in: v1.15 tracing.StartChunkAsync

开始一个新的跟踪块。如果你想在同一个 BrowserContext 上记录多个跟踪,请使用 Tracing.StartAsync() 一次,然后使用 Tracing.StartChunkAsync()Tracing.StopChunkAsync() 创建多个跟踪块。

¥Start a new trace chunk. If you'd like to record multiple traces on the same BrowserContext, use Tracing.StartAsync() once, and then create multiple trace chunks with Tracing.StartChunkAsync() and Tracing.StopChunkAsync().

用法

¥Usage

using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync();
await using var context = await browser.NewContextAsync();
await context.Tracing.StartAsync(new()
{
Screenshots = true,
Snapshots = true
});
var page = await context.NewPageAsync();
await page.GotoAsync("https://playwright.nodejs.cn");

await context.Tracing.StartChunkAsync();
await page.GetByText("Get Started").ClickAsync();
// Everything between StartChunkAsync and StopChunkAsync will be recorded in the trace.
await context.Tracing.StopChunkAsync(new()
{
Path = "trace1.zip"
});

await context.Tracing.StartChunkAsync();
await page.GotoAsync("http://example.com");
// Save a second trace file with different actions.
await context.Tracing.StopChunkAsync(new()
{
Path = "trace2.zip"
});

参数

¥Arguments

  • options TracingStartChunkOptions?(可选)

    ¥options TracingStartChunkOptions? (optional)

    • Name string? (optional) Added in: v1.32#

如果指定,中间跟踪文件将保存到 BrowserType.LaunchAsync() 中指定的 TracesDir 目录中具有给定名称前缀的文件中。要指定最终跟踪 zip 文件名,你需要将 path 选项传递给 Tracing.StopChunkAsync()

¥If specified, intermediate trace files are going to be saved into the files with the given name prefix inside the TracesDir directory specified in BrowserType.LaunchAsync(). To specify the final trace zip file name, you need to pass path option to Tracing.StopChunkAsync() instead.

  • Title string? (optional) Added in: v1.17#

要在跟踪查看器中显示的跟踪名称。

¥Trace name to be shown in the Trace Viewer.

返回

¥Returns


StopAsync

Added in: v1.12 tracing.StopAsync

停止追踪。

¥Stop tracing.

用法

¥Usage

await Tracing.StopAsync(options);

参数

¥Arguments

  • options TracingStopOptions?(可选)

    ¥options TracingStopOptions? (optional)

将跟踪导出到具有给定路径的文件中。

¥Export trace into the file with the given path.

返回

¥Returns


StopChunkAsync

Added in: v1.15 tracing.StopChunkAsync

停止跟踪块。有关多个跟踪块的更多详细信息,请参阅 Tracing.StartChunkAsync()

¥Stop the trace chunk. See Tracing.StartChunkAsync() for more details about multiple trace chunks.

用法

¥Usage

await Tracing.StopChunkAsync(options);

参数

¥Arguments

  • options TracingStopChunkOptions?(可选)

    ¥options TracingStopChunkOptions? (optional)

将自上次 Tracing.StartChunkAsync() 调用以来收集的跟踪导出到具有给定路径的文件中。

¥Export trace collected since the last Tracing.StartChunkAsync() call into the file with the given path.

返回

¥Returns