2021-05-28 05:30:03 +02:00
---
id: test-snapshots
2021-06-01 07:01:21 +02:00
title: "Visual comparisons"
2021-05-28 05:30:03 +02:00
---
2021-06-05 05:54:58 +02:00
Playwright Test includes the ability to produce and visually compare screenshots using `expect(value).toMatchSnapshot(snapshotName)` . On first execution, Playwright test will generate reference screenshots. Subsequent runs will compare against the reference.
2021-05-28 05:30:03 +02:00
2021-06-03 00:31:51 +02:00
```js js-flavor=js
2021-06-01 05:17:15 +02:00
// example.spec.js
2021-06-03 23:46:58 +02:00
const { test, expect } = require('@playwright/test');
2021-06-01 05:17:15 +02:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
2021-06-05 05:54:58 +02:00
expect(await page.screenshot()).toMatchSnapshot('snapshot-name.png');
2021-06-01 05:17:15 +02:00
});
```
2021-06-03 00:31:51 +02:00
```js js-flavor=ts
2021-05-28 05:30:03 +02:00
// example.spec.ts
2021-06-03 23:46:58 +02:00
import { test, expect } from '@playwright/test';
2021-05-28 05:30:03 +02:00
2021-06-01 05:17:15 +02:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
2021-06-05 05:54:58 +02:00
expect(await page.screenshot()).toMatchSnapshot('snapshot-name.png');
2021-05-28 05:30:03 +02:00
});
```
2021-06-01 07:01:21 +02:00
Sometimes you need to update the reference screenshot, for example when the page has changed. Do this with the `--update-snapshots` flag.
2021-06-02 18:23:06 +02:00
```bash
2021-06-01 07:01:21 +02:00
npx playwright test --update-snapshots
```
Playwright Test uses the [pixelmatch ](https://github.com/mapbox/pixelmatch ) library. You can pass comparison `threshold` as an option.
2021-06-03 00:31:51 +02:00
```js js-flavor=js
2021-06-01 07:01:21 +02:00
// example.spec.js
2021-06-03 23:46:58 +02:00
const { test, expect } = require('@playwright/test');
2021-06-01 07:01:21 +02:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
2021-06-05 05:54:58 +02:00
expect(await page.screenshot()).toMatchSnapshot('home.png', { threshold: 0.2 });
2021-06-01 07:01:21 +02:00
});
```
2021-06-03 00:31:51 +02:00
```js js-flavor=ts
2021-06-01 07:01:21 +02:00
// example.spec.ts
2021-06-03 23:46:58 +02:00
import { test, expect } from '@playwright/test';
2021-06-01 07:01:21 +02:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
2021-06-05 05:54:58 +02:00
expect(await page.screenshot()).toMatchSnapshot('home.png', { threshold: 0.2 });
2021-06-01 07:01:21 +02:00
});
```
2021-06-15 06:52:10 +02:00
If you'd like to share the default value among all the tests in the project, you can specify it in the playwright config, either globally or per project:
```js js-flavor=js
module.exports = {
expect: {
toMatchSnapshot: { threshold: 0.1 },
},
};
```
```js js-flavor=ts
import { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
expect: {
toMatchSnapshot: { threshold: 0.1 },
},
};
export default config;
```
2021-06-05 05:54:58 +02:00
Apart from screenshots, `expect(value).toMatchSnapshot(snapshotName)` can also be used to compare text, png and jpeg images, or arbitrary binary data. Playwright Test auto-detects the content type and uses the appropriate comparison algorithm.
2021-06-01 07:01:21 +02:00
Here we compare text content against the reference.
2021-06-03 00:31:51 +02:00
```js js-flavor=js
2021-06-01 07:01:21 +02:00
// example.spec.js
2021-06-03 23:46:58 +02:00
const { test, expect } = require('@playwright/test');
2021-06-01 07:01:21 +02:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
2021-06-05 05:54:58 +02:00
expect(await page.textContent('.hero__title')).toMatchSnapshot('hero.txt');
2021-06-01 07:01:21 +02:00
});
```
2021-06-03 00:31:51 +02:00
```js js-flavor=ts
2021-06-01 07:01:21 +02:00
// example.spec.ts
2021-06-03 23:46:58 +02:00
import { test, expect } from '@playwright/test';
2021-06-01 07:01:21 +02:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
2021-06-05 05:54:58 +02:00
expect(await page.textContent('.hero__title')).toMatchSnapshot('hero.txt');
2021-06-01 07:01:21 +02:00
});
```
2021-06-05 05:54:58 +02:00
Snapshots are stored next to the test file, in a separate directory. For example, `my.spec.ts` file will produce and store snapshots in the `my.spec.ts-snapshots` directory. You should commit this directory to your version control (e.g. `git` ), and review any changes to it.