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