flexysnap 0.2.0-alpha.1 → 0.2.1-alpha.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flexysnap",
3
- "version": "0.2.0-alpha.1",
3
+ "version": "0.2.1-alpha.1",
4
4
  "description": "Flexible visual snapshot testing for Playwright, built for e-commerce.",
5
5
  "keywords": [
6
6
  "playwright",
@@ -39,6 +39,59 @@ function delay(milliseconds) {
39
39
  return new Promise(resolve => setTimeout(resolve, milliseconds));
40
40
  }
41
41
 
42
+ async function getDeviceScaleFactor(page) {
43
+ try {
44
+ const scaleFactor = await page.evaluate(() => window.devicePixelRatio);
45
+ if (typeof scaleFactor === 'number' && isFinite(scaleFactor) && scaleFactor > 0) {
46
+ return scaleFactor;
47
+ }
48
+ return 1;
49
+ } catch {
50
+ return 1;
51
+ }
52
+ }
53
+
54
+ function scaleBoundingRect(boundingRect, scaleFactor) {
55
+ if (!boundingRect) {
56
+ return boundingRect;
57
+ }
58
+
59
+ return {
60
+ top: Math.round(boundingRect.top * scaleFactor),
61
+ left: Math.round(boundingRect.left * scaleFactor),
62
+ bottom: Math.round(boundingRect.bottom * scaleFactor),
63
+ right: Math.round(boundingRect.right * scaleFactor)
64
+ };
65
+ }
66
+
67
+ function scalePosition(position, scaleFactor) {
68
+ if (!position) {
69
+ return { x: 0, y: 0 };
70
+ }
71
+
72
+ return {
73
+ x: Math.round(position.x * scaleFactor),
74
+ y: Math.round(position.y * scaleFactor)
75
+ };
76
+ }
77
+
78
+ function scaleWireframeData(wireframeData, scaleFactor) {
79
+ if (scaleFactor === 1) {
80
+ return wireframeData;
81
+ }
82
+
83
+ for (const elementGroup of wireframeData) {
84
+ for (const element of elementGroup.elements || []) {
85
+ element.boundingRect = scaleBoundingRect(element.boundingRect, scaleFactor);
86
+ for (const text of element.texts || []) {
87
+ text.boundingRect = scaleBoundingRect(text.boundingRect, scaleFactor);
88
+ }
89
+ }
90
+ }
91
+
92
+ return wireframeData;
93
+ }
94
+
42
95
  async function extractWireframe(page, elementGroups) {
43
96
  const wireframeData = await page.evaluate(async ({elementGroups, elementIdAttribute}) => {
44
97
 
@@ -283,12 +336,23 @@ async function expectWireframe(page, elementGroups, outputDir, outputFile, outpu
283
336
  y: window.scrollY
284
337
  }));
285
338
 
339
+ const deviceScaleFactor = await getDeviceScaleFactor(page);
340
+
341
+ if (deviceScaleFactor !== 1) {
342
+ logTimestamp(`Scaling wireframe geometry by device scale factor: ${deviceScaleFactor}`);
343
+ }
344
+
345
+ const scaledWireframeData = scaleWireframeData(wireframeData, deviceScaleFactor);
346
+ const scaledScrollPosition = scalePosition(scrollPosition, deviceScaleFactor);
347
+ const scaledScreenshotScrollPosition = scalePosition(screenshotScrollPosition, deviceScaleFactor);
348
+
286
349
  const wireframeOutput = {
287
350
  name: outputName,
288
351
  timestamp: new Date().toISOString(),
289
- scrollPosition,
290
- screenshotScrollPosition,
291
- elementGroups: wireframeData,
352
+ scrollPosition: scaledScrollPosition,
353
+ screenshotScrollPosition: scaledScreenshotScrollPosition,
354
+ elementGroups: scaledWireframeData,
355
+ deviceScaleFactor,
292
356
  ...metadata
293
357
  };
294
358