跳至主要内容

跟踪

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

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

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

方法

StartAsync

新增于: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.net.cn");
await context.Tracing.StopAsync(new()
{
Path = "trace.zip"
});

参数

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

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

    • Screenshots bool? (可选)#

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

    • Snapshots bool? (可选)#

      如果此选项为真,跟踪将

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

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

    • Title string? (可选)新增于:v1.17#

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

返回


StartChunkAsync

新增于:v1.15 tracing.StartChunkAsync

开始一个新的跟踪块。如果您想在同一个 浏览器上下文 上记录多个跟踪,请使用 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.net.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"
});

参数

  • options TracingStartChunkOptions? (可选)
    • Name string? (可选)新增于:v1.32#

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

    • Title string? (可选)新增于:v1.17#

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

返回


StopAsync

新增于:v1.12 tracing.StopAsync

停止跟踪。

用法

await Tracing.StopAsync(options);

参数

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

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

返回


StopChunkAsync

新增于:v1.15 tracing.StopChunkAsync

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

用法

await Tracing.StopChunkAsync(options);

参数

  • options TracingStopChunkOptions? (可选)

返回