Tracing
用于收集和保存 Playwright 追踪的 API。Playwright 脚本运行后,可以在追踪查看器中打开 Playwright 追踪。
您可能希望在您的配置文件中启用追踪,而不是使用context.tracing
。
context.tracing
API 捕获浏览器操作和网络活动,但它不记录测试断言(如 expect
调用)。我们建议通过 Playwright Test 配置启用追踪,其中包含这些断言,并为调试测试失败提供更完整的追踪。
在执行操作之前开始记录追踪。最后,停止追踪并将其保存到文件中。
- 同步
- 异步
browser = chromium.launch()
context = browser.new_context()
context.tracing.start(screenshots=True, snapshots=True)
page = context.new_page()
page.goto("https://playwright.net.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.net.cn")
await context.tracing.stop(path = "trace.zip")
方法
group
新增于: v1.49如果可用,请改用test.step
。
在追踪中创建一个新组,并将所有后续 API 调用分配到此组,直到调用tracing.group_end()。组可以嵌套,并将在追踪查看器中可见。
用法
- 同步
- 异步
# 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.net.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.net.cn/")
await page.get_by_role("link", name="API").click()
await page.context.tracing.group_end()
参数
-
在追踪查看器中显示的组名。
-
指定组在追踪查看器中显示的自定义位置。默认为 tracing.group() 调用的位置。
返回
group_end
新增于: v1.49关闭由tracing.group()创建的最后一个组。
用法
tracing.group_end()
返回
start
添加于:v1.12开始追踪。
您可能希望在您的配置文件中启用追踪,而不是使用Tracing.start
。
context.tracing
API 捕获浏览器操作和网络活动,但它不记录测试断言(如 expect
调用)。我们建议通过 Playwright Test 配置启用追踪,其中包含这些断言,并为调试测试失败提供更完整的追踪。
用法
- 同步
- 异步
context.tracing.start(screenshots=True, snapshots=True)
page = context.new_page()
page.goto("https://playwright.net.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.net.cn")
await context.tracing.stop(path = "trace.zip")
参数
-
如果指定,中间追踪文件将保存在browser_type.launch()中指定的traces_dir目录中,文件名前缀为给定名称。要指定最终的追踪 zip 文件名,您需要将
path
选项传递给tracing.stop()。 -
是否在追踪期间捕获屏幕截图。屏幕截图用于构建时间线预览。
-
如果此选项为 true,追踪将
- 在每次操作时捕获 DOM 快照
- 记录网络活动
-
是否包含追踪操作的源文件。
-
在追踪查看器中显示的追踪名称。
返回
start_chunk
新增于: v1.15开始一个新的追踪块。如果您想在同一个BrowserContext上记录多个追踪,请先调用一次tracing.start(),然后使用tracing.start_chunk()和tracing.stop_chunk()创建多个追踪块。
用法
- 同步
- 异步
context.tracing.start(screenshots=True, snapshots=True)
page = context.new_page()
page.goto("https://playwright.net.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.net.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")
参数
-
如果指定,中间追踪文件将保存到browser_type.launch()中指定的traces_dir目录中,文件名前缀为给定名称。要指定最终的追踪 zip 文件名,您需要将
path
选项传递给tracing.stop_chunk()。 -
在追踪查看器中显示的追踪名称。
返回
stop
添加于:v1.12停止追踪。
用法
tracing.stop()
tracing.stop(**kwargs)
参数
-
path
Union[str, pathlib.Path] (可选)#将追踪导出到给定路径的文件中。
返回
stop_chunk
新增于: v1.15停止追踪块。有关多个追踪块的更多详细信息,请参阅tracing.start_chunk()。
用法
tracing.stop_chunk()
tracing.stop_chunk(**kwargs)
参数
-
path
Union[str, pathlib.Path] (可选)#将上次调用tracing.start_chunk()以来收集的追踪导出到给定路径的文件中。
返回