2021-05-27 20:30:03 -07:00
---
id: test-snapshots
2021-05-31 22:01:21 -07:00
title: "Visual comparisons"
2021-05-27 20:30:03 -07:00
---
2021-05-31 22:01:21 -07:00
Playwright Test includes the ability to produce and visually compare screenshots using `expect(value).toMatchSnapshot()` . On first execution, Playwright test will generate reference screenshots. Subsequent runs will compare against the reference.
2021-05-27 20:30:03 -07:00
2021-06-02 15:31:51 -07:00
```js js-flavor=js
2021-05-31 20:17:15 -07:00
// example.spec.js
2021-06-03 14:46:58 -07:00
const { test, expect } = require('@playwright/test ');
2021-05-31 20:17:15 -07:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
expect(await page.screenshot()).toMatchSnapshot('optional-snapshot-name.png');
});
```
2021-06-02 15:31:51 -07:00
```js js-flavor=ts
2021-05-27 20:30:03 -07:00
// example.spec.ts
2021-06-03 14:46:58 -07:00
import { test, expect } from '@playwright/test ';
2021-05-27 20:30:03 -07:00
2021-05-31 20:17:15 -07:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
expect(await page.screenshot()).toMatchSnapshot('optional-snapshot-name.png');
2021-05-27 20:30:03 -07:00
});
```
2021-05-31 22:01:21 -07: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 09:23:06 -07:00
```bash
2021-05-31 22:01:21 -07: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-02 15:31:51 -07:00
```js js-flavor=js
2021-05-31 22:01:21 -07:00
// example.spec.js
2021-06-03 14:46:58 -07:00
const { test, expect } = require('@playwright/test ');
2021-05-31 22:01:21 -07:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
expect(await page.screenshot()).toMatchSnapshot({ threshold: 0.2 });
});
```
2021-06-02 15:31:51 -07:00
```js js-flavor=ts
2021-05-31 22:01:21 -07:00
// example.spec.ts
2021-06-03 14:46:58 -07:00
import { test, expect } from '@playwright/test ';
2021-05-31 22:01:21 -07:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
expect(await page.screenshot()).toMatchSnapshot({ threshold: 0.2 });
});
```
Apart from screenshots, `expect(value).toMatchSnapshot()` 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.
Here we compare text content against the reference.
2021-06-02 15:31:51 -07:00
```js js-flavor=js
2021-05-31 22:01:21 -07:00
// example.spec.js
2021-06-03 14:46:58 -07:00
const { test, expect } = require('@playwright/test ');
2021-05-31 22:01:21 -07:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
expect(await page.textContent('.hero__title')).toMatchSnapshot();
});
```
2021-06-02 15:31:51 -07:00
```js js-flavor=ts
2021-05-31 22:01:21 -07:00
// example.spec.ts
2021-06-03 14:46:58 -07:00
import { test, expect } from '@playwright/test ';
2021-05-31 22:01:21 -07:00
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
expect(await page.textContent('.hero__title')).toMatchSnapshot();
});
```
2021-05-31 20:17:15 -07:00
Snapshots are stored next to the test file, in a separate directory. For example, `my.spec.js` file will produce and store snapshots in the `my.spec.js-snapshots` directory. You should commit this directory to your version control (e.g. `git` ), and review any changes to it.