From 9b8e2d9d133c3c284928c639c1e4d45633d86c03 Mon Sep 17 00:00:00 2001 From: Logan Graham Date: Thu, 10 Oct 2024 11:52:49 -0400 Subject: [PATCH 1/2] offset ignore regions based on clipSelector location --- visual-js/visual-playwright/src/api.ts | 103 +++++++++++++------------ 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/visual-js/visual-playwright/src/api.ts b/visual-js/visual-playwright/src/api.ts index d7ca719b..c9efba3f 100644 --- a/visual-js/visual-playwright/src/api.ts +++ b/visual-js/visual-playwright/src/api.ts @@ -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 => { - 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); @@ -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({ From 2481f2b639b71c6e6b8f06876405d29897a5c50f Mon Sep 17 00:00:00 2001 From: Logan Graham Date: Thu, 10 Oct 2024 11:57:47 -0400 Subject: [PATCH 2/2] docs(changeset): fix ignore region positioning when clipSelector is also present --- visual-js/.changeset/five-monkeys-live.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 visual-js/.changeset/five-monkeys-live.md diff --git a/visual-js/.changeset/five-monkeys-live.md b/visual-js/.changeset/five-monkeys-live.md new file mode 100644 index 00000000..e2c84f1b --- /dev/null +++ b/visual-js/.changeset/five-monkeys-live.md @@ -0,0 +1,6 @@ +--- +"@saucelabs/visual-playwright": patch +"@saucelabs/visual-storybook": patch +--- + +fix ignore region positioning when clipSelector is also present