SnapshotAssertions
Playwright 提供了比较页面和元素截图与文件中存储的预期值的方法。
expect(screenshot).toMatchSnapshot('landing-page.png');
方法
toMatchSnapshot(name)
添加于: v1.22警告
如需比较截图,请改用 expect(page).toHaveScreenshot()。
确保传入的值(可以是 string 或 Buffer)与测试快照目录中存储的预期快照匹配。
用法
// 基本用法
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 测试运行器。
参数
-
快照名称
-
options
Object (可选)
toMatchSnapshot(options)
添加于: v1.22警告
如需比较截图,请改用 expect(page).toHaveScreenshot()。
确保传入的值(可以是 string 或 Buffer)与存储在测试快照目录中的预期快照匹配。
用法
// 基本用法,文件名从测试名称派生
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 测试运行器。
参数
options
Object (可选)