メインコンテンツにスキップ

SnapshotAssertions

Playwrightは、ページと要素のスクリーンショットを、ファイルに保存された期待値と比較するためのメソッドを提供します。

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

メソッド

toMatchSnapshot(name)

バージョン v1.22 で追加 snapshotAssertions.toMatchSnapshot(name)
注意

スクリーンショットを比較するには、代わりに expect(page).toHaveScreenshot() を使用してください。

渡された値(string または Buffer)が、テストスナップショットディレクトリに保存されている期待されるスナップショットと一致することを確認します。

使用法

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

ビジュアル比較の詳細についてはこちらをご覧ください。

スナップショットのマッチングは、Playwrightテストランナーでのみ機能することに注意してください。

引数

  • name string | Array<string>#

    スナップショット名。

  • options Object (オプション)

    • maxDiffPixelRatio number (オプション)#

      異なるピクセルの、総ピクセル数に対する許容可能な比率。0から1の間。デフォルトは TestConfig.expect で設定可能。デフォルトでは未設定。

    • maxDiffPixels number (オプション)#

      異なる可能性のあるピクセルの許容可能な量。デフォルトは TestConfig.expect で設定可能。デフォルトでは未設定。

    • threshold number (オプション)#

      比較された画像内の同じピクセル間の、YIQ色空間における許容可能な知覚色の差。ゼロ(厳密)から1(緩い)の間。デフォルトは TestConfig.expect で設定可能。デフォルトは 0.2


toMatchSnapshot(options)

バージョン v1.22 で追加 snapshotAssertions.toMatchSnapshot(options)
注意

スクリーンショットを比較するには、代わりに expect(page).toHaveScreenshot() を使用してください。

渡された値(string または Buffer)が、テストスナップショットディレクトリに保存されている期待されるスナップショットと一致することを確認します。

使用法

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

ビジュアル比較の詳細についてはこちらをご覧ください。

スナップショットのマッチングは、Playwrightテストランナーでのみ機能することに注意してください。

引数

  • options Object (オプション)
    • maxDiffPixelRatio number (オプション)#

      異なるピクセルの、総ピクセル数に対する許容可能な比率。0から1の間。デフォルトは TestConfig.expect で設定可能。デフォルトでは未設定。

    • maxDiffPixels number (オプション)#

      異なる可能性のあるピクセルの許容可能な量。デフォルトは TestConfig.expect で設定可能。デフォルトでは未設定。

    • name string | Array<string> (オプション)#

      スナップショット名。渡されない場合、テスト名と序数が複数回呼び出されたときに使用されます。

    • threshold number (オプション)#

      比較された画像内の同じピクセル間の、YIQ色空間における許容可能な知覚色の差。ゼロ(厳密)から1(緩い)の間。デフォルトは TestConfig.expect で設定可能。デフォルトは 0.2