Skip to main content

Tracing

用于收集和保存 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.dev');
await context.tracing.stop({ path: 'trace.zip' });

tracing.start([options])

Added in: v1.12
  • options? <Object>
    • name? <string> 如果设置,该 trace 将保存到 tracesDir 文件夹(在 browserType.launch([options]) 中指定)内以该名称命名的文件中。#
    • screenshots? <boolean> 是否在 tracing 期间捕获屏幕截图。截图用于构建时间线预览。#
    • snapshots? <boolean> 若该选项为 true,则 tracing 将:#
      • 在每个 action 上捕获 DOM 快照
      • 记录网络活动
    • sources? <boolean> 是否为 trace 操作包含源文件。新增于:v1.17#
    • title? <string> 在 Trace Viewer 中显示的 trace 名称。新增于:v1.17#
  • returns: <Promise<void>>#

开始 tracing。

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

tracing.startChunk([options])

Added in: v1.15

开始一个新的 trace chunk。如果你希望在同一个 BrowserContext 中记录多个 trace,应先调用一次 tracing.start([options]),然后使用 tracing.startChunk([options])tracing.stopChunk([options]) 创建多个 trace chunk。

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

await context.tracing.startChunk();
await page.getByText('Get Started').click();
// startChunk 和 stopChunk 之间的所有内容都将记录在跟踪中。
await context.tracing.stopChunk({ path: 'trace1.zip' });

await context.tracing.startChunk();
await page.goto('http://example.com');
// 保存具有不同操作的第二个跟踪文件。
await context.tracing.stopChunk({ path: 'trace2.zip' });

tracing.stop([options])

Added in: v1.12

停止 tracing。

tracing.stopChunk([options])

Added in: v1.15

停止当前 trace chunk。关于多个 trace chunk 的更多信息,请参见 tracing.startChunk([options])