Tracing
用于收集和保存 Playwright 追踪的 API。Playwright 脚本运行后,可以在追踪查看器中打开 Playwright 追踪。
您可能希望在您的配置文件中启用追踪,而不是使用context.tracing
。
context.tracing
API 捕获浏览器操作和网络活动,但它不记录测试断言(如 expect
调用)。我们建议通过 Playwright 测试配置启用追踪,这包括这些断言,并为调试测试失败提供更完整的追踪。
在执行操作之前开始记录追踪。最后,停止追踪并将其保存到文件中。
Browser browser = chromium.launch();
BrowserContext context = browser.newContext();
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true)
.setSnapshots(true));
Page page = context.newPage();
page.navigate("https://playwright.net.cn");
context.tracing().stop(new Tracing.StopOptions()
.setPath(Paths.get("trace.zip")));
方法
group
新增于: v1.49如果可用,请改用test.step
。
在追踪中创建一个新组,将任何后续 API 调用分配到此组,直到调用Tracing.groupEnd()。组可以嵌套,并将在追踪查看器中可见。
用法
// All actions between group and groupEnd
// will be shown in the trace viewer as a group.
page.context().tracing().group("Open Playwright.dev > API");
page.navigate("https://playwright.net.cn/");
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("API")).click();
page.context().tracing().groupEnd();
参数
-
在追踪查看器中显示的组名。
-
options
Tracing.GroupOptions
(可选)-
setLocation
位置 (可选)#指定组在追踪查看器中显示的自定义位置。默认为Tracing.group()调用的位置。
-
返回
groupEnd
新增于: v1.49关闭由Tracing.group()创建的最后一个组。
用法
Tracing.groupEnd();
返回
start
添加于:v1.12开始追踪。
您可能希望在您的配置文件中启用追踪,而不是使用Tracing.start
。
context.tracing
API 捕获浏览器操作和网络活动,但它不记录测试断言(如 expect
调用)。我们建议通过 Playwright 测试配置启用追踪,这包括这些断言,并为调试测试失败提供更完整的追踪。
用法
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true)
.setSnapshots(true));
Page page = context.newPage();
page.navigate("https://playwright.net.cn");
context.tracing().stop(new Tracing.StopOptions()
.setPath(Paths.get("trace.zip")));
参数
options
Tracing.StartOptions
(可选)-
如果指定,中间追踪文件将以给定的名称前缀保存在BrowserType.launch()中指定的setTracesDir目录中。要指定最终的追踪 zip 文件名,您需要将
path
选项传递给Tracing.stop()。 -
是否在追踪期间捕获屏幕截图。屏幕截图用于构建时间线预览。
-
如果此选项为 true,追踪将
- 在每次操作时捕获 DOM 快照
- 记录网络活动
-
setSources
布尔值 (可选)新增于: v1.17#是否包含追踪操作的源文件。应用程序源代码的目录列表必须通过
PLAYWRIGHT_JAVA_SRC
环境变量提供(在 Windows 上路径应以 ';' 分隔,在其他平台上以 ':' 分隔)。 -
在追踪查看器中显示的追踪名称。
-
返回
startChunk
新增于: v1.15开始一个新的追踪块。如果您想在同一个BrowserContext上记录多个追踪,请使用Tracing.start()一次,然后使用Tracing.startChunk()和Tracing.stopChunk()创建多个追踪块。
用法
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true)
.setSnapshots(true));
Page page = context.newPage();
page.navigate("https://playwright.net.cn");
context.tracing().startChunk();
page.getByText("Get Started").click();
// Everything between startChunk and stopChunk will be recorded in the trace.
context.tracing().stopChunk(new Tracing.StopChunkOptions()
.setPath(Paths.get("trace1.zip")));
context.tracing().startChunk();
page.navigate("http://example.com");
// Save a second trace file with different actions.
context.tracing().stopChunk(new Tracing.StopChunkOptions()
.setPath(Paths.get("trace2.zip")));
参数
options
Tracing.StartChunkOptions
(可选)-
如果指定,中间追踪文件将以给定的名称前缀保存在BrowserType.launch()中指定的setTracesDir目录中。要指定最终的追踪 zip 文件名,您需要将
path
选项传递给Tracing.stopChunk()。 -
在追踪查看器中显示的追踪名称。
-
返回
stop
添加于:v1.12停止追踪。
用法
Tracing.stop();
Tracing.stop(options);
参数
返回
stopChunk
新增于: v1.15停止追踪块。有关多个追踪块的更多详细信息,请参见Tracing.startChunk()。
用法
Tracing.stopChunk();
Tracing.stopChunk(options);
参数
options
Tracing.StopChunkOptions
(可选)-
将自上次调用Tracing.startChunk()以来收集的追踪导出到给定路径的文件中。
-
返回