跳至主要内容

追踪

用于收集和保存Playwright跟踪记录的API。Playwright跟踪记录可以在脚本运行后通过Trace Viewer查看。

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

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.dev");
await context.Tracing.StopAsync(new()
{
Path = "trace.zip"
});

方法

GroupAsync

Added in: v1.49 tracing.GroupAsync
caution

在可用时请改用 test.step

在跟踪记录中创建一个新的组,将后续的所有API调用分配到此组,直到调用Tracing.GroupEndAsync()为止。组可以嵌套,并会在跟踪查看器中显示。

用法

// 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.dev/");
await Page.GetByRole(AriaRole.Link, new() { Name = "API" }).ClickAsync();
await Page.Context.Tracing.GroupEndAsync();

参数

  • name string#

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

  • options TracingGroupOptions? (可选)

    • Location Location? (optional)# Specifies a custom location for the group to be shown in the trace viewer. Defaults to the location of the Tracing.GroupAsync() call.

返回


GroupEndAsync

Added in: v1.49 tracing.GroupEndAsync

关闭由Tracing.GroupAsync()创建的最后分组。

用法

await Tracing.GroupEndAsync();

返回


StartAsync

Added in: v1.12 tracing.StartAsync

开始追踪。

用法

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.dev");
await context.Tracing.StopAsync(new()
{
Path = "trace.zip"
});

参数

  • options TracingStartOptions? (optional)
    • Name string? (可选)#

      如果指定该参数,中间跟踪文件将被保存到BrowserType.LaunchAsync()中指定的TracesDir目录下,并以给定名称作为前缀。要指定最终的跟踪zip文件名,您需要改为向Tracing.StopAsync()传递path选项。

    • Screenshots bool? (可选)#

      是否在跟踪期间捕获屏幕截图。屏幕截图用于构建时间线预览。

    • Snapshots bool? (可选)#

      如果此选项为true,追踪将

      • 在每次操作时捕获DOM快照
      • 记录网络活动
    • Sources bool? (可选) 添加于: v1.17#

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

    • Title string? (可选) 添加于: v1.17#

      在Trace Viewer中显示的追踪名称。

返回


StartChunkAsync

Added in: v1.15 tracing.StartChunkAsync

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

用法

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.dev");

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"
});

参数

  • options TracingStartChunkOptions? (optional)
    • Name string? (可选) 添加于: v1.32#

      如果指定该参数,中间跟踪文件将被保存到BrowserType.LaunchAsync()中指定的TracesDir目录下,并以给定的名称前缀命名。要指定最终的跟踪zip文件名,您需要改为向Tracing.StopChunkAsync()传递path选项。

    • Title string? (可选) 添加于: v1.17#

      将在Trace Viewer中显示的追踪名称。

返回


StopAsync

Added in: v1.12 tracing.StopAsync

停止追踪。

用法

await Tracing.StopAsync(options);

参数

  • options TracingStopOptions? (optional)
    • Path string? (可选)#

      将跟踪记录导出到指定路径的文件中。

返回


StopChunkAsync

Added in: v1.15 tracing.StopChunkAsync

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

用法

await Tracing.StopChunkAsync(options);

参数

  • options TracingStopChunkOptions? (optional)

返回