-
Notifications
You must be signed in to change notification settings - Fork 6.4k
docs(test): document mouse-outside-viewport screenshot helper #42446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Karl Horky (karlhorky)
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
karlhorky:docs/screenshot-mouse-position
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+30
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
42d64d3
Document mouse position to avoid unintended screenshot hovers
karlhorky b49e089
Potential fix for pull request finding
karlhorky e158a8f
Reset mouse position to -1, -1 as per review feedback
karlhorky ca838fc
Describe off-viewport mouse position for screenshot helper
karlhorky cb4ff84
Rename screenshot helper for off-viewport mouse position
karlhorky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
||
| <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> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.