Skip to content

docs(test): document mouse-outside-viewport screenshot helper - #42446

Open
Karl Horky (karlhorky) wants to merge 5 commits into
microsoft:mainfrom
karlhorky:docs/screenshot-mouse-position
Open

docs(test): document mouse-outside-viewport screenshot helper#42446
Karl Horky (karlhorky) wants to merge 5 commits into
microsoft:mainfrom
karlhorky:docs/screenshot-mouse-position

Conversation

@karlhorky

@karlhorky Karl Horky (karlhorky) commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Mouse actions leave the pointer at their last position. If a navigation or re-render places another element under that position, expect(page).toHaveScreenshot() can capture an unrelated hover state.

Playwright keeps this browser behavior as-is in Firefox:

Andrey Lushnikov (@aslushnikov) in comment 1532168395: But a quick workaround would be to move mouse away before taking screenshot.

Chromium also preserves the last mouse position:

Dmitry Gozman (@dgozman) in comment 2493986807: We move it to (0;0) expecting that to be the most predictable position with the least number of side effects.

This adds concise guidance to the Visual comparisons page and a collapsed helper for repeated named screenshots. The helper moves the mouse to -1, -1 before calling toHaveScreenshot() and forwards its screenshot options.

The documentation notes that projects should choose another coordinate when an element at the viewport origin responds to hover.

  • Explain how preserved mouse position can affect screenshots
  • Add a collapsed helper for repeated named screenshots
  • Forward Playwright screenshot assertion options

Copilot AI lite review requested due to automatic review settings August 28, 2026 17:14
});
```

</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();

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Updates Playwright Test visual comparison documentation to explain how preserved mouse position can unintentionally introduce hover states into screenshot assertions, and provides a reusable helper pattern to mitigate it.

Changes:

  • Document why mouse position persistence can affect expect(page).toHaveScreenshot() results after navigations/rerenders.
  • Add a collapsible TypeScript helper that moves the mouse to (0, 0) before taking named screenshots.
  • Show an example of forwarding toHaveScreenshot() options through the helper.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread docs/src/test-snapshots-js.md Outdated
Comment thread docs/src/test-snapshots-js.md Outdated
screenshotName: string,
options?: PageAssertionsToHaveScreenshotOptions,
) {
await page.mouse.move(0, 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(0, 0) is still inside the viewport and commonly hovers a header or any other full width element

(-1, -1) reliably clears hover across all three browsers and matches Playwright’s internal reset behavior

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.

Oh interesting, I didn't find that position in the codebase - maybe I didn't look hard enough

Done in e158a8f

Comment thread docs/src/test-snapshots-js.md Outdated

export async function expectPageToHaveScreenshotWithMouseAtOrigin(
page: Page,
screenshotName: string,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the helper unnecessarily excludes the supported ReadonlyArray<string>

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.

Done in b49e089

Karl Horky (karlhorky) and others added 4 commits August 29, 2026 08:17
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Karl Horky <karl.horky@gmail.com>
Signed-off-by: Karl Horky <karl.horky@gmail.com>
@karlhorky Karl Horky (karlhorky) changed the title docs(test): document mouse-at-origin screenshot helper docs(test): document mouse-outside-viewport screenshot helper Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants