Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/src/test-snapshots-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ await expect(page).toHaveScreenshot('landing.webp');
> Note that `toHaveScreenshot()` also accepts an array of path segments to the snapshot file such as `expect().toHaveScreenshot(['relative', 'path', 'to', 'snapshot.png'])`.
> However, this path must stay within the snapshots directory for each test file (i.e. `a.spec.js-snapshots`), otherwise it will throw.

Mouse actions such as [`method: Locator.click`] leave the pointer at its last position. If the page changes afterwards, another element can end up under the pointer and appear hovered in the screenshot. To avoid this, move the mouse outside the viewport before taking the screenshot.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make a separate section "Hover effects" and put it right before "Options".

In the section, explain that screenshots contain any hover effects present in the page at the moment, and if one would like to avoid any hover effects, we recommend to move the mouse somewhere, or hover an element that has no effects.

A two-line snippet with mouse.move + screenshot would be nice there.


<details>
<summary>Helper for repeated named screenshots</summary>

```ts title="test-utils.ts"
import {
expect,
type Page,
type PageAssertionsToHaveScreenshotOptions,
} from '@playwright/test';

export async function expectPageToHaveScreenshotWithMouseOutsideViewport(
page: Page,
screenshotName: string | ReadonlyArray<string>,
options?: PageAssertionsToHaveScreenshotOptions,
) {
await page.mouse.move(-1, -1);
await expect(page).toHaveScreenshot(screenshotName, options);
}
```

```ts title="example.spec.ts"
await expectPageToHaveScreenshotWithMouseOutsideViewport(page, 'landing.png', {
fullPage: true,
});
```

</details>

@karlhorky Karl Horky (karlhorky) Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case the helper function seems like too much, a simpler alternative without a helper function could be this:

await page.mouse.move(-1, -1);
await expect(page).toHaveScreenshot();


## Updating screenshots

Sometimes you need to update the reference screenshot, for example when the page has changed. Do this with the `--update-snapshots` flag.
Expand Down