Tracing
用于收集和保存 Playwright 跟踪的 API。Playwright 脚本运行后,可以在 Trace Viewer 中打开 Playwright 跟踪。
🌐 API for collecting and saving Playwright traces. Playwright traces can be opened in Trace Viewer after Playwright script runs.
你可能想在配置文件中启用跟踪,而不是使用 context.tracing。
🌐 You probably want to enable tracing in your config file instead of using context.tracing.
context.tracing API 会捕获浏览器操作和网络活动,但不会记录测试断言(如 expect 调用)。我们建议 通过 Playwright Test 配置启用跟踪,这样可以包括这些断言,并为调试测试失败提供更完整的跟踪。
🌐 The context.tracing API captures browser operations and network activity, but it doesn't record test assertions (like expect calls). We recommend enabling tracing through Playwright Test configuration, which includes those assertions and provides a more complete trace for debugging test failures.
在执行操作之前开始记录跟踪。最后,停止跟踪并将其保存到文件中。
🌐 Start recording a trace before performing actions. At the end, stop tracing and save it to a file.
- Sync
- Async
browser = chromium.launch()
context = browser.new_context()
context.tracing.start(screenshots=True, snapshots=True)
page = context.new_page()
page.goto("https://playwright.nodejs.cn")
context.tracing.stop(path = "trace.zip")
browser = await chromium.launch()
context = await browser.new_context()
await context.tracing.start(screenshots=True, snapshots=True)
page = await context.new_page()
await page.goto("https://playwright.nodejs.cn")
await context.tracing.stop(path = "trace.zip")
方法
🌐 Methods
group
Added in: v1.49在可用时,请改用 test.step。
🌐 Use test.step instead when available.
在追踪中创建一个新组,将随后的所有 API 调用分配给该组,直到调用 tracing.group_end()。组可以嵌套,并将在追踪查看器中显示。
🌐 Creates a new group within the trace, assigning any subsequent API calls to this group, until tracing.group_end() is called. Groups can be nested and will be visible in the trace viewer.
用法
- Sync
- Async
# All actions between group and group_end
# will be shown in the trace viewer as a group.
page.context.tracing.group("Open Playwright.dev > API")
page.goto("https://playwright.nodejs.cn/")
page.get_by_role("link", name="API").click()
page.context.tracing.group_end()
# All actions between group and group_end
# will be shown in the trace viewer as a group.
await page.context.tracing.group("Open Playwright.dev > API")
await page.goto("https://playwright.nodejs.cn/")
await page.get_by_role("link", name="API").click()
await page.context.tracing.group_end()
参数
-
跟踪查看器中显示的组名称。
-
指定在跟踪查看器中显示该组的自定义位置。默认情况下为 tracing.group() 调用的位置。
返回
group_end
Added in: v1.49关闭由 tracing.group() 创建的最后一个组。
🌐 Closes the last group created by tracing.group().
用法
tracing.group_end()
返回
start
Added in: v1.12开始追踪。
🌐 Start tracing.
你可能想在配置文件中启用跟踪,而不是使用 Tracing.start。
🌐 You probably want to enable tracing in your config file instead of using Tracing.start.
context.tracing API 会捕获浏览器操作和网络活动,但不会记录测试断言(如 expect 调用)。我们建议 通过 Playwright Test 配置启用跟踪,这样可以包括这些断言,并为调试测试失败提供更完整的跟踪。
🌐 The context.tracing API captures browser operations and network activity, but it doesn't record test assertions (like expect calls). We recommend enabling tracing through Playwright Test configuration, which includes those assertions and provides a more complete trace for debugging test failures.
用法
- Sync
- Async
context.tracing.start(screenshots=True, snapshots=True)
page = context.new_page()
page.goto("https://playwright.nodejs.cn")
context.tracing.stop(path = "trace.zip")
await context.tracing.start(screenshots=True, snapshots=True)
page = await context.new_page()
await page.goto("https://playwright.nodejs.cn")
await context.tracing.stop(path = "trace.zip")
参数
-
如果指定,中间跟踪文件将会保存到在 browser_type.launch() 中指定的 traces_dir 目录下,以给定名称前缀命名的文件中。要指定最终的跟踪压缩文件名称,则需要将
path选项传递给 tracing.stop()。 -
是否在跟踪过程中捕获屏幕截图。屏幕截图用于构建时间线预览。
-
如果此选项为 true,则跟踪将
- 捕获每个动作的 DOM 快照
- 记录网络活动
-
sourcesbool (optional) Added in: v1.17#是否包含跟踪操作的源文件。
-
titlestr (optional) Added in: v1.17#要在跟踪查看器中显示的跟踪名称。
返回
start_chunk
Added in: v1.15开始一个新的跟踪片段。如果你想在同一个 BrowserContext 上记录多个跟踪,请先调用一次 tracing.start(),然后使用 tracing.start_chunk() 和 tracing.stop_chunk() 创建多个跟踪片段。
🌐 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.start_chunk() and tracing.stop_chunk().
用法
- Sync
- Async
context.tracing.start(screenshots=True, snapshots=True)
page = context.new_page()
page.goto("https://playwright.nodejs.cn")
context.tracing.start_chunk()
page.get_by_text("Get Started").click()
# Everything between start_chunk and stop_chunk will be recorded in the trace.
context.tracing.stop_chunk(path = "trace1.zip")
context.tracing.start_chunk()
page.goto("http://example.com")
# Save a second trace file with different actions.
context.tracing.stop_chunk(path = "trace2.zip")
await context.tracing.start(screenshots=True, snapshots=True)
page = await context.new_page()
await page.goto("https://playwright.nodejs.cn")
await context.tracing.start_chunk()
await page.get_by_text("Get Started").click()
# Everything between start_chunk and stop_chunk will be recorded in the trace.
await context.tracing.stop_chunk(path = "trace1.zip")
await context.tracing.start_chunk()
await page.goto("http://example.com")
# Save a second trace file with different actions.
await context.tracing.stop_chunk(path = "trace2.zip")
参数
-
namestr (optional) Added in: v1.32#如果指定,中间跟踪文件将会保存到在 browser_type.launch() 中指定的 traces_dir 目录内并带有给定名称前缀的文件中。要指定最终的跟踪压缩文件名称,你需要将
path选项传递给 tracing.stop_chunk() 。 -
titlestr (optional) Added in: v1.17#要在跟踪查看器中显示的跟踪名称。
返回
stop
Added in: v1.12停止追踪。
🌐 Stop tracing.
用法
tracing.stop()
tracing.stop(**kwargs)
参数
-
pathUnion[str, pathlib.Path] (optional)#将跟踪导出到具有给定路径的文件中。
返回
stop_chunk
Added in: v1.15停止跟踪块。有关多个跟踪块的更多详细信息,请参见 tracing.start_chunk()。
🌐 Stop the trace chunk. See tracing.start_chunk() for more details about multiple trace chunks.
用法
tracing.stop_chunk()
tracing.stop_chunk(**kwargs)
参数
-
pathUnion[str, pathlib.Path] (optional)#将自上次 tracing.start_chunk() 调用以来收集的追踪导出到指定路径的文件中。
返回