SnapshotAssertions
Added in: v1.20Playwright 提供了用于将页面和元素截图与存储在文件中的预期值进行比较的方法。
js
expect(screenshot).toMatchSnapshot('landing-page.png');方法
snapshotAssertions.toMatchSnapshot()
Added in: v1.22WARNING
要比较截图,请改用 pageAssertions.toHaveScreenshot()。
确保传入的值([string] 或 [Buffer])与存储在测试快照目录中的预期快照匹配。
用法
js
// 基本用法。
expect(await page.screenshot()).toMatchSnapshot('landing-page.png');
// 传递选项以自定义快照比较并使用生成的名称。
expect(await page.screenshot()).toMatchSnapshot('landing-page.png', {
maxDiffPixels: 27, // 允许最多 27 个不同的像素。
});
// 配置图像匹配阈值。
expect(await page.screenshot()).toMatchSnapshot('landing-page.png', { threshold: 0.3 });
// 通过传递文件路径段为快照文件提供一些结构。
expect(await page.screenshot()).toMatchSnapshot(['landing', 'step2.png']);
expect(await page.screenshot()).toMatchSnapshot(['landing', 'step3.png']);了解更多关于视觉比较的信息。
注意,快照匹配仅适用于 Playwright 测试运行器。
参数
快照名称。
snapshotAssertions.toMatchSnapshot()
Added in: v1.22WARNING
要比较截图,请改用 pageAssertions.toHaveScreenshot()。
确保传入的值([string] 或 [Buffer])与存储在测试快照目录中的预期快照匹配。
用法
js
// 基本用法,文件名从测试名称派生。
expect(await page.screenshot()).toMatchSnapshot();
// 传递选项以自定义快照比较并使用生成的名称。
expect(await page.screenshot()).toMatchSnapshot({
maxDiffPixels: 27, // 允许最多 27 个不同的像素。
});
// 配置图像匹配阈值和快照名称。
expect(await page.screenshot()).toMatchSnapshot({
name: 'landing-page.png',
threshold: 0.3,
});了解更多关于视觉比较的信息。
注意,快照匹配仅适用于 Playwright 测试运行器。
参数