追蹤
用於收集和儲存 Playwright 追蹤記錄的 API。Playwright 追蹤記錄可以在 Playwright 腳本執行後於 追蹤檢視器 中開啟。
在執行動作前開始記錄追蹤。結束時,停止追蹤並儲存到檔案。
const browser = await chromium.launch();
const context = await browser.newContext();
await context.tracing.start({ screenshots: true, snapshots: true });
const page = await context.newPage();
await page.goto('https://playwright.dev.org.tw');
await context.tracing.stop({ path: 'trace.zip' });
方法
group
Added in: v1.49在可用的情況下,請改用 test.step
。
在追蹤中建立新的群組,將任何後續的 API 呼叫指派給此群組,直到呼叫 tracing.groupEnd() 為止。群組可以巢狀結構,並且在追蹤檢視器中可見。
用法
// use test.step instead
await test.step('Log in', async () => {
// ...
});
參數
-
群組名稱顯示在追蹤檢視器中。
-
options
Object (選填)
回傳
groupEnd
Added in: v1.49關閉由 tracing.group() 建立的最後一個群組。
用法
await tracing.groupEnd();
回傳
start
Added in: v1.12開始追蹤。
用法
await context.tracing.start({ screenshots: true, snapshots: true });
const page = await context.newPage();
await page.goto('https://playwright.dev.org.tw');
await context.tracing.stop({ path: 'trace.zip' });
參數
options
Object (選填)-
如果指定,中繼追蹤檔案將會儲存到 tracesDir 目錄內,並以給定的名稱作為前綴,該目錄在 browserType.launch() 中指定。若要指定最終的追蹤 zip 檔名,您需要將
path
選項傳遞給 tracing.stop()。 -
是否在追蹤期間擷取螢幕截圖。螢幕截圖用於建立時間軸預覽。
-
如果此選項為 true,追蹤將會
- 在每個動作上擷取 DOM 快照
- 記錄網路活動
-
sources
boolean (選填)Added in: v1.17#是否包含追蹤動作的原始碼檔案。
-
title
string (選填)Added in: v1.17#要在追蹤檢視器中顯示的追蹤名稱。
-
回傳
startChunk
Added in: v1.15開始新的追蹤區塊。如果您想要在同一個 BrowserContext 上記錄多個追蹤,請使用 tracing.start() 一次,然後使用 tracing.startChunk() 和 tracing.stopChunk() 建立多個追蹤區塊。
用法
await context.tracing.start({ screenshots: true, snapshots: true });
const page = await context.newPage();
await page.goto('https://playwright.dev.org.tw');
await context.tracing.startChunk();
await page.getByText('Get Started').click();
// Everything between startChunk and stopChunk will be recorded in the trace.
await context.tracing.stopChunk({ path: 'trace1.zip' });
await context.tracing.startChunk();
await page.goto('http://example.com');
// Save a second trace file with different actions.
await context.tracing.stopChunk({ path: 'trace2.zip' });
參數
options
Object (選填)-
name
string (選填)Added in: v1.32#如果指定,中繼追蹤檔案將會儲存到 tracesDir 目錄內,並以給定的名稱作為前綴,該目錄在 browserType.launch() 中指定。若要指定最終的追蹤 zip 檔名,您需要將
path
選項傳遞給 tracing.stopChunk()。 -
title
string (選填)Added in: v1.17#要在追蹤檢視器中顯示的追蹤名稱。
-
回傳
stop
Added in: v1.12停止追蹤。
用法
await tracing.stop();
await tracing.stop(options);
參數
回傳
stopChunk
Added in: v1.15停止追蹤區塊。請參閱 tracing.startChunk() 以取得有關多個追蹤區塊的更多詳細資訊。
用法
await tracing.stopChunk();
await tracing.stopChunk(options);
參數
options
Object (選填)-
將自上次 tracing.startChunk() 呼叫以來收集的追蹤匯出到具有給定路徑的檔案。
-
回傳