2022-11-29 16:57:03 -08:00
|
|
|
# class: SnapshotAssertions
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.20
|
2022-03-03 10:06:14 -08:00
|
|
|
* langs: js
|
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
:::tip
|
|
|
|
For visual regression testing, use [`method: PageAssertions.toHaveScreenshot#1`] and [`method: LocatorAssertions.toHaveScreenshot#1`] instead.
|
|
|
|
:::
|
|
|
|
|
|
|
|
Playwright provides methods for comparing text values with expected values stored in snapshot files.
|
2022-03-03 10:06:14 -08:00
|
|
|
|
|
|
|
```js
|
2023-08-25 18:20:01 -07:00
|
|
|
expect(text).toMatchSnapshot('snapshot.txt');
|
2022-03-03 10:06:14 -08:00
|
|
|
```
|
|
|
|
|
2022-11-29 16:57:03 -08:00
|
|
|
## method: SnapshotAssertions.toMatchSnapshot#1
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.22
|
2022-03-03 10:06:14 -08:00
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
Ensures that the passed [string] matches the expected snapshot stored in the test snapshots directory.
|
|
|
|
|
|
|
|
**Usage**
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Basic usage.
|
|
|
|
expect(await page.title()).toMatchSnapshot('page-title.txt');
|
|
|
|
|
|
|
|
// Bring some structure to your snapshot files by passing file path segments.
|
|
|
|
expect(await page.title()).toMatchSnapshot(['page-title', 'step1.txt']);
|
|
|
|
expect(await page.title()).toMatchSnapshot(['page-title', 'step2.txt']);
|
|
|
|
```
|
|
|
|
|
|
|
|
Note that matching snapshots only work with Playwright test runner.
|
|
|
|
|
|
|
|
### param: SnapshotAssertions.toMatchSnapshot#1.name
|
|
|
|
* since: v1.22
|
|
|
|
- `name` <[string]|[Array]<[string]>>
|
|
|
|
|
|
|
|
Snapshot name.
|
|
|
|
|
|
|
|
## method: SnapshotAssertions.toMatchSnapshot#2
|
|
|
|
* since: v1.22
|
|
|
|
|
2022-03-03 10:06:14 -08:00
|
|
|
Ensures that passed value, either a [string] or a [Buffer], matches the expected snapshot stored in the test snapshots directory.
|
|
|
|
|
2022-11-21 10:40:21 -08:00
|
|
|
**Usage**
|
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
```js
|
|
|
|
// Basic usage.
|
|
|
|
expect(await page.title()).toMatchSnapshot();
|
|
|
|
|
|
|
|
// Configure the snapshot name.
|
|
|
|
expect(await page.title()).toMatchSnapshot({
|
|
|
|
name: 'page-title.txt',
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
Note that matching snapshots only work with Playwright test runner.
|
|
|
|
|
|
|
|
### option: SnapshotAssertions.toMatchSnapshot#2.name
|
|
|
|
* since: v1.22
|
|
|
|
- `name` <[string]|[Array]<[string]>>
|
|
|
|
|
|
|
|
Snapshot name. If not passed, the test name and ordinals are used when called multiple times.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## method: SnapshotAssertions.toMatchSnapshot#3
|
|
|
|
* deprecated: To avoid flakiness, use [`method: PageAssertions.toHaveScreenshot#1`] instead.
|
|
|
|
* since: v1.22
|
|
|
|
|
|
|
|
Ensures that the passed [Buffer] matches the expected snapshot stored in the test snapshots directory.
|
|
|
|
|
|
|
|
**Usage**
|
|
|
|
|
2022-03-03 10:06:14 -08:00
|
|
|
```js
|
|
|
|
// Basic usage.
|
|
|
|
expect(await page.screenshot()).toMatchSnapshot('landing-page.png');
|
|
|
|
|
2022-04-05 15:34:04 +02:00
|
|
|
// Pass options to customize the snapshot comparison and have a generated name.
|
2022-04-11 10:42:19 -07:00
|
|
|
expect(await page.screenshot()).toMatchSnapshot('landing-page.png', {
|
2022-04-05 15:34:04 +02:00
|
|
|
maxDiffPixels: 27, // allow no more than 27 different pixels.
|
|
|
|
});
|
|
|
|
|
2022-03-03 10:06:14 -08:00
|
|
|
// 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']);
|
|
|
|
```
|
|
|
|
|
2022-11-24 08:25:24 -08:00
|
|
|
Learn more about [visual comparisons](../test-snapshots.md).
|
2022-03-03 10:06:14 -08:00
|
|
|
|
2022-11-28 10:32:48 -08:00
|
|
|
Note that matching snapshots only work with Playwright test runner.
|
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
### param: SnapshotAssertions.toMatchSnapshot#3.name
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.22
|
2022-04-11 10:42:19 -07:00
|
|
|
- `name` <[string]|[Array]<[string]>>
|
|
|
|
|
|
|
|
Snapshot name.
|
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
### option: SnapshotAssertions.toMatchSnapshot#3.maxDiffPixels = %%-assertions-max-diff-pixels-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.22
|
2022-04-11 10:42:19 -07:00
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
### option: SnapshotAssertions.toMatchSnapshot#3.maxDiffPixelRatio = %%-assertions-max-diff-pixel-ratio-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.22
|
2022-04-11 10:42:19 -07:00
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
### option: SnapshotAssertions.toMatchSnapshot#3.threshold = %%-assertions-threshold-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.22
|
2022-04-11 10:42:19 -07:00
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
## method: SnapshotAssertions.toMatchSnapshot#4
|
|
|
|
* deprecated: To avoid flakiness, use [`method: PageAssertions.toHaveScreenshot#1`] instead.
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.22
|
2022-04-11 10:42:19 -07:00
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
Ensures that the passed [Buffer] matches the expected snapshot stored in the test snapshots directory.
|
2022-04-11 10:42:19 -07:00
|
|
|
|
2022-11-21 10:40:21 -08:00
|
|
|
**Usage**
|
|
|
|
|
2022-04-11 10:42:19 -07:00
|
|
|
```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,
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2022-11-24 08:25:24 -08:00
|
|
|
Learn more about [visual comparisons](../test-snapshots.md).
|
2022-04-11 10:42:19 -07:00
|
|
|
|
2022-11-28 10:32:48 -08:00
|
|
|
Note that matching snapshots only work with Playwright test runner.
|
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
### option: SnapshotAssertions.toMatchSnapshot#4.maxDiffPixels = %%-assertions-max-diff-pixels-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.22
|
2022-04-11 10:42:19 -07:00
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
### option: SnapshotAssertions.toMatchSnapshot#4.maxDiffPixelRatio = %%-assertions-max-diff-pixel-ratio-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.22
|
2022-03-03 10:06:14 -08:00
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
### option: SnapshotAssertions.toMatchSnapshot#4.name
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.22
|
2022-04-11 10:42:19 -07:00
|
|
|
- `name` <[string]|[Array]<[string]>>
|
2022-03-03 10:06:14 -08:00
|
|
|
|
2022-04-11 10:42:19 -07:00
|
|
|
Snapshot name. If not passed, the test name and ordinals are used when called multiple times.
|
2022-03-10 19:41:16 -07:00
|
|
|
|
2023-08-25 18:20:01 -07:00
|
|
|
### option: SnapshotAssertions.toMatchSnapshot#4.threshold = %%-assertions-threshold-%%
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.22
|
2023-08-25 18:20:01 -07:00
|
|
|
|