跳到主要內容

Tracing

用於收集和儲存 Playwright 追蹤的 API。Playwright 追蹤可以在 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.dev.org.tw");
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.dev.org.tw/");
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.dev.org.tw");
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#

      追蹤名稱將顯示在追蹤檢視器中。

返回


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.dev.org.tw");

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#

      追蹤名稱將顯示在追蹤檢視器中。

返回


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() 呼叫以來收集的追蹤匯出到具有指定路徑的檔案中。

返回