Feat : Adding Cross Iframe Feature#720
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enhances the Selenium WebDriver Percy integration by adding cross-origin iframe DOM capture + stitching into the main DOM snapshot, and by updating responsive snapshot capture to use utils.getResponsiveWidths() (supporting per-width heights). It also adds unit tests around these behaviors and updates Percy-related dependencies.
Changes:
- Added cross-origin iframe processing (
captureSerializedDOM+processFrame) and stitching via exportedstitchCorsIframes(). - Updated responsive DOM capture to use
utils.getResponsiveWidths()and support{ width, height }tuples. - Updated
@percy/sdk-utilsand@percy/cliversions and added extensive new tests.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
index.js |
Adds cross-origin iframe DOM serialization/stitching; switches responsive widths logic to utils.getResponsiveWidths(); adds logging when responsive capture is disabled due to deferUploads. |
test/index.test.mjs |
Adds tests for cross-origin iframe processing, iframe src filtering, and responsive width/height handling. |
package.json |
Updates Percy dependency versions to 1.31.10-alpha.0. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
test/index.test.mjs
Outdated
| describe('stitchCorsIframes', () => { | ||
| let switchSpies; | ||
| let frameElement; | ||
| let iframeDriver; |
test/index.test.mjs
Outdated
| let callCount = 0; | ||
| return { | ||
| getCurrentUrl: jasmine.createSpy().and.returnValue(Promise.resolve('https://host.example.com/')), | ||
| findElements: jasmine.createSpy().and.returnValue(Promise.resolve([frameElement])), | ||
| switchTo: jasmine.createSpy().and.returnValue(switchSpies), | ||
| executeScript: jasmine.createSpy('executeScript').and.callFake(async () => { | ||
| callCount++; | ||
| if (callCount === 1) return { domSnapshot: { html: mainHtml, resources: [] } }; | ||
| return iframeHtml ? { html: iframeHtml, resources: [] } : { resources: [] }; |
| }); | ||
|
|
||
| describe('captureSerializedDOM - iframe src filtering', () => { | ||
| let baseDriver; |
| const currentUrl = new URL(await driver.getCurrentUrl()); | ||
| const iframes = await driver.findElements({ css: 'iframe' }); | ||
| const processedFrames = []; |
index.js
Outdated
| function stitchCorsIframes(domSnapshot, processedFrames) { | ||
| let html = domSnapshot.html; | ||
| for (const { iframeData: { percyElementId }, iframeSnapshot } of processedFrames) { | ||
| if (!iframeSnapshot?.html) continue; | ||
| const srcdocValue = iframeSnapshot.html | ||
| .replace(/&/g, '&') | ||
| .replace(/"/g, '"'); | ||
| html = html.replace( | ||
| new RegExp(`(<iframe\\b[^>]*?data-percy-element-id="${percyElementId}"[^>]*?)(/?>)`, 's'), | ||
| `$1 srcdoc="${srcdocValue}">` | ||
| ); |
There was a problem hiding this comment.
Pull request overview
This PR enhances Percy’s Selenium WebDriver DOM capture to better include cross-origin iframe content in serialized DOM snapshots, while also improving responsive snapshot capture width/height handling and adding unit tests.
Changes:
- Added cross-origin iframe serialization flow (
captureSerializedDOM+processFrame) and HTML stitching via exportedstitchCorsIframes(). - Updated responsive DOM capture to use
utils.getResponsiveWidths()with per-width height support. - Added extensive Jasmine tests around iframe stitching/switching and responsive capture behavior; updated Percy dependencies to alpha versions.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
index.js |
Implements cross-origin iframe capture + stitching, responsive width/height updates, and new logging/export. |
test/index.test.mjs |
Adds integration/unit tests for iframe processing/stitching and responsive capture resizing behavior. |
package.json |
Updates @percy/sdk-utils and @percy/cli versions to 1.31.10-alpha.0. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
index.js
Outdated
| for (const { iframeData: { percyElementId }, iframeSnapshot } of processedFrames) { | ||
| if (!iframeSnapshot?.html) continue; | ||
| const srcdocValue = iframeSnapshot.html | ||
| .replace(/&/g, '&') | ||
| .replace(/"/g, '"'); | ||
| const escapedId = escapeRegExp(percyElementId); | ||
| const iframeRegex = new RegExp( | ||
| `(<iframe\\b[^>]*?data-percy-element-id="${escapedId}"[^>]*?)(/?>)`, | ||
| 's' | ||
| ); | ||
| html = html.replace( | ||
| iframeRegex, | ||
| (match, iframeStart, iframeEnd) => `${iframeStart} srcdoc="${srcdocValue}">` | ||
| ); | ||
| } | ||
| return { ...domSnapshot, html }; |
| "name": "@percy/selenium-webdriver", | ||
| "description": "Selenium client library for visual testing with Percy", | ||
| "version": "2.2.5", | ||
| "version": "2.2.6-alpha.0", |
This pull request introduces significant improvements to cross-origin iframe handling and responsive DOM capture in the Selenium WebDriver integration, along with some code cleanup and dependency updates. The main focus is on enabling the serialization and stitching of cross-origin iframes into the main DOM snapshot, ensuring more accurate visual testing of pages with embedded content from different origins.
Cross-origin iframe handling:
captureSerializedDOMfunction that serializes the main DOM and then processes cross-origin iframes by capturing their DOM snapshots and injecting them back into the main snapshot using a newstitchCorsIframesfunction. This ensures that cross-origin iframe content is included in Percy snapshots by setting thesrcdocattribute on the relevant iframe elements.processFrameutility to handle the switching of frame contexts, execute serialization scripts, and robustly handle errors and context switching for cross-origin iframes.Responsive DOM capture improvements:
getWidthsForMultiDOMfunction withutils.getResponsiveWidths, and updated the responsive DOM capture logic to support per-width height values, improving flexibility and accuracy for responsive snapshots. [1] [2] [3]Dependency updates:
@percy/sdk-utilsand@percy/cliinpackage.jsonfor compatibility with new features and bug fixes.Other improvements:
deferUploadsbeing true, improving debuggability.