Screenshot APIs
When to Use
Use
toHaveScreenshot()for all VR work. Usepage.screenshot()only when you need a raw buffer for non-test purposes. UsetoMatchSnapshot()only when you already have a buffer and need to diff it.
Decision
| API | Type | Auto-retry stability | Baseline diffing | Use for |
|---|---|---|---|---|
expect(page).toHaveScreenshot() |
Assertion | Yes — waits for two consecutive matching frames | Yes (pixelmatch) | Primary VR API |
expect(buffer).toMatchSnapshot() |
Assertion | No | Yes for images | Generic snapshot; buffer already captured |
page.screenshot() |
Action | No | No | Raw capture; returns Buffer or writes file |
Pattern
Minimal VR test
import { test, expect } from '@playwright/test';
test('homepage', async ({ page }) => {
await page.goto('https://playwright.dev');
await expect(page).toHaveScreenshot();
});
Element-level (preferred for components)
await expect(page.locator('.header')).toHaveScreenshot('header.png');
Common Mistakes
- Wrong: Using
page.screenshot()for VR → Right: it produces a file but does no comparison; the test always passes - Wrong: Using
toMatchSnapshot()on a buffer when you wantedtoHaveScreenshot()→ Right: you lose the auto-retry stability check - Wrong: Inconsistent naming —
toHaveScreenshot('thing')vstoHaveScreenshot('Thing.png')→ Right: name consistently; casing produces different baseline filenames