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

SnapshotAssertions

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

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

メソッド

toMatchSnapshot(name)

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

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

渡された値(文字列または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()を使用してください。

渡された値(文字列または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です。