跳到主要内容

追踪

用于收集和保存 Playwright 追踪的 API。Playwright 追踪可以在 Playwright 脚本运行后在 Trace Viewer 中打开。

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

const browser = await chromium.launch();
const context = await browser.newContext();
await context.tracing.start({ screenshots: true, snapshots: true });
const page = await context.newPage();
await page.goto('https://playwright.net.cn');
await context.tracing.stop({ path: 'trace.zip' });

方法

group

添加于: v1.49 tracing.group
注意

优先使用 test.step

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

用法

// use test.step instead
await test.step('Log in', async () => {
// ...
});

参数

  • name string#

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

  • options Object (可选)

返回


groupEnd

添加于: v1.49 tracing.groupEnd

关闭由 tracing.group() 创建的最后一个组。

用法

await tracing.groupEnd();

返回


start

添加于: v1.12 tracing.start

开始追踪。

用法

await context.tracing.start({ screenshots: true, snapshots: true });
const page = await context.newPage();
await page.goto('https://playwright.net.cn');
await context.tracing.stop({ path: 'trace.zip' });

参数

  • options Object (可选)
    • name string (可选)#

      如果指定,中间追踪文件将保存到 tracesDir 目录中,文件名前缀为给定名称,该目录在 browserType.launch() 中指定。要指定最终的追踪 zip 文件名,您需要将 path 选项传递给 tracing.stop()

    • screenshots boolean (可选)#

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

    • snapshots boolean (可选)#

      如果此选项为 true,追踪将

      • 在每个操作上捕获 DOM 快照
      • 记录网络活动
    • sources boolean (可选)添加于: v1.17#

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

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

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

返回


startChunk

添加于: v1.15 tracing.startChunk

开始新的追踪块。如果您想在同一个 BrowserContext 上记录多个追踪,请使用 tracing.start() 一次,然后使用 tracing.startChunk()tracing.stopChunk() 创建多个追踪块。

用法

await context.tracing.start({ screenshots: true, snapshots: true });
const page = await context.newPage();
await page.goto('https://playwright.net.cn');

await context.tracing.startChunk();
await page.getByText('Get Started').click();
// Everything between startChunk and stopChunk will be recorded in the trace.
await context.tracing.stopChunk({ path: 'trace1.zip' });

await context.tracing.startChunk();
await page.goto('http://example.com');
// Save a second trace file with different actions.
await context.tracing.stopChunk({ path: 'trace2.zip' });

参数

  • options Object (可选)
    • name string (可选)添加于: v1.32#

      如果指定,中间追踪文件将保存到 tracesDir 目录中,文件名前缀为给定名称,该目录在 browserType.launch() 中指定。要指定最终的追踪 zip 文件名,您需要将 path 选项传递给 tracing.stopChunk()

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

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

返回


stop

添加于: v1.12 tracing.stop

停止追踪。

用法

await tracing.stop();
await tracing.stop(options);

参数

  • options Object (可选)
    • path string (可选)#

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

返回


stopChunk

添加于: v1.15 tracing.stopChunk

停止追踪块。有关多个追踪块的更多详细信息,请参阅 tracing.startChunk()

用法

await tracing.stopChunk();
await tracing.stopChunk(options);

参数

返回