flexysnap 0.2.1-alpha.1 → 0.2.3-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.1-alpha.1",
3
+ "version": "0.2.3-alpha.1",
4
4
  "description": "Flexible visual snapshot testing for Playwright, built for e-commerce.",
5
5
  "keywords": [
6
6
  "playwright",
package/src/index.js CHANGED
@@ -14,12 +14,7 @@ export {
14
14
  } from './testUtils.js';
15
15
 
16
16
  export {
17
- compareWireframes,
18
- compareElements,
19
- compareTexts,
20
- compareBoundingBoxes,
21
- createPairings,
22
- histogramDiff
17
+ compareWireframes
23
18
  } from './wireframeComparison.js';
24
19
 
25
20
  export { areWireframesStable } from './wireframeStability.js';
@@ -64,13 +64,12 @@ function createPairings(currentElements, baselineElements) {
64
64
  };
65
65
  }
66
66
 
67
- function compareBoundingBoxes(baselineRect, currentRect, tolerance = 10, strictPosition = true) {
67
+ function compareBoundingBoxes(baselineRect, currentRect, strictPosition = true, strictSize = true) {
68
68
  const differences = [];
69
69
 
70
70
  if (strictPosition) {
71
71
  const boundingBoxDifference = calculateBoundingBoxDistance(baselineRect, currentRect)
72
- //expect.soft(boundingBoxDifference).toBeLessThanOrEqual(tolerance)
73
- if (boundingBoxDifference > tolerance) {
72
+ if (boundingBoxDifference > 10) {
74
73
  differences.push({
75
74
  type: "layout_shift",
76
75
  difference: boundingBoxDifference
@@ -81,15 +80,26 @@ function compareBoundingBoxes(baselineRect, currentRect, tolerance = 10, strictP
81
80
  const baselineHeight = baselineRect.bottom - baselineRect.top;
82
81
  const currentWidth = currentRect.right - currentRect.left;
83
82
  const currentHeight = currentRect.bottom - currentRect.top;
84
- const sizeDiff = Math.max(
85
- Math.abs(baselineWidth - currentWidth),
86
- Math.abs(baselineHeight - currentHeight)
87
- );
88
- if (sizeDiff > tolerance) {
89
- differences.push({
90
- type: "size_mismatch",
91
- difference: sizeDiff
92
- });
83
+ if (strictSize) {
84
+ const sizeDiff = Math.max(
85
+ Math.abs(baselineWidth - currentWidth),
86
+ Math.abs(baselineHeight - currentHeight)
87
+ );
88
+ if (sizeDiff > 10) {
89
+ differences.push({
90
+ type: "size_mismatch",
91
+ difference: sizeDiff
92
+ });
93
+ }
94
+ } else {
95
+ const diffRatio = (baselineWidth * baselineHeight) / (currentWidth * currentHeight)
96
+ if (diffRatio > 1.1 || diffRatio < 0.9) {
97
+ differences.push({
98
+ type: "size_mismatch",
99
+ difference: diffRatio
100
+ });
101
+ }
102
+
93
103
  }
94
104
  }
95
105
 
@@ -100,7 +110,7 @@ function replaceDigitsWithPlaceholder(text) {
100
110
  return text.replace(/\d+/g, '[digits]');
101
111
  }
102
112
 
103
- function compareTexts(baselineTexts, currentTexts, strictPosition = true, maskDigits = false) {
113
+ function compareTexts(baselineTexts, currentTexts, strictPosition = true, strictSize = true, maskDigits = false) {
104
114
  const { matchedPairs, unmatchedCurrent, unmatchedBaseline } = createPairings(currentTexts, baselineTexts);
105
115
 
106
116
  for (const pairing of matchedPairs) {
@@ -127,8 +137,8 @@ function compareTexts(baselineTexts, currentTexts, strictPosition = true, maskDi
127
137
  const boundingBoxDifferences = compareBoundingBoxes(
128
138
  baselineText.boundingRect,
129
139
  currentText.boundingRect,
130
- 10,
131
- strictPosition
140
+ strictPosition,
141
+ strictSize
132
142
  );
133
143
  currentText.differences = currentText.differences.concat(boundingBoxDifferences);
134
144
  }
@@ -156,12 +166,12 @@ function compareTexts(baselineTexts, currentTexts, strictPosition = true, maskDi
156
166
  }
157
167
  }
158
168
 
159
- function compareElements(baselineElement, currentElement, strictPosition = true, maskDigits = false) {
169
+ function compareElements(baselineElement, currentElement, strictPosition = true, strictSize = true, maskDigits = false) {
160
170
  currentElement.differences = compareBoundingBoxes(
161
171
  baselineElement.boundingRect,
162
172
  currentElement.boundingRect,
163
- 10,
164
- strictPosition
173
+ strictPosition,
174
+ strictSize
165
175
  );
166
176
 
167
177
  if (baselineElement.histogram && currentElement.histogram) {
@@ -175,7 +185,7 @@ function compareElements(baselineElement, currentElement, strictPosition = true,
175
185
  }
176
186
 
177
187
  if (baselineElement.texts.length > 0 || currentElement.texts.length > 0) {
178
- compareTexts(baselineElement.texts, currentElement.texts, strictPosition, maskDigits);
188
+ compareTexts(baselineElement.texts, currentElement.texts, strictPosition, strictSize, maskDigits);
179
189
  }
180
190
  }
181
191
 
@@ -193,6 +203,7 @@ function compareWireframes(baselineWireframe, currentWireframe) {
193
203
  expect(baselineElementGroup.selector).toEqual(currentElementGroup.selector);
194
204
 
195
205
  const strictPosition = currentElementGroup.strictPosition !== false;
206
+ const strictSize = currentElementGroup.strictSize !== false;
196
207
  const maskDigits = currentElementGroup.maskDigits === true;
197
208
 
198
209
  //TODO: REMOVE later
@@ -208,7 +219,7 @@ function compareWireframes(baselineWireframe, currentWireframe) {
208
219
  for (const pairing of matchedPairs) {
209
220
  const baselineElement = baselineElementGroup.elements[pairing.baselineIndex];
210
221
  const currentElement = currentElementGroup.elements[pairing.currentIndex];
211
- compareElements(baselineElement, currentElement, strictPosition, maskDigits);
222
+ compareElements(baselineElement, currentElement, strictPosition, strictSize, maskDigits);
212
223
  if (currentElement.differences?.length === 0) {
213
224
  currentElement.differences = undefined
214
225
  }
@@ -233,4 +244,4 @@ function compareWireframes(baselineWireframe, currentWireframe) {
233
244
  return currentWireframe;
234
245
  }
235
246
 
236
- export { compareWireframes, compareElements, compareTexts, compareBoundingBoxes, createPairings, histogramDiff };
247
+ export { compareWireframes };
@@ -139,7 +139,8 @@ async function extractWireframe(page, elementGroups) {
139
139
  for (let groupIndex = 0; groupIndex < elementGroups.length; groupIndex++) {
140
140
  const elementGroup = elementGroups[groupIndex];
141
141
  elementGroup.strictPosition = elementGroup.strictPosition !== false;
142
- elementGroup.maskDigits = elementGroup.maskDigits === false;
142
+ elementGroup.strictSize = elementGroup.strictSize !== false;
143
+ elementGroup.maskDigits = elementGroup.maskDigits !== false;
143
144
  elementGroup.elements = [];
144
145
 
145
146
  let index = 0;