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)
提醒

要比较屏幕截图,请改用 expect(page).toHaveScreenshot()

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

确保传递的值(string缓冲)与测试快照目录中存储的预期快照匹配。

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

用法

Usage

// 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.

参数

Arguments

快照名称。

Snapshot name.

可接受的不同于像素总数的像素比例,介于 01 之间。默认值可通过 TestConfig.expect 配置。默认情况下取消设置。

An acceptable ratio of pixels that are different to the total amount of pixels, between 0 and 1. Default is configurable with TestConfig.expect. Unset by default.

可接受的像素数量,可能有所不同。默认值可通过 TestConfig.expect 配置。默认情况下取消设置。

An acceptable amount of pixels that could be different. Default is configurable with TestConfig.expect. Unset by default.

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

An acceptable perceived color difference in the YIQ color space between the same pixel in compared images, between zero (strict) and one (lax), default is configurable with TestConfig.expect. Defaults to 0.2.


toMatchSnapshot()

Added in: v1.22 snapshotAssertions.toMatchSnapshot()
提醒

要比较屏幕截图,请改用 expect(page).toHaveScreenshot()

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

确保传递的值(string缓冲)与测试快照目录中存储的预期快照匹配。

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

用法

Usage

// 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.

参数

Arguments

可接受的不同于像素总数的像素比例,介于 01 之间。默认值可通过 TestConfig.expect 配置。默认情况下取消设置。

An acceptable ratio of pixels that are different to the total amount of pixels, between 0 and 1. Default is configurable with TestConfig.expect. Unset by default.

可接受的像素数量,可能有所不同。默认值可通过 TestConfig.expect 配置。默认情况下取消设置。

An acceptable amount of pixels that could be different. Default is configurable with TestConfig.expect. Unset by default.

快照名称。如果未通过,则多次调用时将使用测试名称和序数。

Snapshot name. If not passed, the test name and ordinals are used when called multiple times.

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

An acceptable perceived color difference in the YIQ color space between the same pixel in compared images, between zero (strict) and one (lax), default is configurable with TestConfig.expect. Defaults to 0.2.