跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

SnapshotAssertions ​

Added in: v1.20

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

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

Methods ​

snapshotAssertions.toMatchSnapshot() ​

Added in: v1.22

WARNING

To compare screenshots, use pageAssertions.toHaveScreenshot() instead.

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

Usage

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

Note that matching snapshots only work with Playwright test runner.

Arguments

  • name string | Array<string>

    Snapshot name.

  • options Object (optional)

    • maxDiffPixelRatio float (optional)

      An acceptable ratio of pixels that are different to the total amount of pixels, between 0 and 1. Default is configurable with TestConfig.expect. Note that threshold defines which pixels are considered different.

    • maxDiffPixels int (optional)

      An acceptable amount of pixels that could be different. Default is configurable with TestConfig.expect. Note that threshold defines which pixels are considered different.

    • threshold float (optional)

      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.

snapshotAssertions.toMatchSnapshot() ​

Added in: v1.22

WARNING

To compare screenshots, use pageAssertions.toHaveScreenshot() instead.

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

Usage

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

Note that matching snapshots only work with Playwright test runner.

Arguments

  • options Object (optional)

    • maxDiffPixelRatio float (optional)

      An acceptable ratio of pixels that are different to the total amount of pixels, between 0 and 1. Default is configurable with TestConfig.expect. Note that threshold defines which pixels are considered different.

    • maxDiffPixels int (optional)

      An acceptable amount of pixels that could be different. Default is configurable with TestConfig.expect. Note that threshold defines which pixels are considered different.

    • name string | Array<string> (optional)

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

    • threshold float (optional)

      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.

基于 Apache 2.0 许可证发布。