@cappa/core 0.1.2 → 0.2.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.
- package/.turbo/turbo-build.log +13 -19
- package/.turbo/turbo-test.log +53 -0
- package/CHANGELOG.md +21 -0
- package/LICENSE +21 -0
- package/README.md +20 -0
- package/dist/index.d.mts +190 -38
- package/dist/index.d.ts +190 -38
- package/dist/index.js +430 -88
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +430 -89
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -12
- package/src/compare.test.ts +9 -4
- package/src/compare.ts +76 -23
- package/src/filesystem.test.ts +83 -0
- package/src/filesystem.ts +202 -0
- package/src/index.ts +23 -2
- package/src/plugin.ts +26 -3
- package/src/screenshot.test.ts +40 -0
- package/src/screenshot.ts +425 -119
- package/src/types.ts +23 -1
package/dist/index.js
CHANGED
|
@@ -42,6 +42,167 @@ function defineConfig(options) {
|
|
|
42
42
|
return options;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/filesystem.ts
|
|
47
|
+
/**
|
|
48
|
+
* Class for interacting with the local file system to store the screenshots.
|
|
49
|
+
*/
|
|
50
|
+
var ScreenshotFileSystem = class {
|
|
51
|
+
actualDir;
|
|
52
|
+
expectedDir;
|
|
53
|
+
diffDir;
|
|
54
|
+
constructor(outputDir) {
|
|
55
|
+
this.actualDir = node_path.default.resolve(outputDir, "actual");
|
|
56
|
+
this.expectedDir = node_path.default.resolve(outputDir, "expected");
|
|
57
|
+
this.diffDir = node_path.default.resolve(outputDir, "diff");
|
|
58
|
+
this.ensureParentDir(this.actualDir);
|
|
59
|
+
this.ensureParentDir(this.expectedDir);
|
|
60
|
+
this.ensureParentDir(this.diffDir);
|
|
61
|
+
}
|
|
62
|
+
clearActual() {
|
|
63
|
+
this.removeDir(this.actualDir);
|
|
64
|
+
}
|
|
65
|
+
clearDiff() {
|
|
66
|
+
this.removeDir(this.diffDir);
|
|
67
|
+
}
|
|
68
|
+
clearActualAndDiff() {
|
|
69
|
+
this.clearActual();
|
|
70
|
+
this.clearDiff();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Approve a screenshot based on its actual file path.
|
|
74
|
+
* @param actualFilePath - The path to the actual screenshot file.
|
|
75
|
+
* @returns The paths to the actual, expected, and diff screenshot files.
|
|
76
|
+
*/
|
|
77
|
+
approveFromActualPath(actualFilePath) {
|
|
78
|
+
const actualAbsolute = this.resolveActualPath(actualFilePath);
|
|
79
|
+
const relativePath = node_path.default.relative(this.actualDir, actualAbsolute);
|
|
80
|
+
if (relativePath.startsWith("..")) throw new Error(`Cannot approve screenshot outside of actual directory: ${actualFilePath}`);
|
|
81
|
+
return this.approveRelative(relativePath);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Approve a screenshot based on its name.
|
|
85
|
+
* @param name - The name of the screenshot. Can include '.png' extension.
|
|
86
|
+
* @returns The paths to the actual, expected, and diff screenshot files.
|
|
87
|
+
*/
|
|
88
|
+
approveByName(name) {
|
|
89
|
+
const relativeWithExtension = name.endsWith(".png") ? name : `${name}.png`;
|
|
90
|
+
return this.approveRelative(relativeWithExtension);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Approve a screenshot based on a relative path.
|
|
94
|
+
* @param relativePath - The relative path to the screenshot.
|
|
95
|
+
* @returns The paths to the actual, expected, and diff screenshot files.
|
|
96
|
+
*/
|
|
97
|
+
approveRelative(relativePath) {
|
|
98
|
+
const actualPath = node_path.default.resolve(this.actualDir, relativePath);
|
|
99
|
+
const expectedPath = node_path.default.resolve(this.expectedDir, relativePath);
|
|
100
|
+
const diffPath = node_path.default.resolve(this.diffDir, relativePath);
|
|
101
|
+
this.ensureParentDir(expectedPath);
|
|
102
|
+
node_fs.default.copyFileSync(actualPath, expectedPath);
|
|
103
|
+
if (node_fs.default.existsSync(diffPath)) node_fs.default.unlinkSync(diffPath);
|
|
104
|
+
return {
|
|
105
|
+
actualPath,
|
|
106
|
+
expectedPath,
|
|
107
|
+
diffPath
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Ensure the parent directory of a file exists. If not, it creates it.
|
|
112
|
+
* @param filePath - The path to the file.
|
|
113
|
+
*/
|
|
114
|
+
ensureParentDir(filePath) {
|
|
115
|
+
const dir = node_path.default.dirname(filePath);
|
|
116
|
+
if (!node_fs.default.existsSync(dir)) node_fs.default.mkdirSync(dir, { recursive: true });
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Remove a directory and all its contents.
|
|
120
|
+
* @param dir - The path to the directory.
|
|
121
|
+
*/
|
|
122
|
+
removeDir(dir) {
|
|
123
|
+
if (node_fs.default.existsSync(dir)) node_fs.default.rmSync(dir, {
|
|
124
|
+
recursive: true,
|
|
125
|
+
force: true
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get the actual directory path
|
|
130
|
+
*/
|
|
131
|
+
getActualDir() {
|
|
132
|
+
return this.actualDir;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get the expected directory path
|
|
136
|
+
*/
|
|
137
|
+
getExpectedDir() {
|
|
138
|
+
return this.expectedDir;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Get the diff directory path
|
|
142
|
+
*/
|
|
143
|
+
getDiffDir() {
|
|
144
|
+
return this.diffDir;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get the path to an actual screenshot file
|
|
148
|
+
*/
|
|
149
|
+
getActualFilePath(filename) {
|
|
150
|
+
return node_path.default.resolve(this.actualDir, filename);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Get the path to an expected screenshot file
|
|
154
|
+
*/
|
|
155
|
+
getExpectedFilePath(filename) {
|
|
156
|
+
return node_path.default.resolve(this.expectedDir, filename);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Get the path to a diff screenshot file
|
|
160
|
+
*/
|
|
161
|
+
getDiffFilePath(filename) {
|
|
162
|
+
return node_path.default.resolve(this.diffDir, filename);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Write a file to the actual directory
|
|
166
|
+
*/
|
|
167
|
+
writeActualFile(filename, data) {
|
|
168
|
+
const filepath = this.getActualFilePath(filename);
|
|
169
|
+
this.ensureParentDir(filepath);
|
|
170
|
+
node_fs.default.writeFileSync(filepath, data);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Write a file to the diff directory
|
|
174
|
+
*/
|
|
175
|
+
writeDiffFile(filename, data) {
|
|
176
|
+
const filepath = this.getDiffFilePath(filename);
|
|
177
|
+
this.ensureParentDir(filepath);
|
|
178
|
+
node_fs.default.writeFileSync(filepath, data);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Check if an expected file exists
|
|
182
|
+
*/
|
|
183
|
+
hasExpectedFile(filename) {
|
|
184
|
+
const expectedPath = this.getExpectedFilePath(filename);
|
|
185
|
+
return node_fs.default.existsSync(expectedPath);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Read an expected file
|
|
189
|
+
*/
|
|
190
|
+
readExpectedFile(filename) {
|
|
191
|
+
const expectedPath = this.getExpectedFilePath(filename);
|
|
192
|
+
if (!node_fs.default.existsSync(expectedPath)) throw new Error(`Expected image not found: ${expectedPath}`);
|
|
193
|
+
return node_fs.default.readFileSync(expectedPath);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Resolve the actual path of a screenshot file.
|
|
197
|
+
* @param filePath - The path to the screenshot file.
|
|
198
|
+
* @returns The actual path of the screenshot file.
|
|
199
|
+
*/
|
|
200
|
+
resolveActualPath(filePath) {
|
|
201
|
+
if (node_path.default.isAbsolute(filePath)) return filePath;
|
|
202
|
+
return node_path.default.resolve(this.actualDir, filePath);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
45
206
|
//#endregion
|
|
46
207
|
//#region src/compare.ts
|
|
47
208
|
const compare = (image1, image2, diff, width, height, options) => {
|
|
@@ -59,21 +220,40 @@ async function compareImages(image1, image2, withDiff = false, options = {}) {
|
|
|
59
220
|
const png1 = await loadPNG(image1);
|
|
60
221
|
const png2 = await loadPNG(image2);
|
|
61
222
|
const { width, height } = png1;
|
|
223
|
+
if (width !== png2.width || height !== png2.height) return {
|
|
224
|
+
numDiffPixels: 0,
|
|
225
|
+
totalPixels: 0,
|
|
226
|
+
percentDifference: 0,
|
|
227
|
+
passed: false,
|
|
228
|
+
differentSizes: true
|
|
229
|
+
};
|
|
62
230
|
const diff = withDiff ? new pngjs.PNG({
|
|
63
231
|
width,
|
|
64
232
|
height
|
|
65
233
|
}) : void 0;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
234
|
+
try {
|
|
235
|
+
const numDiffPixels = compare(png1.data, png2.data, diff?.data, width, height, options);
|
|
236
|
+
const totalPixels = width * height;
|
|
237
|
+
const percentDifference = numDiffPixels / totalPixels * 100;
|
|
238
|
+
return {
|
|
239
|
+
numDiffPixels,
|
|
240
|
+
totalPixels,
|
|
241
|
+
percentDifference,
|
|
242
|
+
diffBuffer: diff ? pngjs.PNG.sync.write(diff) : void 0,
|
|
243
|
+
passed: isPassed(percentDifference, numDiffPixels, options),
|
|
244
|
+
differentSizes: false
|
|
245
|
+
};
|
|
246
|
+
} catch (error) {
|
|
247
|
+
(0, __cappa_logger.getLogger)().error(error.message || "Unknown error");
|
|
248
|
+
return {
|
|
249
|
+
numDiffPixels: 0,
|
|
250
|
+
totalPixels: 0,
|
|
251
|
+
percentDifference: 0,
|
|
252
|
+
passed: false,
|
|
253
|
+
differentSizes: false,
|
|
254
|
+
error: error.message || "Unknown error"
|
|
255
|
+
};
|
|
256
|
+
}
|
|
77
257
|
}
|
|
78
258
|
/**
|
|
79
259
|
* Load a PNG image from file path or Buffer
|
|
@@ -85,12 +265,37 @@ async function loadPNG(source) {
|
|
|
85
265
|
return pngjs.PNG.sync.read(buffer);
|
|
86
266
|
}
|
|
87
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* Check if the comparison passed based on the options
|
|
270
|
+
* @param percentDifference - Percentage difference between the images
|
|
271
|
+
* @param numDiffPixels - Number of different pixels
|
|
272
|
+
* @param options - Comparison options
|
|
273
|
+
* @returns Whether the comparison passed
|
|
274
|
+
*/
|
|
88
275
|
const isPassed = (percentDifference, numDiffPixels, options) => {
|
|
89
276
|
if (options.maxDiffPercentage && options.maxDiffPixels) return percentDifference <= options.maxDiffPercentage && numDiffPixels <= options.maxDiffPixels;
|
|
90
277
|
if (options.maxDiffPercentage) return percentDifference <= options.maxDiffPercentage;
|
|
91
278
|
if (options.maxDiffPixels) return numDiffPixels <= options.maxDiffPixels;
|
|
92
279
|
return numDiffPixels === 0;
|
|
93
280
|
};
|
|
281
|
+
/**
|
|
282
|
+
* Create different size png image with all pixels red
|
|
283
|
+
* @returns
|
|
284
|
+
*/
|
|
285
|
+
function createDiffSizePngImage(width, height) {
|
|
286
|
+
const png = new pngjs.PNG({
|
|
287
|
+
width,
|
|
288
|
+
height
|
|
289
|
+
});
|
|
290
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
|
291
|
+
const idx = width * y + x << 2;
|
|
292
|
+
png.data[idx] = 255;
|
|
293
|
+
png.data[idx + 1] = 0;
|
|
294
|
+
png.data[idx + 2] = 0;
|
|
295
|
+
png.data[idx + 3] = 255;
|
|
296
|
+
}
|
|
297
|
+
return pngjs.PNG.sync.write(png);
|
|
298
|
+
}
|
|
94
299
|
|
|
95
300
|
//#endregion
|
|
96
301
|
//#region src/screenshot.ts
|
|
@@ -106,14 +311,16 @@ var ScreenshotTool = class {
|
|
|
106
311
|
headless;
|
|
107
312
|
viewport;
|
|
108
313
|
outputDir;
|
|
109
|
-
actualDir;
|
|
110
|
-
diffDir;
|
|
111
|
-
expectedDir;
|
|
112
314
|
browser = null;
|
|
113
315
|
context = null;
|
|
114
316
|
page = null;
|
|
317
|
+
concurrency;
|
|
318
|
+
contexts = [];
|
|
319
|
+
pages = [];
|
|
115
320
|
diff;
|
|
116
321
|
logger;
|
|
322
|
+
retries;
|
|
323
|
+
filesystem;
|
|
117
324
|
constructor(options) {
|
|
118
325
|
this.browserType = options.browserType || "chromium";
|
|
119
326
|
this.headless = options.headless !== false;
|
|
@@ -126,16 +333,15 @@ var ScreenshotTool = class {
|
|
|
126
333
|
...defaultDiffConfig,
|
|
127
334
|
...options.diff
|
|
128
335
|
};
|
|
129
|
-
this.
|
|
130
|
-
this.diffDir = node_path.default.join(this.outputDir, "diff");
|
|
131
|
-
this.expectedDir = node_path.default.join(this.outputDir, "expected");
|
|
336
|
+
this.concurrency = Math.max(1, options.concurrency || 1);
|
|
132
337
|
this.logger = (0, __cappa_logger.getLogger)();
|
|
338
|
+
this.retries = options.retries || 2;
|
|
339
|
+
this.filesystem = new ScreenshotFileSystem(this.outputDir);
|
|
133
340
|
}
|
|
134
341
|
/**
|
|
135
342
|
* Initialize playwright and page
|
|
136
343
|
*/
|
|
137
344
|
async init() {
|
|
138
|
-
this.createDirectories();
|
|
139
345
|
const browserClass = {
|
|
140
346
|
chromium: playwright_core.chromium,
|
|
141
347
|
firefox: playwright_core.firefox,
|
|
@@ -144,20 +350,28 @@ var ScreenshotTool = class {
|
|
|
144
350
|
if (!browserClass) throw new Error(`Unsupported browser type: ${this.browserType}`);
|
|
145
351
|
this.browser = await browserClass.launch({ headless: this.headless });
|
|
146
352
|
const defaultUserAgent = (await this.browser.newContext()).newPage().then((page) => page.evaluate(() => navigator.userAgent));
|
|
147
|
-
this.
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
353
|
+
this.contexts = [];
|
|
354
|
+
this.pages = [];
|
|
355
|
+
for (let i = 0; i < this.concurrency; i++) {
|
|
356
|
+
const context = await this.browser.newContext({
|
|
357
|
+
reducedMotion: "reduce",
|
|
358
|
+
deviceScaleFactor: 2,
|
|
359
|
+
userAgent: `${await defaultUserAgent} CappaStorybook`,
|
|
360
|
+
viewport: this.viewport
|
|
361
|
+
});
|
|
362
|
+
const page = await context.newPage();
|
|
363
|
+
this.contexts.push(context);
|
|
364
|
+
this.pages.push(page);
|
|
365
|
+
}
|
|
366
|
+
this.context = this.contexts[0] ?? null;
|
|
367
|
+
this.page = this.pages[0] ?? null;
|
|
154
368
|
}
|
|
155
369
|
/**
|
|
156
370
|
* Closes browser
|
|
157
371
|
* If not closed, the process will not exit
|
|
158
372
|
*/
|
|
159
373
|
async close() {
|
|
160
|
-
|
|
374
|
+
for (const context of this.contexts) await context.close();
|
|
161
375
|
if (this.browser) await this.browser.close();
|
|
162
376
|
}
|
|
163
377
|
/**
|
|
@@ -168,46 +382,13 @@ var ScreenshotTool = class {
|
|
|
168
382
|
await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
169
383
|
}
|
|
170
384
|
/**
|
|
171
|
-
* Creates directories for screenshots and other files
|
|
172
|
-
*/
|
|
173
|
-
createDirectories() {
|
|
174
|
-
const directories = [
|
|
175
|
-
this.outputDir,
|
|
176
|
-
this.actualDir,
|
|
177
|
-
this.diffDir,
|
|
178
|
-
this.expectedDir
|
|
179
|
-
];
|
|
180
|
-
for (const dir of directories) if (!node_fs.default.existsSync(dir)) {
|
|
181
|
-
node_fs.default.mkdirSync(dir, { recursive: true });
|
|
182
|
-
this.logger.debug(`Initialized directory: ${dir}`);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Gets the path to the actual screenshot file
|
|
187
|
-
*/
|
|
188
|
-
getActualFilePath(filename) {
|
|
189
|
-
return node_path.default.join(this.actualDir, filename);
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Gets the path to the diff screenshot file
|
|
193
|
-
*/
|
|
194
|
-
getDiffFilePath(filename) {
|
|
195
|
-
return node_path.default.join(this.diffDir, filename);
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Gets the path to the expected screenshot file
|
|
199
|
-
*/
|
|
200
|
-
getExpectedFilePath(filename) {
|
|
201
|
-
return node_path.default.join(this.expectedDir, filename);
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
385
|
* Takes a screenshot of the page
|
|
205
386
|
*/
|
|
206
387
|
async takeScreenshot(page, filename, options) {
|
|
207
388
|
if (!this.browser) throw new Error("Browser not initialized");
|
|
208
389
|
if (options.skip) return;
|
|
209
390
|
try {
|
|
210
|
-
const filepath = this.getActualFilePath(filename);
|
|
391
|
+
const filepath = this.filesystem.getActualFilePath(filename);
|
|
211
392
|
const screenshotOptions = {
|
|
212
393
|
path: filepath,
|
|
213
394
|
fullPage: options.fullPage,
|
|
@@ -215,51 +396,96 @@ var ScreenshotTool = class {
|
|
|
215
396
|
timeout: 6e4,
|
|
216
397
|
mask: options.mask,
|
|
217
398
|
omitBackground: options.omitBackground,
|
|
218
|
-
scale: "css"
|
|
399
|
+
scale: "css",
|
|
400
|
+
animations: "disabled",
|
|
401
|
+
caret: "hide"
|
|
219
402
|
};
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
403
|
+
await this.withViewport(page, options.viewport, async () => {
|
|
404
|
+
if (options.delay) await page.waitForTimeout(options.delay);
|
|
405
|
+
await page.screenshot(screenshotOptions);
|
|
406
|
+
});
|
|
223
407
|
return filepath;
|
|
224
408
|
} catch (error) {
|
|
225
409
|
this.logger.error(`Error taking screenshot of ${page.url()}:`, error.message);
|
|
226
410
|
throw error;
|
|
227
411
|
}
|
|
228
412
|
}
|
|
229
|
-
|
|
413
|
+
/**
|
|
414
|
+
* Sets the viewport size and executes the action, ensuring the original viewport is restored
|
|
415
|
+
* even if the action throws an error.
|
|
416
|
+
*/
|
|
417
|
+
async withViewport(page, viewport, action) {
|
|
418
|
+
if (!viewport) return action();
|
|
419
|
+
const previousViewport = page.viewportSize();
|
|
420
|
+
if (previousViewport && previousViewport.width === viewport.width && previousViewport.height === viewport.height) return action();
|
|
421
|
+
await page.setViewportSize(viewport);
|
|
422
|
+
try {
|
|
423
|
+
return await action();
|
|
424
|
+
} finally {
|
|
425
|
+
if (previousViewport) await page.setViewportSize(previousViewport);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Takes a screenshot and returns the buffer (for comparison purposes)
|
|
430
|
+
*/
|
|
431
|
+
async takeScreenshotBuffer(page, options) {
|
|
230
432
|
if (!this.browser) throw new Error("Browser not initialized");
|
|
433
|
+
if (options.skip) throw new Error("Screenshot skipped");
|
|
231
434
|
try {
|
|
232
|
-
const filepath = this.getActualFilePath(filename);
|
|
233
|
-
node_fs.default.mkdirSync(node_path.default.dirname(filepath), { recursive: true });
|
|
234
435
|
const screenshotOptions = {
|
|
235
436
|
fullPage: options.fullPage,
|
|
236
437
|
type: "png",
|
|
237
438
|
timeout: 6e4,
|
|
238
439
|
mask: options.mask,
|
|
239
440
|
omitBackground: options.omitBackground,
|
|
240
|
-
scale: "css"
|
|
441
|
+
scale: "css",
|
|
442
|
+
animations: "disabled",
|
|
443
|
+
caret: "hide"
|
|
241
444
|
};
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
const comparisonResult = await compareImages(screenshotBuffer, referenceImage, true, {
|
|
246
|
-
...this.diff,
|
|
247
|
-
...options.compareOptions
|
|
445
|
+
return await this.withViewport(page, options.viewport, async () => {
|
|
446
|
+
if (options.delay) await page.waitForTimeout(options.delay);
|
|
447
|
+
return page.screenshot(screenshotOptions);
|
|
248
448
|
});
|
|
249
|
-
|
|
250
|
-
|
|
449
|
+
} catch (error) {
|
|
450
|
+
this.logger.error(`Error taking screenshot of ${page.url()}:`, error.message);
|
|
451
|
+
throw error;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
async takeScreenshotWithComparison(page, filename, referenceImage, options) {
|
|
455
|
+
if (!this.browser) throw new Error("Browser not initialized");
|
|
456
|
+
try {
|
|
457
|
+
const filepath = this.filesystem.getActualFilePath(filename);
|
|
458
|
+
const screenshotSettings = {
|
|
459
|
+
fullPage: options.fullPage,
|
|
460
|
+
mask: options.mask,
|
|
461
|
+
omitBackground: options.omitBackground,
|
|
462
|
+
delay: options.delay,
|
|
463
|
+
viewport: options.viewport
|
|
464
|
+
};
|
|
465
|
+
const retryScreenshot = await this.retryScreenshot(page, referenceImage, screenshotSettings);
|
|
466
|
+
if (!retryScreenshot.screenshotPath) throw new Error("Screenshot buffer is undefined");
|
|
467
|
+
this.filesystem.writeActualFile(filename, retryScreenshot.screenshotPath);
|
|
468
|
+
this.logger.success(`Screenshot saved: ${filepath}`);
|
|
469
|
+
if (retryScreenshot.passed) this.logger.success(`Screenshot passed visual comparison`);
|
|
251
470
|
else this.logger.error(`Screenshot failed visual comparison`);
|
|
252
471
|
let diffImagePath;
|
|
253
|
-
if (options.saveDiffImage && comparisonResult.diffBuffer && !
|
|
472
|
+
if (options.saveDiffImage && retryScreenshot.comparisonResult.diffBuffer && !retryScreenshot.passed) {
|
|
254
473
|
const diffFilename = options.diffImageFilename || filename;
|
|
255
|
-
diffImagePath = this.getDiffFilePath(diffFilename);
|
|
256
|
-
|
|
257
|
-
node_fs.default.writeFileSync(diffImagePath, comparisonResult.diffBuffer);
|
|
474
|
+
diffImagePath = this.filesystem.getDiffFilePath(diffFilename);
|
|
475
|
+
this.filesystem.writeDiffFile(diffFilename, retryScreenshot.comparisonResult.diffBuffer);
|
|
258
476
|
this.logger.debug(`Diff image saved: ${diffImagePath}`);
|
|
259
477
|
}
|
|
478
|
+
if (retryScreenshot.comparisonResult.differentSizes) {
|
|
479
|
+
this.logger.warn(`Screenshot has different sizes than reference image`);
|
|
480
|
+
const diffFilename = options.diffImageFilename || filename;
|
|
481
|
+
diffImagePath = this.filesystem.getDiffFilePath(diffFilename);
|
|
482
|
+
const diffBuffer = createDiffSizePngImage(200, 200);
|
|
483
|
+
this.filesystem.writeDiffFile(diffFilename, diffBuffer);
|
|
484
|
+
this.logger.debug(`Diff Size image saved: ${diffImagePath}`);
|
|
485
|
+
}
|
|
260
486
|
return {
|
|
261
487
|
screenshotPath: filepath,
|
|
262
|
-
comparisonResult,
|
|
488
|
+
comparisonResult: retryScreenshot.comparisonResult,
|
|
263
489
|
diffImagePath
|
|
264
490
|
};
|
|
265
491
|
} catch (error) {
|
|
@@ -267,19 +493,135 @@ var ScreenshotTool = class {
|
|
|
267
493
|
throw error;
|
|
268
494
|
}
|
|
269
495
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
496
|
+
async retryScreenshot(page, referenceImage, options) {
|
|
497
|
+
for (let i = 0; i < this.retries; i++) try {
|
|
498
|
+
const screenshotBuffer = await this.takeScreenshotBuffer(page, {
|
|
499
|
+
...options,
|
|
500
|
+
delay: this.getIncBackoffDelay(i, options.delay || 0)
|
|
501
|
+
});
|
|
502
|
+
if (!screenshotBuffer) throw new Error("Screenshot buffer is undefined");
|
|
503
|
+
const comparisonResult = await compareImages(screenshotBuffer, referenceImage, true, this.diff);
|
|
504
|
+
if (comparisonResult.differentSizes) return {
|
|
505
|
+
screenshotPath: screenshotBuffer,
|
|
506
|
+
comparisonResult,
|
|
507
|
+
passed: false
|
|
508
|
+
};
|
|
509
|
+
if (i === this.retries - 1 && !comparisonResult.passed) {
|
|
510
|
+
this.logger.error(`Failed to match screenshot after ${this.retries} retries`);
|
|
511
|
+
return {
|
|
512
|
+
screenshotPath: screenshotBuffer,
|
|
513
|
+
comparisonResult,
|
|
514
|
+
passed: false
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
if (comparisonResult.passed) return {
|
|
518
|
+
screenshotPath: screenshotBuffer,
|
|
519
|
+
comparisonResult,
|
|
520
|
+
passed: true
|
|
521
|
+
};
|
|
522
|
+
else this.logger.warn(`Comparison did not match. Retrying in ${this.getIncBackoffDelay(i + 1, options.delay || 0)}ms... \nComparison result: ${comparisonResult.numDiffPixels} pixels different (${comparisonResult.percentDifference.toFixed(2)}%)`);
|
|
523
|
+
} catch (error) {
|
|
524
|
+
this.logger.error(`Error taking screenshot after ${i + 1} retries:`, error.message);
|
|
525
|
+
}
|
|
526
|
+
return {
|
|
527
|
+
screenshotPath: null,
|
|
528
|
+
comparisonResult: null,
|
|
529
|
+
passed: false
|
|
530
|
+
};
|
|
273
531
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
532
|
+
getVariantFilename(filename, variant) {
|
|
533
|
+
if (variant.filename) return variant.filename;
|
|
534
|
+
const parsed = node_path.default.parse(filename);
|
|
535
|
+
const suffix = variant.id.trim().replace(/[^a-zA-Z0-9_-]+/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "") || "variant";
|
|
536
|
+
const ext = parsed.ext || ".png";
|
|
537
|
+
const variantName = `${parsed.name}--${suffix}${ext}`;
|
|
538
|
+
return parsed.dir ? node_path.default.join(parsed.dir, variantName) : variantName;
|
|
539
|
+
}
|
|
540
|
+
async capture(page, filename, options, extras = {}) {
|
|
541
|
+
if (!this.browser) throw new Error("Browser not initialized");
|
|
542
|
+
const { variants = [],...baseOptions } = options;
|
|
543
|
+
const baseResult = { filename };
|
|
544
|
+
const variantResults = [];
|
|
545
|
+
const baseSaveDiff = extras.saveDiffImage ?? false;
|
|
546
|
+
const baseDiffFilename = extras.diffImageFilename ?? filename;
|
|
547
|
+
if (baseOptions.skip) return {
|
|
548
|
+
base: {
|
|
549
|
+
...baseResult,
|
|
550
|
+
skipped: true
|
|
551
|
+
},
|
|
552
|
+
variants: variants.map((variant) => ({
|
|
553
|
+
id: variant.id,
|
|
554
|
+
label: variant.label,
|
|
555
|
+
filename: this.getVariantFilename(filename, variant),
|
|
556
|
+
skipped: true
|
|
557
|
+
}))
|
|
558
|
+
};
|
|
559
|
+
if (this.filesystem.hasExpectedFile(filename)) {
|
|
560
|
+
const { screenshotPath, comparisonResult, diffImagePath } = await this.takeScreenshotWithComparison(page, filename, this.filesystem.readExpectedFile(filename), {
|
|
561
|
+
...baseOptions,
|
|
562
|
+
saveDiffImage: baseSaveDiff,
|
|
563
|
+
diffImageFilename: baseDiffFilename
|
|
564
|
+
});
|
|
565
|
+
baseResult.filepath = screenshotPath;
|
|
566
|
+
baseResult.comparisonResult = comparisonResult;
|
|
567
|
+
baseResult.diffImagePath = diffImagePath;
|
|
568
|
+
} else baseResult.filepath = await this.takeScreenshot(page, filename, baseOptions);
|
|
569
|
+
for (const variant of variants) {
|
|
570
|
+
const variantFilename = this.getVariantFilename(filename, variant);
|
|
571
|
+
const variantOptions = {
|
|
572
|
+
...baseOptions,
|
|
573
|
+
...variant.options
|
|
574
|
+
};
|
|
575
|
+
const variantResult = {
|
|
576
|
+
id: variant.id,
|
|
577
|
+
label: variant.label,
|
|
578
|
+
filename: variantFilename
|
|
579
|
+
};
|
|
580
|
+
if (variantOptions.skip) {
|
|
581
|
+
variantResult.skipped = true;
|
|
582
|
+
variantResults.push(variantResult);
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
const variantExtra = extras.variants?.[variant.id];
|
|
586
|
+
const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;
|
|
587
|
+
const variantDiffFilename = variantExtra?.diffImageFilename ?? variantFilename;
|
|
588
|
+
if (this.filesystem.hasExpectedFile(variantFilename)) {
|
|
589
|
+
const { screenshotPath, comparisonResult, diffImagePath } = await this.takeScreenshotWithComparison(page, variantFilename, this.filesystem.readExpectedFile(variantFilename), {
|
|
590
|
+
...variantOptions,
|
|
591
|
+
saveDiffImage: variantSaveDiff,
|
|
592
|
+
diffImageFilename: variantDiffFilename
|
|
593
|
+
});
|
|
594
|
+
variantResult.filepath = screenshotPath;
|
|
595
|
+
variantResult.comparisonResult = comparisonResult;
|
|
596
|
+
variantResult.diffImagePath = diffImagePath;
|
|
597
|
+
} else variantResult.filepath = await this.takeScreenshot(page, variantFilename, variantOptions);
|
|
598
|
+
variantResults.push(variantResult);
|
|
599
|
+
}
|
|
600
|
+
return {
|
|
601
|
+
base: baseResult,
|
|
602
|
+
variants: variantResults
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Gets the exponential backoff delay
|
|
607
|
+
*/
|
|
608
|
+
getIncBackoffDelay(i, delay) {
|
|
609
|
+
return i > 0 ? 500 * 2 ** (i - 1) + (delay || 0) : 0;
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Get a page from the pool by index
|
|
613
|
+
*/
|
|
614
|
+
getPageFromPool(index) {
|
|
615
|
+
if (index < 0) throw new Error(`Page index ${index} must be non-negative`);
|
|
616
|
+
if (index >= this.pages.length) throw new Error(`Page index ${index} exceeds pool size ${this.pages.length}`);
|
|
617
|
+
if (!this.pages[index]) throw new Error(`Page index ${index} is undefined`);
|
|
618
|
+
return this.pages[index];
|
|
278
619
|
}
|
|
279
620
|
};
|
|
280
621
|
var screenshot_default = ScreenshotTool;
|
|
281
622
|
|
|
282
623
|
//#endregion
|
|
624
|
+
exports.ScreenshotFileSystem = ScreenshotFileSystem;
|
|
283
625
|
exports.ScreenshotTool = screenshot_default;
|
|
284
626
|
exports.defineConfig = defineConfig;
|
|
285
627
|
//# sourceMappingURL=index.js.map
|