Skip to content
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

[Playwright] Fix ignore region positioning when clipSelector is also present #150

Merged
merged 2 commits into from
Oct 10, 2024
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
6 changes: 6 additions & 0 deletions visual-js/.changeset/five-monkeys-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@saucelabs/visual-playwright": patch
"@saucelabs/visual-storybook": patch
---

fix ignore region positioning when clipSelector is also present
103 changes: 55 additions & 48 deletions visual-js/visual-playwright/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,54 +169,6 @@ ${e instanceof Error ? e.message : JSON.stringify(e)}
promises.push(new Promise((resolve) => setTimeout(resolve, delay)));
}

if (userIgnoreRegions) {
promises.push(
(async (): Promise<void> => {
const filterIgnoreRegion = (
region: RegionIn | string,
): region is RegionIn => typeof region !== 'string';
const filterIgnoreSelector = (
region: RegionIn | string,
): region is string => typeof region === 'string';

const selectors = userIgnoreRegions.filter(filterIgnoreSelector);
let selectorRegions: RegionIn[] = [];

if (selectors.length) {
selectorRegions = await page.evaluate(
({ selectors }) => {
const selectorRegions: RegionIn[] = [];
selectors.forEach((selector) => {
const elements = document.querySelectorAll(selector);
elements.forEach((element) => {
const rect = element.getBoundingClientRect();

selectorRegions.push({
name: selector,
x: Math.round(rect.x),
y: Math.round(rect.y),
height: Math.round(rect.height),
width: Math.round(rect.width),
});
});
});
return selectorRegions;
},
{ selectors },
);
}

ignoreRegions = [
...userIgnoreRegions.filter(filterIgnoreRegion),
...selectorRegions,
].filter(
(region) =>
0 < Math.max(region.width, 0) * Math.max(region.height, 0),
);
})(),
);
}

// Await all queued / concurrent promises before resuming
await Promise.all(promises);

Expand Down Expand Up @@ -260,6 +212,61 @@ ${e instanceof Error ? e.message : JSON.stringify(e)}
)
: undefined;

if (userIgnoreRegions) {
const filterIgnoreRegion = (
region: RegionIn | string,
): region is RegionIn => typeof region !== 'string';
const filterIgnoreSelector = (
region: RegionIn | string,
): region is string => typeof region === 'string';

const selectors = userIgnoreRegions.filter(filterIgnoreSelector);
let selectorRegions: RegionIn[] = [];

if (selectors.length) {
selectorRegions = await page.evaluate(
({ selectors }) => {
const selectorRegions: RegionIn[] = [];
selectors.forEach((selector) => {
const elements = document.querySelectorAll(selector);
elements.forEach((element) => {
const rect = element.getBoundingClientRect();

selectorRegions.push({
name: selector,
x: Math.round(rect.x),
y: Math.round(rect.y),
height: Math.round(rect.height),
width: Math.round(rect.width),
});
});
});
return selectorRegions;
},
{ selectors },
);
}

ignoreRegions = [
...userIgnoreRegions.filter(filterIgnoreRegion),
...selectorRegions,
]
.map((region) =>
clip
? {
// Offset ignore regions using the location of the element we clipped to
...region,
x: Math.round(region.x - clip.x),
y: Math.round(region.y - clip.y),
}
: region,
)
.filter(
(region) =>
0 < Math.max(region.width, 0) * Math.max(region.height, 0),
);
}

const devicePixelRatio = await page.evaluate(() => window.devicePixelRatio);

const screenshotBuffer = await page.screenshot({
Expand Down
Loading