@percy/playwright 1.0.11-alpha.0 → 1.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +59 -131
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -20,6 +20,13 @@ async function processFrame(page, frame, options, percyDOM) {
20
20
  return PercyDOM.serialize(opts);
21
21
  }, { ...options, enableJavascript: true });
22
22
 
23
+ // Create a new resource for the iframe's HTML
24
+ const iframeResource = {
25
+ url: frameUrl,
26
+ content: iframeSnapshot.html,
27
+ mimetype: 'text/html'
28
+ };
29
+
23
30
  // Get the iframe's element data from the main page context
24
31
  /* istanbul ignore next: browser-executed evaluation function */
25
32
  const iframeData = await page.evaluate((fUrl) => {
@@ -34,141 +41,12 @@ async function processFrame(page, frame, options, percyDOM) {
34
41
 
35
42
  return {
36
43
  iframeData,
44
+ iframeResource,
37
45
  iframeSnapshot,
38
46
  frameUrl
39
47
  };
40
48
  }
41
49
 
42
- async function captureSerializedDOM(page, options, percyDOM) {
43
- /* istanbul ignore next: no instrumenting injected code */
44
- let domSnapshot = await page.evaluate((options) => {
45
- /* eslint-disable-next-line no-undef */
46
- return PercyDOM.serialize(options);
47
- }, options);
48
-
49
- // Process CORS IFrames
50
- // Note: Blob URL handling (data-src images, blob background images) is now handled
51
- // in the CLI via async DOM serialization. See: percy/cli packages/dom/src/serialize-blob-urls.js
52
- // This section only handles cross-origin iframe serialization and resource merging.
53
- const pageUrl = new URL(page.url());
54
- const crossOriginFrames = page.frames()
55
- .filter(frame => frame.url() !== 'about:blank' && new URL(frame.url()).origin !== pageUrl.origin);
56
-
57
- // Inject Percy DOM into all cross-origin frames before processing them in parallel
58
- await Promise.all(crossOriginFrames.map(frame => frame.evaluate(percyDOM)));
59
-
60
- const processedFrames = await Promise.all(
61
- crossOriginFrames.map(frame => processFrame(page, frame, options, percyDOM))
62
- );
63
- domSnapshot.corsIframes = processedFrames;
64
- domSnapshot.cookies = await page.context().cookies();
65
- return domSnapshot;
66
- }
67
-
68
- async function changeViewportAndWait(page, width, height, resizeCount) {
69
- try {
70
- await page.setViewportSize({ width, height });
71
- } catch (error) {
72
- log.debug(`Resizing using setViewportSize failed for width ${width}`, error);
73
- return;
74
- }
75
-
76
- try {
77
- /* istanbul ignore next: no instrumenting injected code */
78
- await page.waitForFunction((count) => window.resizeCount === count, resizeCount, { timeout: 1000 });
79
- } catch (error) {
80
- log.debug(`Timed out waiting for window resize event for width ${width}`, error);
81
- }
82
- }
83
-
84
- function isResponsiveDOMCaptureValid(options) {
85
- if (utils.percy?.config?.percy?.deferUploads) {
86
- return false;
87
- }
88
- return (
89
- options?.responsive_snapshot_capture ||
90
- options?.responsiveSnapshotCapture ||
91
- utils.percy?.config?.snapshot?.responsiveSnapshotCapture ||
92
- false
93
- );
94
- }
95
-
96
- async function captureResponsiveDOM(page, options, percyDOM) {
97
- const domSnapshots = [];
98
- /* istanbul ignore next: no instrumenting injected code */
99
- const currentViewport = page.viewportSize() || await page.evaluate(() => ({
100
- width: window.innerWidth,
101
- height: window.innerHeight
102
- }));
103
- let currentWidth = currentViewport.width;
104
- let currentHeight = currentViewport.height;
105
- let lastWindowWidth = currentWidth;
106
- let resizeCount = 0;
107
-
108
- /* istanbul ignore next: no instrumenting injected code */
109
- await page.evaluate(() => {
110
- /* eslint-disable-next-line no-undef */
111
- PercyDOM.waitForResize();
112
- });
113
-
114
- // Calculate default height for non-mobile widths
115
- let defaultHeight = currentHeight;
116
- if (process.env.PERCY_RESPONSIVE_CAPTURE_MIN_HEIGHT?.toLowerCase() === 'true') {
117
- const minHeight = utils.percy?.config?.snapshot?.minHeight;
118
- /* istanbul ignore next: no instrumenting injected code */
119
- defaultHeight = await page.evaluate((minH) => window.outerHeight - window.innerHeight + minH, minHeight);
120
- }
121
-
122
- // Get width and height combinations
123
- /* istanbul ignore next: CLI version compatibility check */
124
- if (!utils.getResponsiveWidths) {
125
- throw new Error('Update Percy CLI to the latest version to use responsiveSnapshotCapture');
126
- }
127
- const widthHeights = await utils.getResponsiveWidths(options.widths || []);
128
-
129
- try {
130
- for (let { width, height } of widthHeights) {
131
- height = height || defaultHeight;
132
- if (lastWindowWidth !== width) {
133
- resizeCount++;
134
- await changeViewportAndWait(page, width, height, resizeCount);
135
- lastWindowWidth = width;
136
- }
137
-
138
- if (process.env.PERCY_RESPONSIVE_CAPTURE_RELOAD_PAGE?.toLowerCase() === 'true') {
139
- await page.reload();
140
- await page.evaluate(percyDOM);
141
- /* istanbul ignore next: no instrumenting injected code */
142
- await page.evaluate(() => {
143
- /* eslint-disable-next-line no-undef */
144
- PercyDOM.waitForResize();
145
- });
146
- resizeCount = 0; // Reset local counter to match window.resizeCount after reload
147
- }
148
-
149
- if (process.env.RESPONSIVE_CAPTURE_SLEEP_TIME) {
150
- await new Promise(resolve => setTimeout(resolve, parseInt(process.env.RESPONSIVE_CAPTURE_SLEEP_TIME) * 1000));
151
- }
152
-
153
- let domSnapshot = await captureSerializedDOM(page, options, percyDOM);
154
- domSnapshot.width = width;
155
- domSnapshots.push(domSnapshot);
156
- }
157
- } finally {
158
- await changeViewportAndWait(page, currentWidth, currentHeight, resizeCount + 1);
159
- }
160
- return domSnapshots;
161
- }
162
-
163
- async function captureDOM(page, options, percyDOM) {
164
- const responsiveSnapshotCapture = isResponsiveDOMCaptureValid(options);
165
- if (responsiveSnapshotCapture) {
166
- return await captureResponsiveDOM(page, options, percyDOM);
167
- } else {
168
- return await captureSerializedDOM(page, options, percyDOM);
169
- }
170
- }
171
-
172
50
  // Take a DOM snapshot and post it to the snapshot endpoint
173
51
  const percySnapshot = async function(page, name, options) {
174
52
  if (!page) throw new Error('A Playwright `page` object is required.');
@@ -180,7 +58,57 @@ const percySnapshot = async function(page, name, options) {
180
58
  const percyDOM = await utils.fetchPercyDOM();
181
59
  await page.evaluate(percyDOM);
182
60
 
183
- let domSnapshot = await captureDOM(page, options || {}, percyDOM);
61
+ // Serialize and capture the DOM
62
+ /* istanbul ignore next: no instrumenting injected code */
63
+ let domSnapshot = await page.evaluate((options) => {
64
+ /* eslint-disable-next-line no-undef */
65
+ return PercyDOM.serialize(options);
66
+ }, options);
67
+
68
+ // Process CORS IFrames
69
+ // Note: Blob URL handling (data-src images, blob background images) is now handled
70
+ // in the CLI via async DOM serialization. See: percy/cli packages/dom/src/serialize-blob-urls.js
71
+ // This section only handles cross-origin iframe serialization and resource merging.
72
+ const pageUrl = new URL(page.url());
73
+ const crossOriginFrames = page.frames()
74
+ .filter(frame => {
75
+ const frameUrl = frame.url();
76
+ if (!frameUrl || frameUrl === 'about:blank') return false;
77
+ try {
78
+ return new URL(frameUrl).origin !== pageUrl.origin;
79
+ } catch {
80
+ return false;
81
+ }
82
+ });
83
+
84
+ // Inject Percy DOM into all cross-origin frames before processing them in parallel
85
+ await Promise.all(crossOriginFrames.map(frame => frame.evaluate(percyDOM)));
86
+
87
+ const processedFrames = await Promise.all(
88
+ crossOriginFrames.map(frame => processFrame(page, frame, options, percyDOM))
89
+ );
90
+
91
+ for (const { iframeData, iframeResource, iframeSnapshot, frameUrl } of processedFrames) {
92
+ // Add the iframe's own resources to the main snapshot
93
+ domSnapshot.resources.push(...iframeSnapshot.resources);
94
+ // Add the iframe HTML resource itself
95
+ domSnapshot.resources.push(iframeResource);
96
+
97
+ if (iframeData && iframeData.percyElementId) {
98
+ const regex = new RegExp(`(<iframe[^>]*data-percy-element-id=["']${iframeData.percyElementId}["'][^>]*>)`);
99
+ const match = domSnapshot.html.match(regex);
100
+
101
+ /* istanbul ignore next: iframe matching logic depends on DOM structure */
102
+ if (match) {
103
+ const iframeTag = match[1];
104
+ // Replace the original iframe tag with one that points to the new resource.
105
+ const newIframeTag = iframeTag.replace(/src="[^"]*"/i, `src="${frameUrl}"`);
106
+ domSnapshot.html = domSnapshot.html.replace(iframeTag, newIframeTag);
107
+ }
108
+ }
109
+ }
110
+
111
+ domSnapshot.cookies = await page.context().cookies();
184
112
 
185
113
  // Post the DOM to the snapshot endpoint with snapshot options and other info
186
114
  const response = await utils.postSnapshot({
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@percy/playwright",
3
3
  "description": "Playwright client library for visual testing with Percy",
4
- "version": "1.0.11-alpha.0",
4
+ "version": "1.1.0-beta.0",
5
5
  "license": "MIT",
6
6
  "author": "Perceptual Inc.",
7
7
  "repository": "https://github.com/percy/percy-playwright",
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "publishConfig": {
31
31
  "access": "public",
32
- "tag": "alpha"
32
+ "tag": "beta"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "playwright-core": ">=1"