SnapshotAssertions
Playwright 提供了用于将页面和元素屏幕截图与文件中存储的预期值进行比较的方法。
expect(screenshot).toMatchSnapshot('landing-page.png');
方法
toMatchSnapshot(name)
添加于:v1.22注意
要比较屏幕截图,请使用 expect(page).toHaveScreenshot() 代替。
确保传递的值(string 或 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
Object (可选)
toMatchSnapshot(options)
添加于:v1.22注意
要比较屏幕截图,请使用 expect(page).toHaveScreenshot() 代替。
确保传递的值(string 或 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 测试运行器。
参数
options
Object (可选)