@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/src/screenshot.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
1
|
import path from "node:path";
|
|
3
2
|
import { getLogger, type Logger } from "@cappa/logger";
|
|
4
3
|
import {
|
|
@@ -6,17 +5,22 @@ import {
|
|
|
6
5
|
type BrowserContext,
|
|
7
6
|
chromium,
|
|
8
7
|
firefox,
|
|
9
|
-
type Locator,
|
|
10
8
|
type Page,
|
|
11
9
|
type PageScreenshotOptions,
|
|
12
10
|
webkit,
|
|
13
11
|
} from "playwright-core";
|
|
14
12
|
import {
|
|
15
|
-
type CompareOptions,
|
|
16
13
|
type CompareResult,
|
|
17
14
|
compareImages,
|
|
15
|
+
createDiffSizePngImage,
|
|
18
16
|
} from "./compare";
|
|
19
|
-
import
|
|
17
|
+
import { ScreenshotFileSystem } from "./filesystem";
|
|
18
|
+
import type {
|
|
19
|
+
DiffConfig,
|
|
20
|
+
ScreenshotOptions,
|
|
21
|
+
ScreenshotSettings,
|
|
22
|
+
ScreenshotVariant,
|
|
23
|
+
} from "./types";
|
|
20
24
|
|
|
21
25
|
const defaultDiffConfig: DiffConfig = {
|
|
22
26
|
threshold: 0.1,
|
|
@@ -26,19 +30,52 @@ const defaultDiffConfig: DiffConfig = {
|
|
|
26
30
|
maxDiffPercentage: 0,
|
|
27
31
|
};
|
|
28
32
|
|
|
33
|
+
export interface ScreenshotCaptureDetails {
|
|
34
|
+
filename: string;
|
|
35
|
+
filepath?: string;
|
|
36
|
+
comparisonResult?: CompareResult;
|
|
37
|
+
diffImagePath?: string;
|
|
38
|
+
skipped?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface ScreenshotVariantCaptureDetails
|
|
42
|
+
extends ScreenshotCaptureDetails {
|
|
43
|
+
id: string;
|
|
44
|
+
label?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ScreenshotCaptureResult {
|
|
48
|
+
base: ScreenshotCaptureDetails;
|
|
49
|
+
variants: ScreenshotVariantCaptureDetails[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ScreenshotCaptureExtras {
|
|
53
|
+
saveDiffImage?: boolean;
|
|
54
|
+
diffImageFilename?: string;
|
|
55
|
+
variants?: Record<
|
|
56
|
+
string,
|
|
57
|
+
{
|
|
58
|
+
saveDiffImage?: boolean;
|
|
59
|
+
diffImageFilename?: string;
|
|
60
|
+
}
|
|
61
|
+
>;
|
|
62
|
+
}
|
|
63
|
+
|
|
29
64
|
class ScreenshotTool {
|
|
30
65
|
browserType: "chromium" | "firefox" | "webkit";
|
|
31
66
|
headless: boolean;
|
|
32
67
|
viewport: { width: number; height: number };
|
|
33
68
|
outputDir: string;
|
|
34
|
-
actualDir: string;
|
|
35
|
-
diffDir: string;
|
|
36
|
-
expectedDir: string;
|
|
37
69
|
browser: Browser | null = null;
|
|
38
70
|
context: BrowserContext | null = null;
|
|
39
71
|
page: Page | null = null;
|
|
72
|
+
concurrency: number;
|
|
73
|
+
contexts: BrowserContext[] = [];
|
|
74
|
+
pages: Page[] = [];
|
|
40
75
|
diff: DiffConfig;
|
|
41
76
|
logger: Logger;
|
|
77
|
+
retries: number;
|
|
78
|
+
filesystem: ScreenshotFileSystem;
|
|
42
79
|
|
|
43
80
|
constructor(options: {
|
|
44
81
|
browserType?: "chromium" | "firefox" | "webkit";
|
|
@@ -46,27 +83,25 @@ class ScreenshotTool {
|
|
|
46
83
|
viewport?: { width: number; height: number };
|
|
47
84
|
outputDir?: string;
|
|
48
85
|
diff?: DiffConfig;
|
|
86
|
+
retries?: number;
|
|
87
|
+
concurrency?: number;
|
|
49
88
|
}) {
|
|
50
89
|
this.browserType = options.browserType || "chromium";
|
|
51
90
|
this.headless = options.headless !== false; // Default to headless
|
|
52
91
|
this.viewport = options.viewport || { width: 1920, height: 1080 };
|
|
53
92
|
this.outputDir = options.outputDir || "./screenshots";
|
|
54
93
|
this.diff = { ...defaultDiffConfig, ...options.diff };
|
|
55
|
-
|
|
56
|
-
this.actualDir = path.join(this.outputDir, "actual");
|
|
57
|
-
this.diffDir = path.join(this.outputDir, "diff");
|
|
58
|
-
this.expectedDir = path.join(this.outputDir, "expected");
|
|
94
|
+
this.concurrency = Math.max(1, options.concurrency || 1);
|
|
59
95
|
|
|
60
96
|
this.logger = getLogger();
|
|
97
|
+
this.retries = options.retries || 2;
|
|
98
|
+
this.filesystem = new ScreenshotFileSystem(this.outputDir);
|
|
61
99
|
}
|
|
62
100
|
|
|
63
101
|
/**
|
|
64
102
|
* Initialize playwright and page
|
|
65
103
|
*/
|
|
66
104
|
async init() {
|
|
67
|
-
// Create output directories if they don't exist
|
|
68
|
-
this.createDirectories();
|
|
69
|
-
|
|
70
105
|
// Launch browser based on type
|
|
71
106
|
const browserMap = {
|
|
72
107
|
chromium,
|
|
@@ -87,14 +122,26 @@ class ScreenshotTool {
|
|
|
87
122
|
.newPage()
|
|
88
123
|
.then((page) => page.evaluate(() => navigator.userAgent));
|
|
89
124
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
125
|
+
// Create N contexts and pages
|
|
126
|
+
this.contexts = [];
|
|
127
|
+
this.pages = [];
|
|
128
|
+
|
|
129
|
+
for (let i = 0; i < this.concurrency; i++) {
|
|
130
|
+
const context = await this.browser.newContext({
|
|
131
|
+
reducedMotion: "reduce",
|
|
132
|
+
deviceScaleFactor: 2,
|
|
133
|
+
userAgent: `${await defaultUserAgent} CappaStorybook`,
|
|
134
|
+
viewport: this.viewport,
|
|
135
|
+
});
|
|
136
|
+
const page = await context.newPage();
|
|
137
|
+
|
|
138
|
+
this.contexts.push(context);
|
|
139
|
+
this.pages.push(page);
|
|
140
|
+
}
|
|
96
141
|
|
|
97
|
-
|
|
142
|
+
// Backward compatibility
|
|
143
|
+
this.context = this.contexts[0] ?? null;
|
|
144
|
+
this.page = this.pages[0] ?? null;
|
|
98
145
|
}
|
|
99
146
|
|
|
100
147
|
/**
|
|
@@ -102,8 +149,8 @@ class ScreenshotTool {
|
|
|
102
149
|
* If not closed, the process will not exit
|
|
103
150
|
*/
|
|
104
151
|
async close() {
|
|
105
|
-
|
|
106
|
-
await
|
|
152
|
+
for (const context of this.contexts) {
|
|
153
|
+
await context.close();
|
|
107
154
|
}
|
|
108
155
|
if (this.browser) {
|
|
109
156
|
await this.browser.close();
|
|
@@ -121,85 +168,133 @@ class ScreenshotTool {
|
|
|
121
168
|
}
|
|
122
169
|
|
|
123
170
|
/**
|
|
124
|
-
*
|
|
171
|
+
* Takes a screenshot of the page
|
|
125
172
|
*/
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
];
|
|
134
|
-
|
|
135
|
-
for (const dir of directories) {
|
|
136
|
-
if (!fs.existsSync(dir)) {
|
|
137
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
138
|
-
this.logger.debug(`Initialized directory: ${dir}`);
|
|
139
|
-
}
|
|
173
|
+
async takeScreenshot(
|
|
174
|
+
page: Page,
|
|
175
|
+
filename: string,
|
|
176
|
+
options: ScreenshotSettings,
|
|
177
|
+
) {
|
|
178
|
+
if (!this.browser) {
|
|
179
|
+
throw new Error("Browser not initialized");
|
|
140
180
|
}
|
|
141
|
-
}
|
|
142
181
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
getActualFilePath(filename: string): string {
|
|
147
|
-
return path.join(this.actualDir, filename);
|
|
148
|
-
}
|
|
182
|
+
if (options.skip) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
149
185
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
186
|
+
try {
|
|
187
|
+
// Generate filename - save to actual directory
|
|
188
|
+
const filepath = this.filesystem.getActualFilePath(filename);
|
|
189
|
+
|
|
190
|
+
// Take screenshot
|
|
191
|
+
const screenshotOptions = {
|
|
192
|
+
path: filepath,
|
|
193
|
+
fullPage: options.fullPage,
|
|
194
|
+
type: "png" as const,
|
|
195
|
+
timeout: 60000,
|
|
196
|
+
mask: options.mask,
|
|
197
|
+
omitBackground: options.omitBackground,
|
|
198
|
+
scale: "css" as const,
|
|
199
|
+
animations: "disabled",
|
|
200
|
+
caret: "hide",
|
|
201
|
+
} satisfies PageScreenshotOptions;
|
|
202
|
+
|
|
203
|
+
await this.withViewport(page, options.viewport, async () => {
|
|
204
|
+
if (options.delay) {
|
|
205
|
+
await page.waitForTimeout(options.delay);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
await page.screenshot(screenshotOptions);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
return filepath;
|
|
212
|
+
} catch (error) {
|
|
213
|
+
this.logger.error(
|
|
214
|
+
`Error taking screenshot of ${page.url()}:`,
|
|
215
|
+
(error as Error).message,
|
|
216
|
+
);
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
155
219
|
}
|
|
156
220
|
|
|
157
221
|
/**
|
|
158
|
-
*
|
|
222
|
+
* Sets the viewport size and executes the action, ensuring the original viewport is restored
|
|
223
|
+
* even if the action throws an error.
|
|
159
224
|
*/
|
|
160
|
-
|
|
161
|
-
|
|
225
|
+
private async withViewport<T>(
|
|
226
|
+
page: Page,
|
|
227
|
+
viewport: ScreenshotSettings["viewport"],
|
|
228
|
+
action: () => Promise<T>,
|
|
229
|
+
): Promise<T> {
|
|
230
|
+
if (!viewport) {
|
|
231
|
+
return action();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const previousViewport = page.viewportSize();
|
|
235
|
+
|
|
236
|
+
// Check if the new viewport is the same as the current one
|
|
237
|
+
if (
|
|
238
|
+
previousViewport &&
|
|
239
|
+
previousViewport.width === viewport.width &&
|
|
240
|
+
previousViewport.height === viewport.height
|
|
241
|
+
) {
|
|
242
|
+
return action();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
await page.setViewportSize(viewport);
|
|
246
|
+
|
|
247
|
+
try {
|
|
248
|
+
return await action();
|
|
249
|
+
} finally {
|
|
250
|
+
// Always restore the original viewport, even if the action throws an error
|
|
251
|
+
if (previousViewport) {
|
|
252
|
+
await page.setViewportSize(previousViewport);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
162
255
|
}
|
|
163
256
|
|
|
164
257
|
/**
|
|
165
|
-
* Takes a screenshot
|
|
258
|
+
* Takes a screenshot and returns the buffer (for comparison purposes)
|
|
166
259
|
*/
|
|
167
|
-
async
|
|
260
|
+
async takeScreenshotBuffer(
|
|
168
261
|
page: Page,
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
) {
|
|
262
|
+
options: ScreenshotSettings,
|
|
263
|
+
): Promise<Buffer> {
|
|
172
264
|
if (!this.browser) {
|
|
173
265
|
throw new Error("Browser not initialized");
|
|
174
266
|
}
|
|
175
267
|
|
|
176
268
|
if (options.skip) {
|
|
177
|
-
|
|
269
|
+
throw new Error("Screenshot skipped");
|
|
178
270
|
}
|
|
179
271
|
|
|
180
272
|
try {
|
|
181
|
-
//
|
|
182
|
-
const filepath = this.getActualFilePath(filename);
|
|
183
|
-
|
|
184
|
-
// Take screenshot
|
|
273
|
+
// Take screenshot without saving to file
|
|
185
274
|
const screenshotOptions = {
|
|
186
|
-
path: filepath,
|
|
187
275
|
fullPage: options.fullPage,
|
|
188
276
|
type: "png" as const,
|
|
189
277
|
timeout: 60000,
|
|
190
278
|
mask: options.mask,
|
|
191
279
|
omitBackground: options.omitBackground,
|
|
192
280
|
scale: "css" as const,
|
|
281
|
+
animations: "disabled",
|
|
282
|
+
caret: "hide",
|
|
193
283
|
} satisfies PageScreenshotOptions;
|
|
194
284
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
285
|
+
const buffer = await this.withViewport(
|
|
286
|
+
page,
|
|
287
|
+
options.viewport,
|
|
288
|
+
async () => {
|
|
289
|
+
if (options.delay) {
|
|
290
|
+
await page.waitForTimeout(options.delay);
|
|
291
|
+
}
|
|
198
292
|
|
|
199
|
-
|
|
293
|
+
return page.screenshot(screenshotOptions);
|
|
294
|
+
},
|
|
295
|
+
);
|
|
200
296
|
|
|
201
|
-
|
|
202
|
-
return filepath;
|
|
297
|
+
return buffer;
|
|
203
298
|
} catch (error) {
|
|
204
299
|
this.logger.error(
|
|
205
300
|
`Error taking screenshot of ${page.url()}:`,
|
|
@@ -213,16 +308,10 @@ class ScreenshotTool {
|
|
|
213
308
|
page: Page,
|
|
214
309
|
filename: string,
|
|
215
310
|
referenceImage: Buffer,
|
|
216
|
-
options: {
|
|
217
|
-
fullPage?: boolean;
|
|
218
|
-
mask?: Locator[];
|
|
219
|
-
omitBackground?: boolean;
|
|
220
|
-
// Comparison options
|
|
221
|
-
compareOptions?: CompareOptions;
|
|
222
|
-
maxDifferencePercent?: number;
|
|
311
|
+
options: ScreenshotSettings & {
|
|
223
312
|
saveDiffImage?: boolean;
|
|
224
313
|
diffImageFilename?: string;
|
|
225
|
-
}
|
|
314
|
+
},
|
|
226
315
|
): Promise<{
|
|
227
316
|
screenshotPath: string;
|
|
228
317
|
comparisonResult: CompareResult;
|
|
@@ -234,40 +323,31 @@ class ScreenshotTool {
|
|
|
234
323
|
|
|
235
324
|
try {
|
|
236
325
|
// Generate filename and take screenshot - save to actual directory
|
|
237
|
-
const filepath = this.getActualFilePath(filename);
|
|
238
|
-
fs.mkdirSync(path.dirname(filepath), { recursive: true });
|
|
326
|
+
const filepath = this.filesystem.getActualFilePath(filename);
|
|
239
327
|
|
|
240
|
-
|
|
241
|
-
const screenshotOptions = {
|
|
328
|
+
const screenshotSettings: ScreenshotSettings = {
|
|
242
329
|
fullPage: options.fullPage,
|
|
243
|
-
type: "png" as const,
|
|
244
|
-
timeout: 60000,
|
|
245
330
|
mask: options.mask,
|
|
246
331
|
omitBackground: options.omitBackground,
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
// Get screenshot as buffer for comparison
|
|
251
|
-
const screenshotBuffer = await page.screenshot(screenshotOptions);
|
|
252
|
-
|
|
253
|
-
// Save screenshot to actual directory
|
|
254
|
-
fs.writeFileSync(filepath, screenshotBuffer);
|
|
255
|
-
|
|
256
|
-
this.logger.success(`Screenshot saved: ${filepath}`);
|
|
332
|
+
delay: options.delay,
|
|
333
|
+
viewport: options.viewport,
|
|
334
|
+
};
|
|
257
335
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
screenshotBuffer,
|
|
336
|
+
const retryScreenshot = await this.retryScreenshot(
|
|
337
|
+
page,
|
|
261
338
|
referenceImage,
|
|
262
|
-
|
|
263
|
-
{ ...this.diff, ...options.compareOptions },
|
|
339
|
+
screenshotSettings,
|
|
264
340
|
);
|
|
265
341
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
342
|
+
if (!retryScreenshot.screenshotPath) {
|
|
343
|
+
throw new Error("Screenshot buffer is undefined");
|
|
344
|
+
}
|
|
269
345
|
|
|
270
|
-
|
|
346
|
+
// Save screenshot to actual directory
|
|
347
|
+
this.filesystem.writeActualFile(filename, retryScreenshot.screenshotPath);
|
|
348
|
+
this.logger.success(`Screenshot saved: ${filepath}`);
|
|
349
|
+
|
|
350
|
+
if (retryScreenshot.passed) {
|
|
271
351
|
this.logger.success(`Screenshot passed visual comparison`);
|
|
272
352
|
} else {
|
|
273
353
|
this.logger.error(`Screenshot failed visual comparison`);
|
|
@@ -278,22 +358,37 @@ class ScreenshotTool {
|
|
|
278
358
|
// Save diff image if requested and there are differences
|
|
279
359
|
if (
|
|
280
360
|
options.saveDiffImage &&
|
|
281
|
-
comparisonResult.diffBuffer &&
|
|
282
|
-
!
|
|
361
|
+
retryScreenshot.comparisonResult.diffBuffer &&
|
|
362
|
+
!retryScreenshot.passed
|
|
283
363
|
) {
|
|
284
364
|
const diffFilename = options.diffImageFilename || filename;
|
|
285
365
|
// Save to diff directory
|
|
286
|
-
diffImagePath = this.getDiffFilePath(diffFilename);
|
|
366
|
+
diffImagePath = this.filesystem.getDiffFilePath(diffFilename);
|
|
287
367
|
|
|
288
|
-
|
|
289
|
-
|
|
368
|
+
this.filesystem.writeDiffFile(
|
|
369
|
+
diffFilename,
|
|
370
|
+
retryScreenshot.comparisonResult.diffBuffer,
|
|
371
|
+
);
|
|
290
372
|
|
|
291
373
|
this.logger.debug(`Diff image saved: ${diffImagePath}`);
|
|
292
374
|
}
|
|
293
375
|
|
|
376
|
+
// different sizes diff image
|
|
377
|
+
if (retryScreenshot.comparisonResult.differentSizes) {
|
|
378
|
+
this.logger.warn(`Screenshot has different sizes than reference image`);
|
|
379
|
+
|
|
380
|
+
const diffFilename = options.diffImageFilename || filename;
|
|
381
|
+
diffImagePath = this.filesystem.getDiffFilePath(diffFilename);
|
|
382
|
+
const diffBuffer = createDiffSizePngImage(200, 200);
|
|
383
|
+
|
|
384
|
+
this.filesystem.writeDiffFile(diffFilename, diffBuffer);
|
|
385
|
+
|
|
386
|
+
this.logger.debug(`Diff Size image saved: ${diffImagePath}`);
|
|
387
|
+
}
|
|
388
|
+
|
|
294
389
|
return {
|
|
295
390
|
screenshotPath: filepath,
|
|
296
|
-
comparisonResult,
|
|
391
|
+
comparisonResult: retryScreenshot.comparisonResult,
|
|
297
392
|
diffImagePath,
|
|
298
393
|
};
|
|
299
394
|
} catch (error) {
|
|
@@ -305,19 +400,230 @@ class ScreenshotTool {
|
|
|
305
400
|
}
|
|
306
401
|
}
|
|
307
402
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
403
|
+
async retryScreenshot(
|
|
404
|
+
page: Page,
|
|
405
|
+
referenceImage: Buffer,
|
|
406
|
+
options: ScreenshotSettings,
|
|
407
|
+
) {
|
|
408
|
+
for (let i = 0; i < this.retries; i++) {
|
|
409
|
+
try {
|
|
410
|
+
const screenshotBuffer = await this.takeScreenshotBuffer(page, {
|
|
411
|
+
...options,
|
|
412
|
+
delay: this.getIncBackoffDelay(i, options.delay || 0),
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
if (!screenshotBuffer) {
|
|
416
|
+
throw new Error("Screenshot buffer is undefined");
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const comparisonResult = await compareImages(
|
|
420
|
+
screenshotBuffer,
|
|
421
|
+
referenceImage,
|
|
422
|
+
true,
|
|
423
|
+
this.diff,
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
// bail early if the images are different sizes
|
|
427
|
+
if (comparisonResult.differentSizes) {
|
|
428
|
+
return {
|
|
429
|
+
screenshotPath: screenshotBuffer,
|
|
430
|
+
comparisonResult,
|
|
431
|
+
passed: false,
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// last retry
|
|
436
|
+
if (i === this.retries - 1 && !comparisonResult.passed) {
|
|
437
|
+
this.logger.error(
|
|
438
|
+
`Failed to match screenshot after ${this.retries} retries`,
|
|
439
|
+
);
|
|
440
|
+
return {
|
|
441
|
+
screenshotPath: screenshotBuffer,
|
|
442
|
+
comparisonResult,
|
|
443
|
+
passed: false,
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (comparisonResult.passed) {
|
|
448
|
+
return {
|
|
449
|
+
screenshotPath: screenshotBuffer,
|
|
450
|
+
comparisonResult,
|
|
451
|
+
passed: true,
|
|
452
|
+
};
|
|
453
|
+
} else {
|
|
454
|
+
this.logger.warn(
|
|
455
|
+
`Comparison did not match. Retrying in ${this.getIncBackoffDelay(i + 1, options.delay || 0)}ms... \nComparison result: ${comparisonResult.numDiffPixels} pixels different (${comparisonResult.percentDifference.toFixed(2)}%)`,
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
} catch (error) {
|
|
459
|
+
this.logger.error(
|
|
460
|
+
`Error taking screenshot after ${i + 1} retries:`,
|
|
461
|
+
(error as Error).message,
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
return {
|
|
467
|
+
screenshotPath: null,
|
|
468
|
+
comparisonResult: null,
|
|
469
|
+
passed: false,
|
|
470
|
+
};
|
|
312
471
|
}
|
|
313
472
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
if (!fs.existsSync(expectedPath)) {
|
|
318
|
-
throw new Error(`Expected image not found: ${expectedPath}`);
|
|
473
|
+
getVariantFilename(filename: string, variant: ScreenshotVariant): string {
|
|
474
|
+
if (variant.filename) {
|
|
475
|
+
return variant.filename;
|
|
319
476
|
}
|
|
320
|
-
|
|
477
|
+
|
|
478
|
+
const parsed = path.parse(filename);
|
|
479
|
+
const sanitizedId = variant.id
|
|
480
|
+
.trim()
|
|
481
|
+
.replace(/[^a-zA-Z0-9_-]+/g, "-")
|
|
482
|
+
.replace(/-+/g, "-")
|
|
483
|
+
.replace(/^-|-$/g, "");
|
|
484
|
+
const suffix = sanitizedId || "variant";
|
|
485
|
+
const ext = parsed.ext || ".png";
|
|
486
|
+
const variantName = `${parsed.name}--${suffix}${ext}`;
|
|
487
|
+
|
|
488
|
+
return parsed.dir ? path.join(parsed.dir, variantName) : variantName;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
async capture(
|
|
492
|
+
page: Page,
|
|
493
|
+
filename: string,
|
|
494
|
+
options: ScreenshotOptions,
|
|
495
|
+
extras: ScreenshotCaptureExtras = {},
|
|
496
|
+
): Promise<ScreenshotCaptureResult> {
|
|
497
|
+
if (!this.browser) {
|
|
498
|
+
throw new Error("Browser not initialized");
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const { variants = [], ...baseOptions } = options;
|
|
502
|
+
const baseResult: ScreenshotCaptureDetails = { filename };
|
|
503
|
+
const variantResults: ScreenshotVariantCaptureDetails[] = [];
|
|
504
|
+
|
|
505
|
+
const baseSaveDiff = extras.saveDiffImage ?? false;
|
|
506
|
+
const baseDiffFilename = extras.diffImageFilename ?? filename;
|
|
507
|
+
|
|
508
|
+
if (baseOptions.skip) {
|
|
509
|
+
return {
|
|
510
|
+
base: { ...baseResult, skipped: true },
|
|
511
|
+
variants: variants.map((variant) => ({
|
|
512
|
+
id: variant.id,
|
|
513
|
+
label: variant.label,
|
|
514
|
+
filename: this.getVariantFilename(filename, variant),
|
|
515
|
+
skipped: true,
|
|
516
|
+
})),
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (this.filesystem.hasExpectedFile(filename)) {
|
|
521
|
+
const { screenshotPath, comparisonResult, diffImagePath } =
|
|
522
|
+
await this.takeScreenshotWithComparison(
|
|
523
|
+
page,
|
|
524
|
+
filename,
|
|
525
|
+
this.filesystem.readExpectedFile(filename),
|
|
526
|
+
{
|
|
527
|
+
...baseOptions,
|
|
528
|
+
saveDiffImage: baseSaveDiff,
|
|
529
|
+
diffImageFilename: baseDiffFilename,
|
|
530
|
+
},
|
|
531
|
+
);
|
|
532
|
+
|
|
533
|
+
baseResult.filepath = screenshotPath;
|
|
534
|
+
baseResult.comparisonResult = comparisonResult;
|
|
535
|
+
baseResult.diffImagePath = diffImagePath;
|
|
536
|
+
} else {
|
|
537
|
+
baseResult.filepath = await this.takeScreenshot(
|
|
538
|
+
page,
|
|
539
|
+
filename,
|
|
540
|
+
baseOptions,
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
for (const variant of variants) {
|
|
545
|
+
const variantFilename = this.getVariantFilename(filename, variant);
|
|
546
|
+
const variantOptions: ScreenshotSettings = {
|
|
547
|
+
...baseOptions,
|
|
548
|
+
...variant.options,
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
const variantResult: ScreenshotVariantCaptureDetails = {
|
|
552
|
+
id: variant.id,
|
|
553
|
+
label: variant.label,
|
|
554
|
+
filename: variantFilename,
|
|
555
|
+
};
|
|
556
|
+
|
|
557
|
+
if (variantOptions.skip) {
|
|
558
|
+
variantResult.skipped = true;
|
|
559
|
+
variantResults.push(variantResult);
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const variantExtra = extras.variants?.[variant.id];
|
|
564
|
+
const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;
|
|
565
|
+
const variantDiffFilename =
|
|
566
|
+
variantExtra?.diffImageFilename ?? variantFilename;
|
|
567
|
+
|
|
568
|
+
if (this.filesystem.hasExpectedFile(variantFilename)) {
|
|
569
|
+
const { screenshotPath, comparisonResult, diffImagePath } =
|
|
570
|
+
await this.takeScreenshotWithComparison(
|
|
571
|
+
page,
|
|
572
|
+
variantFilename,
|
|
573
|
+
this.filesystem.readExpectedFile(variantFilename),
|
|
574
|
+
{
|
|
575
|
+
...variantOptions,
|
|
576
|
+
saveDiffImage: variantSaveDiff,
|
|
577
|
+
diffImageFilename: variantDiffFilename,
|
|
578
|
+
},
|
|
579
|
+
);
|
|
580
|
+
|
|
581
|
+
variantResult.filepath = screenshotPath;
|
|
582
|
+
variantResult.comparisonResult = comparisonResult;
|
|
583
|
+
variantResult.diffImagePath = diffImagePath;
|
|
584
|
+
} else {
|
|
585
|
+
variantResult.filepath = await this.takeScreenshot(
|
|
586
|
+
page,
|
|
587
|
+
variantFilename,
|
|
588
|
+
variantOptions,
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
variantResults.push(variantResult);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
return {
|
|
596
|
+
base: baseResult,
|
|
597
|
+
variants: variantResults,
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* Gets the exponential backoff delay
|
|
603
|
+
*/
|
|
604
|
+
getIncBackoffDelay(i: number, delay: number): number {
|
|
605
|
+
return i > 0 ? 500 * 2 ** (i - 1) + (delay || 0) : 0;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Get a page from the pool by index
|
|
610
|
+
*/
|
|
611
|
+
getPageFromPool(index: number): Page {
|
|
612
|
+
if (index < 0) {
|
|
613
|
+
throw new Error(`Page index ${index} must be non-negative`);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
if (index >= this.pages.length) {
|
|
617
|
+
throw new Error(
|
|
618
|
+
`Page index ${index} exceeds pool size ${this.pages.length}`,
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
if (!this.pages[index]) {
|
|
623
|
+
throw new Error(`Page index ${index} is undefined`);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
return this.pages[index];
|
|
321
627
|
}
|
|
322
628
|
}
|
|
323
629
|
|