SnapshotAssertions
Playwright 提供了比較頁面和元素螢幕截圖與儲存在檔案中預期值的方法。
expect(screenshot).toMatchSnapshot('landing-page.png');
方法
toMatchSnapshot(name)
新增於:v1.22注意
若要比較螢幕截圖,請改用 expect(page).toHaveScreenshot()。
確保傳入的值(字串或 Buffer)與儲存在測試快照目錄中的預期快照相符。
用法
// Basic usage.
expect(await page.screenshot()).toMatchSnapshot('landing-page.png');
// Pass options to customize the snapshot comparison and have a generated name.
expect(await page.screenshot()).toMatchSnapshot('landing-page.png', {
maxDiffPixels: 27, // allow no more than 27 different pixels.
});
// Configure image matching threshold.
expect(await page.screenshot()).toMatchSnapshot('landing-page.png', { threshold: 0.3 });
// Bring some structure to your snapshot files by passing file path segments.
expect(await page.screenshot()).toMatchSnapshot(['landing', 'step2.png']);
expect(await page.screenshot()).toMatchSnapshot(['landing', 'step3.png']);
深入了解視覺比較。
請注意,快照比對僅適用於 Playwright 測試執行器。
參數
-
快照名稱。
-
options
物件 (選填)
toMatchSnapshot(options)
新增於:v1.22注意
若要比較螢幕截圖,請改用 expect(page).toHaveScreenshot()。
確保傳入的值(字串或 Buffer)與儲存在測試快照目錄中的預期快照相符。
用法
// Basic usage and the file name is derived from the test name.
expect(await page.screenshot()).toMatchSnapshot();
// Pass options to customize the snapshot comparison and have a generated name.
expect(await page.screenshot()).toMatchSnapshot({
maxDiffPixels: 27, // allow no more than 27 different pixels.
});
// Configure image matching threshold and snapshot name.
expect(await page.screenshot()).toMatchSnapshot({
name: 'landing-page.png',
threshold: 0.3,
});
深入了解視覺比較。
請注意,快照比對僅適用於 Playwright 測試執行器。
參數