Skip to main content

SnapshotAssertions

Playwright 提供了将页面和元素屏幕截图与文件中存储的预期值进行比较的方法。

🌐 Playwright provides methods for comparing page and element screenshots with expected values stored in files.

expect(screenshot).toMatchSnapshot('landing-page.png');

方法

🌐 Methods

toMatchSnapshot(name)

Added in: v1.22 snapshotAssertions.toMatchSnapshot(name)
caution

要比较截图,请使用 expect(page).toHaveScreenshot() 替代。

🌐 To compare screenshots, use expect(page).toHaveScreenshot() instead.

确保传入的值,无论是string还是Buffer,都与存储在测试快照目录中的预期快照匹配。

🌐 Ensures that passed value, either a string or a Buffer, matches the expected snapshot stored in the test snapshots directory.

用法

// 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']);

了解更多有关视觉比较的信息。

🌐 Learn more about visual comparisons.

请注意,匹配快照仅适用于 Playwright 测试运行程序。

🌐 Note that matching snapshots only work with Playwright test runner.

参数

  • name string | Array<string>#

    快照名称。

  • options Object (optional)

    • maxDiffPixelRatio number (optional)#

      可接受的不同像素与总像素的比例,介于 01 之间。默认值可通过 TestConfig.expect 配置。默认未设置。

    • maxDiffPixels number (optional)#

      可以不同的像素数量是可接受的。默认值可以通过 TestConfig.expect 配置。默认情况下未设置。

    • threshold number (optional)#

      YIQ 色彩空间中,对比图片中相同像素可接受的感知颜色差异,介于零(严格)到一(宽松)之间,默认值可通过 TestConfig.expect 配置。默认值为 0.2


toMatchSnapshot(options)

Added in: v1.22 snapshotAssertions.toMatchSnapshot(options)
caution

要比较截图,请使用 expect(page).toHaveScreenshot() 替代。

🌐 To compare screenshots, use expect(page).toHaveScreenshot() instead.

确保传入的值,无论是string还是Buffer,都与存储在测试快照目录中的预期快照匹配。

🌐 Ensures that passed value, either a string or a Buffer, matches the expected snapshot stored in the test snapshots directory.

用法

// 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,
});

了解更多有关视觉比较的信息。

🌐 Learn more about visual comparisons.

请注意,匹配快照仅适用于 Playwright 测试运行程序。

🌐 Note that matching snapshots only work with Playwright test runner.

参数

  • options Object (optional)
    • maxDiffPixelRatio number (optional)#

      可接受的不同像素与总像素的比例,介于 01 之间。默认值可通过 TestConfig.expect 配置。默认未设置。

    • maxDiffPixels number (optional)#

      可以不同的像素数量是可接受的。默认值可以通过 TestConfig.expect 配置。默认情况下未设置。

    • name string | Array<string> (optional)#

      快照名称。如果未提供,将在多次调用时使用测试名称和序号。

    • threshold number (optional)#

      YIQ 色彩空间中,对比图片中相同像素可接受的感知颜色差异,介于零(严格)到一(宽松)之间,默认值可通过 TestConfig.expect 配置。默认值为 0.2