跳到主要内容

追踪

用于收集和保存 Playwright 追踪的 API。Playwright 追踪可以在 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.net.cn");
context.tracing().stop(new Tracing.StopOptions()
.setPath(Paths.get("trace.zip")));

方法

group

添加于: v1.49 tracing.group
注意

当可用时,请使用 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();

参数

  • name String#

    在追踪查看器中显示的分组名称。

  • options Tracing.GroupOptions (可选)

    • setLocation Location (可选)#
      • setFile String

      • setLine int (可选)

      • setColumn int (可选)

      指定组在追踪查看器中显示的自定义位置。默认为 Tracing.group() 调用的位置。

返回值


groupEnd

添加于: v1.49 tracing.groupEnd

关闭由 Tracing.group() 创建的最后一个组。

用法

Tracing.groupEnd();

返回值


start

添加于: v1.12 tracing.start

开始追踪。

用法

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 (可选)
    • setName String (可选)#

      如果指定,中间追踪文件将保存到 setTracesDir 目录中,文件名带有给定的前缀,该目录在 BrowserType.launch() 中指定。要指定最终追踪 zip 文件名,您需要将 path 选项传递给 Tracing.stop()

    • setScreenshots boolean (可选)#

      是否在追踪期间捕获屏幕截图。屏幕截图用于构建时间线预览。

    • setSnapshots boolean (可选)#

      如果此选项为 true,追踪将

      • 在每个操作上捕获 DOM 快照
      • 记录网络活动
    • setSources boolean (可选)添加于: v1.17#

      是否包含追踪操作的源文件。应用程序源代码目录列表必须通过 PLAYWRIGHT_JAVA_SRC 环境变量提供(路径在 Windows 上应以 ';' 分隔,在其他平台上应以 ':' 分隔)。

    • setTitle String (可选)添加于: v1.17#

      要在 Trace Viewer 中显示的追踪名称。

返回值


startChunk

添加于: v1.15 tracing.startChunk

开始新的追踪块。如果您想在同一个 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 (可选)
    • setName String (可选)添加于: v1.32#

      如果指定,中间追踪文件将保存到 setTracesDir 目录中,文件名带有给定的前缀,该目录在 BrowserType.launch() 中指定。要指定最终追踪 zip 文件名,您需要将 path 选项传递给 Tracing.stopChunk()

    • setTitle String (可选)添加于: v1.17#

      要在 Trace Viewer 中显示的追踪名称。

返回值


stop

添加于: v1.12 tracing.stop

停止追踪。

用法

Tracing.stop();
Tracing.stop(options);

参数

  • options Tracing.StopOptions (可选)
    • setPath Path (可选)#

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

返回值


stopChunk

添加于: v1.15 tracing.stopChunk

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

用法

Tracing.stopChunk();
Tracing.stopChunk(options);

参数

  • options Tracing.StopChunkOptions (可选)
    • setPath Path (可选)#

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

返回值