追踪
用于收集和保存Playwright跟踪记录的API。Playwright跟踪记录可以在脚本运行后通过Trace Viewer查看。
在执行操作前开始记录追踪。最后停止追踪并将其保存到文件中。
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.dev");
context.tracing().stop(new Tracing.StopOptions()
.setPath(Paths.get("trace.zip")));
方法
分组
Added in: 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.dev/");
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("API")).click();
page.context().tracing().groupEnd();
参数
-
在追踪查看器中显示的组名。
-
options
Tracing.GroupOptions
(可选)setLocation
Location (optional)# Specifies a custom location for the group to be shown in the trace viewer. Defaults to the location of the Tracing.group() call.
返回
groupEnd
Added in: v1.49关闭由Tracing.group()创建的最后分组。
用法
Tracing.groupEnd();
返回
开始
Added in: v1.12开始追踪。
用法
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true)
.setSnapshots(true));
Page page = context.newPage();
page.navigate("https://playwright.dev");
context.tracing().stop(new Tracing.StopOptions()
.setPath(Paths.get("trace.zip")));
参数
options
Tracing.StartOptions
(optional)-
如果指定该参数,中间跟踪文件将被保存到BrowserType.launch()中指定的setTracesDir目录下,并以给定名称作为前缀。要指定最终的跟踪zip文件名,您需要改为向Tracing.stop()传递
path
选项。 -
是否在跟踪过程中捕获屏幕截图。屏幕截图用于构建时间线预览。
-
如果此选项为true,追踪将
- 在每次操作时捕获DOM快照
- 记录网络活动
-
setSources
boolean (可选) 新增于: v1.17#是否包含跟踪操作的源文件。必须通过
PLAYWRIGHT_JAVA_SRC
环境变量提供应用程序的源代码目录列表(在Windows上路径应该用';'分隔,在其他平台上用':'分隔)。 -
setTitle
String (可选) 添加于: v1.17#在Trace Viewer中显示的追踪名称。
-
返回
startChunk
Added in: 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.dev");
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
(optional)-
setName
String (可选) 添加于: v1.32#如果指定该参数,中间跟踪文件将被保存到BrowserType.launch()中指定的setTracesDir目录下,并以给定名称作为前缀。要指定最终的跟踪zip文件名,您需要改为向Tracing.stopChunk()传递
path
选项。 -
setTitle
String (可选) 添加于: v1.17#在Trace Viewer中显示的追踪名称。
-
返回
停止
Added in: v1.12停止追踪。
用法
Tracing.stop();
Tracing.stop(options);
参数
返回
stopChunk
Added in: v1.15停止跟踪块。有关多个跟踪块的更多详情,请参阅Tracing.startChunk()。
用法
Tracing.stopChunk();
Tracing.stopChunk(options);
参数
options
Tracing.StopChunkOptions
(optional)-
将自上次Tracing.startChunk()调用以来收集的跟踪数据导出到指定路径的文件中。
-
返回