@cappa/core 0.1.3 → 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 +13 -0
- package/LICENSE +21 -0
- package/README.md +20 -0
- package/dist/index.d.mts +173 -36
- package/dist/index.d.ts +173 -36
- package/dist/index.js +391 -86
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +391 -87
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -11
- 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 +344 -104
- package/src/types.ts +19 -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,15 +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;
|
|
117
322
|
retries;
|
|
323
|
+
filesystem;
|
|
118
324
|
constructor(options) {
|
|
119
325
|
this.browserType = options.browserType || "chromium";
|
|
120
326
|
this.headless = options.headless !== false;
|
|
@@ -127,17 +333,15 @@ var ScreenshotTool = class {
|
|
|
127
333
|
...defaultDiffConfig,
|
|
128
334
|
...options.diff
|
|
129
335
|
};
|
|
130
|
-
this.
|
|
131
|
-
this.diffDir = node_path.default.join(this.outputDir, "diff");
|
|
132
|
-
this.expectedDir = node_path.default.join(this.outputDir, "expected");
|
|
336
|
+
this.concurrency = Math.max(1, options.concurrency || 1);
|
|
133
337
|
this.logger = (0, __cappa_logger.getLogger)();
|
|
134
338
|
this.retries = options.retries || 2;
|
|
339
|
+
this.filesystem = new ScreenshotFileSystem(this.outputDir);
|
|
135
340
|
}
|
|
136
341
|
/**
|
|
137
342
|
* Initialize playwright and page
|
|
138
343
|
*/
|
|
139
344
|
async init() {
|
|
140
|
-
this.createDirectories();
|
|
141
345
|
const browserClass = {
|
|
142
346
|
chromium: playwright_core.chromium,
|
|
143
347
|
firefox: playwright_core.firefox,
|
|
@@ -146,20 +350,28 @@ var ScreenshotTool = class {
|
|
|
146
350
|
if (!browserClass) throw new Error(`Unsupported browser type: ${this.browserType}`);
|
|
147
351
|
this.browser = await browserClass.launch({ headless: this.headless });
|
|
148
352
|
const defaultUserAgent = (await this.browser.newContext()).newPage().then((page) => page.evaluate(() => navigator.userAgent));
|
|
149
|
-
this.
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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;
|
|
156
368
|
}
|
|
157
369
|
/**
|
|
158
370
|
* Closes browser
|
|
159
371
|
* If not closed, the process will not exit
|
|
160
372
|
*/
|
|
161
373
|
async close() {
|
|
162
|
-
|
|
374
|
+
for (const context of this.contexts) await context.close();
|
|
163
375
|
if (this.browser) await this.browser.close();
|
|
164
376
|
}
|
|
165
377
|
/**
|
|
@@ -170,46 +382,13 @@ var ScreenshotTool = class {
|
|
|
170
382
|
await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
171
383
|
}
|
|
172
384
|
/**
|
|
173
|
-
* Creates directories for screenshots and other files
|
|
174
|
-
*/
|
|
175
|
-
createDirectories() {
|
|
176
|
-
const directories = [
|
|
177
|
-
this.outputDir,
|
|
178
|
-
this.actualDir,
|
|
179
|
-
this.diffDir,
|
|
180
|
-
this.expectedDir
|
|
181
|
-
];
|
|
182
|
-
for (const dir of directories) if (!node_fs.default.existsSync(dir)) {
|
|
183
|
-
node_fs.default.mkdirSync(dir, { recursive: true });
|
|
184
|
-
this.logger.debug(`Initialized directory: ${dir}`);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Gets the path to the actual screenshot file
|
|
189
|
-
*/
|
|
190
|
-
getActualFilePath(filename) {
|
|
191
|
-
return node_path.default.join(this.actualDir, filename);
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Gets the path to the diff screenshot file
|
|
195
|
-
*/
|
|
196
|
-
getDiffFilePath(filename) {
|
|
197
|
-
return node_path.default.join(this.diffDir, filename);
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Gets the path to the expected screenshot file
|
|
201
|
-
*/
|
|
202
|
-
getExpectedFilePath(filename) {
|
|
203
|
-
return node_path.default.join(this.expectedDir, filename);
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
385
|
* Takes a screenshot of the page
|
|
207
386
|
*/
|
|
208
387
|
async takeScreenshot(page, filename, options) {
|
|
209
388
|
if (!this.browser) throw new Error("Browser not initialized");
|
|
210
389
|
if (options.skip) return;
|
|
211
390
|
try {
|
|
212
|
-
const filepath = this.getActualFilePath(filename);
|
|
391
|
+
const filepath = this.filesystem.getActualFilePath(filename);
|
|
213
392
|
const screenshotOptions = {
|
|
214
393
|
path: filepath,
|
|
215
394
|
fullPage: options.fullPage,
|
|
@@ -217,43 +396,93 @@ var ScreenshotTool = class {
|
|
|
217
396
|
timeout: 6e4,
|
|
218
397
|
mask: options.mask,
|
|
219
398
|
omitBackground: options.omitBackground,
|
|
220
|
-
scale: "css"
|
|
399
|
+
scale: "css",
|
|
400
|
+
animations: "disabled",
|
|
401
|
+
caret: "hide"
|
|
221
402
|
};
|
|
222
|
-
|
|
223
|
-
|
|
403
|
+
await this.withViewport(page, options.viewport, async () => {
|
|
404
|
+
if (options.delay) await page.waitForTimeout(options.delay);
|
|
405
|
+
await page.screenshot(screenshotOptions);
|
|
406
|
+
});
|
|
224
407
|
return filepath;
|
|
225
408
|
} catch (error) {
|
|
226
409
|
this.logger.error(`Error taking screenshot of ${page.url()}:`, error.message);
|
|
227
410
|
throw error;
|
|
228
411
|
}
|
|
229
412
|
}
|
|
230
|
-
|
|
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) {
|
|
231
432
|
if (!this.browser) throw new Error("Browser not initialized");
|
|
433
|
+
if (options.skip) throw new Error("Screenshot skipped");
|
|
232
434
|
try {
|
|
233
|
-
const filepath = this.getActualFilePath(filename);
|
|
234
|
-
node_fs.default.mkdirSync(node_path.default.dirname(filepath), { recursive: true });
|
|
235
435
|
const screenshotOptions = {
|
|
236
436
|
fullPage: options.fullPage,
|
|
237
437
|
type: "png",
|
|
238
438
|
timeout: 6e4,
|
|
239
439
|
mask: options.mask,
|
|
240
440
|
omitBackground: options.omitBackground,
|
|
241
|
-
scale: "css"
|
|
441
|
+
scale: "css",
|
|
442
|
+
animations: "disabled",
|
|
443
|
+
caret: "hide"
|
|
444
|
+
};
|
|
445
|
+
return await this.withViewport(page, options.viewport, async () => {
|
|
446
|
+
if (options.delay) await page.waitForTimeout(options.delay);
|
|
447
|
+
return page.screenshot(screenshotOptions);
|
|
448
|
+
});
|
|
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
|
|
242
464
|
};
|
|
243
|
-
const retryScreenshot = await this.retryScreenshot(page,
|
|
465
|
+
const retryScreenshot = await this.retryScreenshot(page, referenceImage, screenshotSettings);
|
|
244
466
|
if (!retryScreenshot.screenshotPath) throw new Error("Screenshot buffer is undefined");
|
|
245
|
-
|
|
467
|
+
this.filesystem.writeActualFile(filename, retryScreenshot.screenshotPath);
|
|
246
468
|
this.logger.success(`Screenshot saved: ${filepath}`);
|
|
247
469
|
if (retryScreenshot.passed) this.logger.success(`Screenshot passed visual comparison`);
|
|
248
470
|
else this.logger.error(`Screenshot failed visual comparison`);
|
|
249
471
|
let diffImagePath;
|
|
250
472
|
if (options.saveDiffImage && retryScreenshot.comparisonResult.diffBuffer && !retryScreenshot.passed) {
|
|
251
473
|
const diffFilename = options.diffImageFilename || filename;
|
|
252
|
-
diffImagePath = this.getDiffFilePath(diffFilename);
|
|
253
|
-
|
|
254
|
-
node_fs.default.writeFileSync(diffImagePath, retryScreenshot.comparisonResult.diffBuffer);
|
|
474
|
+
diffImagePath = this.filesystem.getDiffFilePath(diffFilename);
|
|
475
|
+
this.filesystem.writeDiffFile(diffFilename, retryScreenshot.comparisonResult.diffBuffer);
|
|
255
476
|
this.logger.debug(`Diff image saved: ${diffImagePath}`);
|
|
256
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
|
+
}
|
|
257
486
|
return {
|
|
258
487
|
screenshotPath: filepath,
|
|
259
488
|
comparisonResult: retryScreenshot.comparisonResult,
|
|
@@ -264,17 +493,19 @@ var ScreenshotTool = class {
|
|
|
264
493
|
throw error;
|
|
265
494
|
}
|
|
266
495
|
}
|
|
267
|
-
async retryScreenshot(page,
|
|
496
|
+
async retryScreenshot(page, referenceImage, options) {
|
|
268
497
|
for (let i = 0; i < this.retries; i++) try {
|
|
269
|
-
const screenshotBuffer = await this.
|
|
498
|
+
const screenshotBuffer = await this.takeScreenshotBuffer(page, {
|
|
270
499
|
...options,
|
|
271
500
|
delay: this.getIncBackoffDelay(i, options.delay || 0)
|
|
272
501
|
});
|
|
273
502
|
if (!screenshotBuffer) throw new Error("Screenshot buffer is undefined");
|
|
274
|
-
const comparisonResult = await compareImages(screenshotBuffer, referenceImage, true,
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
503
|
+
const comparisonResult = await compareImages(screenshotBuffer, referenceImage, true, this.diff);
|
|
504
|
+
if (comparisonResult.differentSizes) return {
|
|
505
|
+
screenshotPath: screenshotBuffer,
|
|
506
|
+
comparisonResult,
|
|
507
|
+
passed: false
|
|
508
|
+
};
|
|
278
509
|
if (i === this.retries - 1 && !comparisonResult.passed) {
|
|
279
510
|
this.logger.error(`Failed to match screenshot after ${this.retries} retries`);
|
|
280
511
|
return {
|
|
@@ -298,25 +529,99 @@ var ScreenshotTool = class {
|
|
|
298
529
|
passed: false
|
|
299
530
|
};
|
|
300
531
|
}
|
|
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
|
+
}
|
|
301
605
|
/**
|
|
302
606
|
* Gets the exponential backoff delay
|
|
303
607
|
*/
|
|
304
608
|
getIncBackoffDelay(i, delay) {
|
|
305
609
|
return i > 0 ? 500 * 2 ** (i - 1) + (delay || 0) : 0;
|
|
306
610
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
if (!
|
|
314
|
-
return
|
|
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];
|
|
315
619
|
}
|
|
316
620
|
};
|
|
317
621
|
var screenshot_default = ScreenshotTool;
|
|
318
622
|
|
|
319
623
|
//#endregion
|
|
624
|
+
exports.ScreenshotFileSystem = ScreenshotFileSystem;
|
|
320
625
|
exports.ScreenshotTool = screenshot_default;
|
|
321
626
|
exports.defineConfig = defineConfig;
|
|
322
627
|
//# sourceMappingURL=index.js.map
|