Tracing
用于收集和保存 Playwright 跟踪(trace)的 API。Playwright 跟踪可以在 Playwright 脚本运行后在跟踪查看器中打开。
在执行操作之前开始记录跟踪。结束时,停止跟踪并将其保存到文件中。
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如果可以使用,请改用 test.step
。
在跟踪中创建一个新组,将任何后续 API 调用分配到此组,直到调用tracing.groupEnd()。组可以嵌套,并在跟踪查看器中可见。
用法
// use test.step instead
await test.step('Log in', async () => {
// ...
});
参数
-
在跟踪查看器中显示的组名。
-
options
Object (可选)
返回值
groupEnd
新增于: v1.49关闭由tracing.group()创建的最后一个组。
用法
await tracing.groupEnd();
返回值
start
新增于: v1.12开始跟踪。
用法
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 (可选)-
如果指定,中间跟踪文件将使用给定的名称前缀保存在tracesDir目录中,该目录在browserType.launch()中指定。要指定最终的跟踪 zip 文件名,你需要将
path
选项传递给tracing.stop()。 -
跟踪期间是否捕获屏幕截图。屏幕截图用于构建时间线预览。
-
如果此选项为 true,跟踪将:
- 在每个操作上捕获 DOM 快照
- 记录网络活动
-
sources
boolean (可选)新增于: v1.17#是否包含跟踪操作的源文件。
-
在跟踪查看器中显示的跟踪名称。
-
返回值
startChunk
新增于: v1.15开始一个新的跟踪分块。如果你想在同一个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 (可选)-
如果指定,中间跟踪文件将使用给定的名称前缀保存在tracesDir目录中,该目录在browserType.launch()中指定。要指定最终的跟踪 zip 文件名,你需要将
path
选项传递给tracing.stopChunk()。 -
在跟踪查看器中显示的跟踪名称。
-
返回值
stop
新增于: v1.12停止跟踪。
用法
await tracing.stop();
await tracing.stop(options);
参数
返回值
stopChunk
新增于: v1.15停止跟踪分块。有关多个跟踪分块的更多详细信息,请参阅tracing.startChunk()。
用法
await tracing.stopChunk();
await tracing.stopChunk(options);
参数
options
Object (可选)-
将自上次调用tracing.startChunk()以来收集的跟踪导出到给定路径的文件中。
-
返回值