These are usually used for screenshots instead of toHaveScreenshot. String-based snapshots are left as is. <img width="836" alt="deprecated" src="https://github.com/microsoft/playwright/assets/9881434/d02818da-7eea-47fc-8ea8-4104b0dd49b5"> --- Also deemphasized toMatchSnapshot throughout all docs. Fixes #26612.
3.4 KiB
| id | title |
|---|---|
| test-snapshots | Visual comparisons |
Playwright Test includes the ability to produce and visually compare screenshots using await expect(page).toHaveScreenshot(). On first execution, Playwright test will generate reference screenshots. Subsequent runs will compare against the reference.
import { test, expect } from '@playwright/test';
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
await expect(page).toHaveScreenshot();
});
When you run above for the first time, test runner will say:
Error: A snapshot doesn't exist at example.spec.ts-snapshots/example-test-1-chromium-darwin.png, writing actual.
That's because there was no reference screenshot yet. This method took a bunch of screenshots until two consecutive screenshots matched, and saved the last screenshot to file system. It is now ready to be added to the repository. You should review any changes to the reference screenshots to make sure they are expected.
The name of the folder with the reference screenshots starts with the name of your test file:
drwxr-xr-x 5 user group 160 Jun 4 11:46 .
drwxr-xr-x 6 user group 192 Jun 4 11:45 ..
-rw-r--r-- 1 user group 231 Jun 4 11:16 example.spec.ts
drwxr-xr-x 3 user group 96 Jun 4 11:46 example.spec.ts-snapshots
The name of the reference screenshot example-test-1-chromium-darwin.png consists of a few parts:
-
example-test-1.png- an auto-generated name of the snapshot. Alternatively you can specify snapshot name as the first argument of thetoHaveScreenshot()method:await expect(page).toHaveScreenshot('landing.png'); -
chromium-darwin- the browser name and the platform. Screenshots differ between browsers and platforms due to different rendering, fonts and more, so you will need different snapshots for them. If you use multiple projects in your configuration file, project name will be used instead ofchromium.
You can customize the name and the path of the reference screnshot by modifying [property: TestProject.snapshotPathTemplate] property in the config file.
Sometimes you need to update the reference screenshot, for example when the page has changed. Do this with the --update-snapshots flag.
npx playwright test --update-snapshots
Playwright Test uses the pixelmatch library. You can pass various options to modify its behavior:
import { test, expect } from '@playwright/test';
test('example test', async ({ page }) => {
await page.goto('https://playwright.dev');
await expect(page).toHaveScreenshot({ maxDiffPixels: 100 });
});
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:
import { defineConfig } from '@playwright/test';
export default defineConfig({
expect: {
toHaveScreenshot: { maxDiffPixels: 100 },
},
});
If you are not on the same operating system as your CI system, you can use Docker to generate/update the screenshots:
docker run --rm --network host -v $(pwd):/work/ -w /work/ -it mcr.microsoft.com/playwright:v%%VERSION%%-jammy /bin/bash
npm install
npx playwright test --update-snapshots