Skip to content

[Playwright] Fix ignore region & clip positioning when page is scrolled prior to snapshot #189

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

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions visual-js/.changeset/spotty-pumpkins-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@saucelabs/visual-playwright": patch
---

Correct clipping and ignore regions when page is pre-scrolled
10 changes: 8 additions & 2 deletions visual-js/visual-playwright/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ ${e instanceof Error ? e.message : JSON.stringify(e)}
const clientDims = clipElement.getBoundingClientRect();
let { x, y, height, width } = clientDims;

// Add scroll offset to coordinates in case page is scrolled prior to capture
({ x, y } = {
x: x + window.scrollX,
y: y + window.scrollY,
});

// corrected coordinates
const cX = x < 0 ? Math.abs(x) : 0;
const cY = y < 0 ? Math.abs(y) : 0;
Expand Down Expand Up @@ -243,8 +249,8 @@ ${e instanceof Error ? e.message : JSON.stringify(e)}

selectorRegions.push({
name: selector,
x: Math.round(rect.x),
y: Math.round(rect.y),
x: Math.round(rect.x + window.scrollX),
y: Math.round(rect.y + window.scrollY),
height: Math.round(rect.height),
width: Math.round(rect.width),
});
Expand Down