Skip to main content

Tracing

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

¥API for collecting and saving Playwright traces. Playwright traces can be opened in Trace Viewer after Playwright script runs.

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

¥Start recording a trace before performing actions. At the end, stop tracing and save it to a file.

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.nodejs.cn');
await context.tracing.stop({ path: 'trace.zip' });

方法

¥Methods

start

Added in: v1.12 tracing.start

开始追踪。

¥Start tracing.

用法

¥Usage

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

参数

¥Arguments

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

¥If specified, intermediate trace files are going to be saved into the files with the given name prefix inside the tracesDir directory specified in browserType.launch(). To specify the final trace zip file name, you need to pass path option to tracing.stop() instead.

追踪过程中是否截屏。屏幕截图用于构建时间线预览。

¥Whether to capture screenshots during tracing. Screenshots are used to build a timeline preview.

如果此选项为 true,则跟踪将

¥If this option is true tracing will

  • 捕获每个动作的 DOM 快照

    ¥capture DOM snapshot on every action

  • 记录网络活动

    ¥record network activity

    • sources boolean (optional) Added in: v1.17#

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

¥Whether to include source files for trace actions.

  • title string (optional) Added in: v1.17#

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

¥Trace name to be shown in the Trace Viewer.

返回

¥Returns


startChunk

Added in: v1.15 tracing.startChunk

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

¥Start a new trace chunk. If you'd like to record multiple traces on the same BrowserContext, use tracing.start() once, and then create multiple trace chunks with tracing.startChunk() and tracing.stopChunk().

用法

¥Usage

await context.tracing.start({ screenshots: true, snapshots: true });
const page = await context.newPage();
await page.goto('https://playwright.nodejs.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' });

参数

¥Arguments

  • options Object (optional)

    • name string (optional) Added in: v1.32#

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

¥If specified, intermediate trace files are going to be saved into the files with the given name prefix inside the tracesDir directory specified in browserType.launch(). To specify the final trace zip file name, you need to pass path option to tracing.stopChunk() instead.

  • title string (optional) Added in: v1.17#

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

¥Trace name to be shown in the Trace Viewer.

返回

¥Returns


stop

Added in: v1.12 tracing.stop

停止追踪。

¥Stop tracing.

用法

¥Usage

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

参数

¥Arguments

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

¥Export trace into the file with the given path.

返回

¥Returns


stopChunk

Added in: v1.15 tracing.stopChunk

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

¥Stop the trace chunk. See tracing.startChunk() for more details about multiple trace chunks.

用法

¥Usage

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

参数

¥Arguments

将自上次 tracing.startChunk() 调用以来收集的跟踪导出到具有给定路径的文件中。

¥Export trace collected since the last tracing.startChunk() call into the file with the given path.

返回

¥Returns