@cappa/core 0.2.0 → 0.2.2
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/CHANGELOG.md +14 -0
- package/dist/index.d.mts +21 -2
- package/dist/index.d.ts +21 -2
- package/dist/index.js +123 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +123 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/filesystem.ts +1 -1
- package/src/index.ts +2 -0
- package/src/screenshot.ts +202 -42
- package/src/types.ts +8 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @cappa/core
|
|
2
2
|
|
|
3
|
+
## 0.2.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 6526418: fix: actual screenshot error
|
|
8
|
+
|
|
9
|
+
## 0.2.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 526857a: feat: add variant and play function support
|
|
14
|
+
- Updated dependencies [526857a]
|
|
15
|
+
- @cappa/logger@0.0.4
|
|
16
|
+
|
|
3
17
|
## 0.2.0
|
|
4
18
|
|
|
5
19
|
### Minor Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -54,7 +54,7 @@ declare class ScreenshotFileSystem {
|
|
|
54
54
|
* Ensure the parent directory of a file exists. If not, it creates it.
|
|
55
55
|
* @param filePath - The path to the file.
|
|
56
56
|
*/
|
|
57
|
-
|
|
57
|
+
ensureParentDir(filePath: string): void;
|
|
58
58
|
/**
|
|
59
59
|
* Remove a directory and all its contents.
|
|
60
60
|
* @param dir - The path to the directory.
|
|
@@ -206,7 +206,19 @@ declare class ScreenshotTool {
|
|
|
206
206
|
passed: boolean;
|
|
207
207
|
}>;
|
|
208
208
|
getVariantFilename(filename: string, variant: ScreenshotVariant): string;
|
|
209
|
+
/**
|
|
210
|
+
* Captures a single screenshot with the given options
|
|
211
|
+
*/
|
|
212
|
+
private captureSingleScreenshot;
|
|
209
213
|
capture(page: Page, filename: string, options: ScreenshotOptions, extras?: ScreenshotCaptureExtras): Promise<ScreenshotCaptureResult>;
|
|
214
|
+
/**
|
|
215
|
+
* Applies SVG and UI optimizations for screenshots
|
|
216
|
+
*/
|
|
217
|
+
private applyScreenshotOptimizations;
|
|
218
|
+
/**
|
|
219
|
+
* Captures screenshots for multiple variants, each with their own URL
|
|
220
|
+
*/
|
|
221
|
+
captureWithVariants(page: Page, baseFilename: string, baseUrl: string, baseOptions: ScreenshotSettings, variants: ScreenshotVariantWithUrl[], extras?: ScreenshotCaptureExtras): Promise<ScreenshotCaptureResult>;
|
|
210
222
|
/**
|
|
211
223
|
* Gets the exponential backoff delay
|
|
212
224
|
*/
|
|
@@ -341,6 +353,13 @@ type ScreenshotVariant = {
|
|
|
341
353
|
filename?: string;
|
|
342
354
|
options?: ScreenshotSettings;
|
|
343
355
|
};
|
|
356
|
+
type ScreenshotVariantWithUrl = {
|
|
357
|
+
id: string;
|
|
358
|
+
label?: string;
|
|
359
|
+
filename?: string;
|
|
360
|
+
url: string;
|
|
361
|
+
options?: ScreenshotSettings;
|
|
362
|
+
};
|
|
344
363
|
type ScreenshotOptions = ScreenshotSettings & {
|
|
345
364
|
variants?: ScreenshotVariant[];
|
|
346
365
|
};
|
|
@@ -360,5 +379,5 @@ interface Screenshot {
|
|
|
360
379
|
*/
|
|
361
380
|
declare function defineConfig(options: PossiblePromise<UserConfig | Array<UserConfig>> | (() => PossiblePromise<UserConfig | Array<UserConfig>>)): typeof options;
|
|
362
381
|
//#endregion
|
|
363
|
-
export { type Plugin, type PluginDef, type PluginFunction, type Screenshot, type ScreenshotCaptureDetails, type ScreenshotCaptureExtras, type ScreenshotCaptureResult, ScreenshotFileSystem, type ScreenshotOptions, type ScreenshotSettings, ScreenshotTool, type ScreenshotVariant, type ScreenshotVariantCaptureDetails, type UserConfig, type Viewport, defineConfig };
|
|
382
|
+
export { type Plugin, type PluginDef, type PluginFunction, type Screenshot, type ScreenshotCaptureDetails, type ScreenshotCaptureExtras, type ScreenshotCaptureResult, ScreenshotFileSystem, type ScreenshotOptions, type ScreenshotSettings, ScreenshotTool, type ScreenshotVariant, type ScreenshotVariantCaptureDetails, type ScreenshotVariantWithUrl, type UserConfig, type Viewport, defineConfig };
|
|
364
383
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -54,7 +54,7 @@ declare class ScreenshotFileSystem {
|
|
|
54
54
|
* Ensure the parent directory of a file exists. If not, it creates it.
|
|
55
55
|
* @param filePath - The path to the file.
|
|
56
56
|
*/
|
|
57
|
-
|
|
57
|
+
ensureParentDir(filePath: string): void;
|
|
58
58
|
/**
|
|
59
59
|
* Remove a directory and all its contents.
|
|
60
60
|
* @param dir - The path to the directory.
|
|
@@ -206,7 +206,19 @@ declare class ScreenshotTool {
|
|
|
206
206
|
passed: boolean;
|
|
207
207
|
}>;
|
|
208
208
|
getVariantFilename(filename: string, variant: ScreenshotVariant): string;
|
|
209
|
+
/**
|
|
210
|
+
* Captures a single screenshot with the given options
|
|
211
|
+
*/
|
|
212
|
+
private captureSingleScreenshot;
|
|
209
213
|
capture(page: Page, filename: string, options: ScreenshotOptions, extras?: ScreenshotCaptureExtras): Promise<ScreenshotCaptureResult>;
|
|
214
|
+
/**
|
|
215
|
+
* Applies SVG and UI optimizations for screenshots
|
|
216
|
+
*/
|
|
217
|
+
private applyScreenshotOptimizations;
|
|
218
|
+
/**
|
|
219
|
+
* Captures screenshots for multiple variants, each with their own URL
|
|
220
|
+
*/
|
|
221
|
+
captureWithVariants(page: Page, baseFilename: string, baseUrl: string, baseOptions: ScreenshotSettings, variants: ScreenshotVariantWithUrl[], extras?: ScreenshotCaptureExtras): Promise<ScreenshotCaptureResult>;
|
|
210
222
|
/**
|
|
211
223
|
* Gets the exponential backoff delay
|
|
212
224
|
*/
|
|
@@ -341,6 +353,13 @@ type ScreenshotVariant = {
|
|
|
341
353
|
filename?: string;
|
|
342
354
|
options?: ScreenshotSettings;
|
|
343
355
|
};
|
|
356
|
+
type ScreenshotVariantWithUrl = {
|
|
357
|
+
id: string;
|
|
358
|
+
label?: string;
|
|
359
|
+
filename?: string;
|
|
360
|
+
url: string;
|
|
361
|
+
options?: ScreenshotSettings;
|
|
362
|
+
};
|
|
344
363
|
type ScreenshotOptions = ScreenshotSettings & {
|
|
345
364
|
variants?: ScreenshotVariant[];
|
|
346
365
|
};
|
|
@@ -360,5 +379,5 @@ interface Screenshot {
|
|
|
360
379
|
*/
|
|
361
380
|
declare function defineConfig(options: PossiblePromise<UserConfig | Array<UserConfig>> | (() => PossiblePromise<UserConfig | Array<UserConfig>>)): typeof options;
|
|
362
381
|
//#endregion
|
|
363
|
-
export { type Plugin, type PluginDef, type PluginFunction, type Screenshot, type ScreenshotCaptureDetails, type ScreenshotCaptureExtras, type ScreenshotCaptureResult, ScreenshotFileSystem, type ScreenshotOptions, type ScreenshotSettings, ScreenshotTool, type ScreenshotVariant, type ScreenshotVariantCaptureDetails, type UserConfig, type Viewport, defineConfig };
|
|
382
|
+
export { type Plugin, type PluginDef, type PluginFunction, type Screenshot, type ScreenshotCaptureDetails, type ScreenshotCaptureExtras, type ScreenshotCaptureResult, ScreenshotFileSystem, type ScreenshotOptions, type ScreenshotSettings, ScreenshotTool, type ScreenshotVariant, type ScreenshotVariantCaptureDetails, type ScreenshotVariantWithUrl, type UserConfig, type Viewport, defineConfig };
|
|
364
383
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -389,6 +389,7 @@ var ScreenshotTool = class {
|
|
|
389
389
|
if (options.skip) return;
|
|
390
390
|
try {
|
|
391
391
|
const filepath = this.filesystem.getActualFilePath(filename);
|
|
392
|
+
this.filesystem.ensureParentDir(filepath);
|
|
392
393
|
const screenshotOptions = {
|
|
393
394
|
path: filepath,
|
|
394
395
|
fullPage: options.fullPage,
|
|
@@ -537,6 +538,30 @@ var ScreenshotTool = class {
|
|
|
537
538
|
const variantName = `${parsed.name}--${suffix}${ext}`;
|
|
538
539
|
return parsed.dir ? node_path.default.join(parsed.dir, variantName) : variantName;
|
|
539
540
|
}
|
|
541
|
+
/**
|
|
542
|
+
* Captures a single screenshot with the given options
|
|
543
|
+
*/
|
|
544
|
+
async captureSingleScreenshot(page, filename, options, extras = {}) {
|
|
545
|
+
const result = { filename };
|
|
546
|
+
if (options.skip) {
|
|
547
|
+
result.skipped = true;
|
|
548
|
+
return result;
|
|
549
|
+
}
|
|
550
|
+
const saveDiff = extras.saveDiffImage ?? false;
|
|
551
|
+
const diffFilename = extras.diffImageFilename ?? filename;
|
|
552
|
+
if (!this.filesystem.hasExpectedFile(filename)) {
|
|
553
|
+
this.logger.debug(`Expected image not found for ${filename}, taking screenshot`);
|
|
554
|
+
const { screenshotPath, comparisonResult, diffImagePath } = await this.takeScreenshotWithComparison(page, filename, this.filesystem.readExpectedFile(filename), {
|
|
555
|
+
...options,
|
|
556
|
+
saveDiffImage: saveDiff,
|
|
557
|
+
diffImageFilename: diffFilename
|
|
558
|
+
});
|
|
559
|
+
result.filepath = screenshotPath;
|
|
560
|
+
result.comparisonResult = comparisonResult;
|
|
561
|
+
result.diffImagePath = diffImagePath;
|
|
562
|
+
} else result.filepath = await this.takeScreenshot(page, filename, options);
|
|
563
|
+
return result;
|
|
564
|
+
}
|
|
540
565
|
async capture(page, filename, options, extras = {}) {
|
|
541
566
|
if (!this.browser) throw new Error("Browser not initialized");
|
|
542
567
|
const { variants = [],...baseOptions } = options;
|
|
@@ -556,18 +581,96 @@ var ScreenshotTool = class {
|
|
|
556
581
|
skipped: true
|
|
557
582
|
}))
|
|
558
583
|
};
|
|
559
|
-
|
|
560
|
-
|
|
584
|
+
const baseCaptureResult = await this.captureSingleScreenshot(page, filename, baseOptions, {
|
|
585
|
+
saveDiffImage: baseSaveDiff,
|
|
586
|
+
diffImageFilename: baseDiffFilename
|
|
587
|
+
});
|
|
588
|
+
baseResult.filepath = baseCaptureResult.filepath;
|
|
589
|
+
baseResult.comparisonResult = baseCaptureResult.comparisonResult;
|
|
590
|
+
baseResult.diffImagePath = baseCaptureResult.diffImagePath;
|
|
591
|
+
baseResult.skipped = baseCaptureResult.skipped;
|
|
592
|
+
for (const variant of variants) {
|
|
593
|
+
const variantFilename = this.getVariantFilename(filename, variant);
|
|
594
|
+
const variantOptions = {
|
|
561
595
|
...baseOptions,
|
|
562
|
-
|
|
563
|
-
|
|
596
|
+
...variant.options
|
|
597
|
+
};
|
|
598
|
+
const variantResult = {
|
|
599
|
+
id: variant.id,
|
|
600
|
+
label: variant.label,
|
|
601
|
+
filename: variantFilename
|
|
602
|
+
};
|
|
603
|
+
if (variantOptions.skip) {
|
|
604
|
+
variantResult.skipped = true;
|
|
605
|
+
variantResults.push(variantResult);
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
const variantExtra = extras.variants?.[variant.id];
|
|
609
|
+
const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;
|
|
610
|
+
const variantDiffFilename = variantExtra?.diffImageFilename ?? variantFilename;
|
|
611
|
+
const variantCaptureResult = await this.captureSingleScreenshot(page, variantFilename, variantOptions, {
|
|
612
|
+
saveDiffImage: variantSaveDiff,
|
|
613
|
+
diffImageFilename: variantDiffFilename
|
|
564
614
|
});
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
615
|
+
variantResult.filepath = variantCaptureResult.filepath;
|
|
616
|
+
variantResult.comparisonResult = variantCaptureResult.comparisonResult;
|
|
617
|
+
variantResult.diffImagePath = variantCaptureResult.diffImagePath;
|
|
618
|
+
variantResult.skipped = variantCaptureResult.skipped;
|
|
619
|
+
variantResults.push(variantResult);
|
|
620
|
+
}
|
|
621
|
+
return {
|
|
622
|
+
base: baseResult,
|
|
623
|
+
variants: variantResults
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Applies SVG and UI optimizations for screenshots
|
|
628
|
+
*/
|
|
629
|
+
async applyScreenshotOptimizations(page) {
|
|
630
|
+
await page.addStyleTag({ content: `
|
|
631
|
+
*, *::before, *::after {
|
|
632
|
+
-moz-animation: none !important;
|
|
633
|
+
-moz-transition: none !important;
|
|
634
|
+
transition: none !important;
|
|
635
|
+
animation: none !important;
|
|
636
|
+
}
|
|
637
|
+
*, input, textarea { caret-color: transparent !important }
|
|
638
|
+
*:focus { outline: none !important }
|
|
639
|
+
*:focus-visible { outline: none !important }
|
|
640
|
+
*:focus-within { outline: none !important }
|
|
641
|
+
* { text-rendering: optimizeLegibility !important }
|
|
642
|
+
::-webkit-scrollbar { display: none !important }
|
|
643
|
+
html { scroll-behavior: auto !important }
|
|
644
|
+
svg, svg * {
|
|
645
|
+
shape-rendering: crispEdges !important;
|
|
646
|
+
text-rendering: geometricPrecision !important;
|
|
647
|
+
vector-effect: non-scaling-stroke !important;
|
|
648
|
+
}
|
|
649
|
+
img { image-rendering: crisp-edges !important }
|
|
650
|
+
` });
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* Captures screenshots for multiple variants, each with their own URL
|
|
654
|
+
*/
|
|
655
|
+
async captureWithVariants(page, baseFilename, baseUrl, baseOptions, variants, extras = {}) {
|
|
656
|
+
if (!this.browser) throw new Error("Browser not initialized");
|
|
657
|
+
const baseResult = { filename: baseFilename };
|
|
658
|
+
const variantResults = [];
|
|
659
|
+
const baseSaveDiff = extras.saveDiffImage ?? false;
|
|
660
|
+
const baseDiffFilename = extras.diffImageFilename ?? baseFilename;
|
|
661
|
+
(0, __cappa_logger.getLogger)().debug(`Going to base URL: ${baseUrl}`);
|
|
662
|
+
await page.goto(baseUrl);
|
|
663
|
+
await this.applyScreenshotOptimizations(page);
|
|
664
|
+
const baseCaptureResult = await this.captureSingleScreenshot(page, baseFilename, baseOptions, {
|
|
665
|
+
saveDiffImage: baseSaveDiff,
|
|
666
|
+
diffImageFilename: baseDiffFilename
|
|
667
|
+
});
|
|
668
|
+
baseResult.filepath = baseCaptureResult.filepath;
|
|
669
|
+
baseResult.comparisonResult = baseCaptureResult.comparisonResult;
|
|
670
|
+
baseResult.diffImagePath = baseCaptureResult.diffImagePath;
|
|
671
|
+
baseResult.skipped = baseCaptureResult.skipped;
|
|
569
672
|
for (const variant of variants) {
|
|
570
|
-
const variantFilename = this.getVariantFilename(
|
|
673
|
+
const variantFilename = variant.filename || this.getVariantFilename(baseFilename, variant);
|
|
571
674
|
const variantOptions = {
|
|
572
675
|
...baseOptions,
|
|
573
676
|
...variant.options
|
|
@@ -582,19 +685,20 @@ var ScreenshotTool = class {
|
|
|
582
685
|
variantResults.push(variantResult);
|
|
583
686
|
continue;
|
|
584
687
|
}
|
|
688
|
+
(0, __cappa_logger.getLogger)().debug(`Going to variant URL: ${variant.url}`);
|
|
689
|
+
await page.goto(variant.url);
|
|
690
|
+
await this.applyScreenshotOptimizations(page);
|
|
585
691
|
const variantExtra = extras.variants?.[variant.id];
|
|
586
692
|
const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;
|
|
587
693
|
const variantDiffFilename = variantExtra?.diffImageFilename ?? variantFilename;
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
variantResult.diffImagePath = diffImagePath;
|
|
597
|
-
} else variantResult.filepath = await this.takeScreenshot(page, variantFilename, variantOptions);
|
|
694
|
+
const variantCaptureResult = await this.captureSingleScreenshot(page, variantFilename, variantOptions, {
|
|
695
|
+
saveDiffImage: variantSaveDiff,
|
|
696
|
+
diffImageFilename: variantDiffFilename
|
|
697
|
+
});
|
|
698
|
+
variantResult.filepath = variantCaptureResult.filepath;
|
|
699
|
+
variantResult.comparisonResult = variantCaptureResult.comparisonResult;
|
|
700
|
+
variantResult.diffImagePath = variantCaptureResult.diffImagePath;
|
|
701
|
+
variantResult.skipped = variantCaptureResult.skipped;
|
|
598
702
|
variantResults.push(variantResult);
|
|
599
703
|
}
|
|
600
704
|
return {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["path","fs","PNG","fs","defaultDiffConfig: DiffConfig","screenshotSettings: ScreenshotSettings","diffImagePath: string | undefined","path","baseResult: ScreenshotCaptureDetails","variantResults: ScreenshotVariantCaptureDetails[]","variantOptions: ScreenshotSettings","variantResult: ScreenshotVariantCaptureDetails"],"sources":["../src/config.ts","../src/filesystem.ts","../src/compare.ts","../src/screenshot.ts"],"sourcesContent":["import type { PossiblePromise, UserConfig } from \"./types\";\n\n/**\n * Type helper to make it easier to use cappa.config.ts, or a function that returns it. The function receives a ConfigEnv object.\n */\nexport function defineConfig(\n options:\n | PossiblePromise<UserConfig | Array<UserConfig>>\n | (() => PossiblePromise<UserConfig | Array<UserConfig>>),\n): typeof options {\n return options;\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\n\n/**\n * Class for interacting with the local file system to store the screenshots.\n */\nexport class ScreenshotFileSystem {\n private readonly actualDir: string;\n private readonly expectedDir: string;\n private readonly diffDir: string;\n\n constructor(outputDir: string) {\n this.actualDir = path.resolve(outputDir, \"actual\");\n this.expectedDir = path.resolve(outputDir, \"expected\");\n this.diffDir = path.resolve(outputDir, \"diff\");\n\n this.ensureParentDir(this.actualDir);\n this.ensureParentDir(this.expectedDir);\n this.ensureParentDir(this.diffDir);\n }\n\n clearActual() {\n this.removeDir(this.actualDir);\n }\n\n clearDiff() {\n this.removeDir(this.diffDir);\n }\n\n clearActualAndDiff() {\n this.clearActual();\n this.clearDiff();\n }\n\n /**\n * Approve a screenshot based on its actual file path.\n * @param actualFilePath - The path to the actual screenshot file.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n approveFromActualPath(actualFilePath: string) {\n const actualAbsolute = this.resolveActualPath(actualFilePath);\n const relativePath = path.relative(this.actualDir, actualAbsolute);\n\n if (relativePath.startsWith(\"..\")) {\n throw new Error(\n `Cannot approve screenshot outside of actual directory: ${actualFilePath}`,\n );\n }\n\n return this.approveRelative(relativePath);\n }\n\n /**\n * Approve a screenshot based on its name.\n * @param name - The name of the screenshot. Can include '.png' extension.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n approveByName(name: string) {\n const relativeWithExtension = name.endsWith(\".png\") ? name : `${name}.png`;\n\n return this.approveRelative(relativeWithExtension);\n }\n\n /**\n * Approve a screenshot based on a relative path.\n * @param relativePath - The relative path to the screenshot.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n private approveRelative(relativePath: string) {\n const actualPath = path.resolve(this.actualDir, relativePath);\n const expectedPath = path.resolve(this.expectedDir, relativePath);\n const diffPath = path.resolve(this.diffDir, relativePath);\n\n this.ensureParentDir(expectedPath);\n\n fs.copyFileSync(actualPath, expectedPath);\n\n if (fs.existsSync(diffPath)) {\n fs.unlinkSync(diffPath);\n }\n\n return {\n actualPath,\n expectedPath,\n diffPath,\n };\n }\n\n /**\n * Ensure the parent directory of a file exists. If not, it creates it.\n * @param filePath - The path to the file.\n */\n private ensureParentDir(filePath: string) {\n const dir = path.dirname(filePath);\n\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n }\n\n /**\n * Remove a directory and all its contents.\n * @param dir - The path to the directory.\n */\n private removeDir(dir: string) {\n if (fs.existsSync(dir)) {\n fs.rmSync(dir, { recursive: true, force: true });\n }\n }\n\n /**\n * Get the actual directory path\n */\n getActualDir(): string {\n return this.actualDir;\n }\n\n /**\n * Get the expected directory path\n */\n getExpectedDir(): string {\n return this.expectedDir;\n }\n\n /**\n * Get the diff directory path\n */\n getDiffDir(): string {\n return this.diffDir;\n }\n\n /**\n * Get the path to an actual screenshot file\n */\n getActualFilePath(filename: string): string {\n return path.resolve(this.actualDir, filename);\n }\n\n /**\n * Get the path to an expected screenshot file\n */\n getExpectedFilePath(filename: string): string {\n return path.resolve(this.expectedDir, filename);\n }\n\n /**\n * Get the path to a diff screenshot file\n */\n getDiffFilePath(filename: string): string {\n return path.resolve(this.diffDir, filename);\n }\n\n /**\n * Write a file to the actual directory\n */\n writeActualFile(filename: string, data: Buffer): void {\n const filepath = this.getActualFilePath(filename);\n this.ensureParentDir(filepath);\n fs.writeFileSync(filepath, data);\n }\n\n /**\n * Write a file to the diff directory\n */\n writeDiffFile(filename: string, data: Buffer): void {\n const filepath = this.getDiffFilePath(filename);\n this.ensureParentDir(filepath);\n fs.writeFileSync(filepath, data);\n }\n\n /**\n * Check if an expected file exists\n */\n hasExpectedFile(filename: string): boolean {\n const expectedPath = this.getExpectedFilePath(filename);\n return fs.existsSync(expectedPath);\n }\n\n /**\n * Read an expected file\n */\n readExpectedFile(filename: string): Buffer {\n const expectedPath = this.getExpectedFilePath(filename);\n if (!fs.existsSync(expectedPath)) {\n throw new Error(`Expected image not found: ${expectedPath}`);\n }\n return fs.readFileSync(expectedPath);\n }\n\n /**\n * Resolve the actual path of a screenshot file.\n * @param filePath - The path to the screenshot file.\n * @returns The actual path of the screenshot file.\n */\n private resolveActualPath(filePath: string) {\n if (path.isAbsolute(filePath)) {\n return filePath;\n }\n\n return path.resolve(this.actualDir, filePath);\n }\n}\n","import fs from \"node:fs\";\nimport blazediff from \"@blazediff/core\";\nimport type { BlazeDiffOptions } from \"@blazediff/types\";\nimport { getLogger } from \"@cappa/logger\";\nimport { PNG } from \"pngjs\";\nimport type { DiffConfig } from \"./types\";\n\nexport type CompareOptions = BlazeDiffOptions;\n\nexport interface CompareResult {\n numDiffPixels: number;\n totalPixels: number;\n percentDifference: number;\n diffBuffer?: Buffer; // PNG buffer of the diff image\n passed: boolean; // Whether the comparison passed based on threshold\n error?: string; // Error message if the comparison failed\n differentSizes: boolean; // Whether the images have different sizes\n}\n\nconst compare = (\n image1: Uint8Array | Uint8ClampedArray,\n image2: Uint8Array | Uint8ClampedArray,\n diff: Uint8Array | Uint8ClampedArray | undefined,\n width: number,\n height: number,\n options?: BlazeDiffOptions,\n) => {\n return blazediff(image1, image2, diff, width, height, options);\n};\n\n/**\n * Compare two PNG images and return the difference\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param withDiff - Whether to create a diff image\n * @param options - Comparison options\n * @returns Comparison result\n */\nexport async function compareImages(\n image1: string | Buffer,\n image2: string | Buffer,\n withDiff: boolean = false,\n options: DiffConfig = {},\n): Promise<CompareResult> {\n const png1 = await loadPNG(image1);\n const png2 = await loadPNG(image2);\n\n const { width, height } = png1;\n\n if (width !== png2.width || height !== png2.height) {\n return {\n numDiffPixels: 0,\n totalPixels: 0,\n percentDifference: 0,\n passed: false,\n differentSizes: true,\n };\n }\n\n const diff = withDiff ? new PNG({ width, height }) : undefined;\n\n try {\n const numDiffPixels = compare(\n png1.data,\n png2.data,\n diff?.data,\n width,\n height,\n options,\n );\n\n const totalPixels = width * height;\n const percentDifference = (numDiffPixels / totalPixels) * 100;\n\n // Convert diff image to buffer\n const diffBuffer = diff ? PNG.sync.write(diff) : undefined;\n\n return {\n numDiffPixels,\n totalPixels,\n percentDifference,\n diffBuffer,\n passed: isPassed(percentDifference, numDiffPixels, options),\n differentSizes: false,\n };\n } catch (error) {\n getLogger().error((error as Error).message || \"Unknown error\");\n\n return {\n numDiffPixels: 0,\n totalPixels: 0,\n percentDifference: 0,\n passed: false,\n differentSizes: false,\n error: (error as Error).message || \"Unknown error\",\n };\n }\n}\n\n/**\n * Load a PNG image from file path or Buffer\n */\nasync function loadPNG(source: string | Buffer): Promise<PNG> {\n if (Buffer.isBuffer(source)) {\n return PNG.sync.read(source);\n } else {\n const buffer = fs.readFileSync(source);\n return PNG.sync.read(buffer);\n }\n}\n\n/**\n * Save a comparison result diff image to file\n * @param result - Comparison result containing diffBuffer\n * @param outputPath - Path to save the diff image\n */\nexport function saveDiffImage(result: CompareResult, outputPath: string): void {\n if (!result.diffBuffer) {\n throw new Error(\"No diff buffer available in comparison result\");\n }\n\n fs.writeFileSync(outputPath, result.diffBuffer);\n}\n\n/**\n * Quick comparison function that returns only pass/fail\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param options - Comparison options\n * @returns Whether the images match\n */\nexport async function imagesMatch(\n image1: string | Buffer,\n image2: string | Buffer,\n options: DiffConfig = {},\n): Promise<boolean> {\n const result = await compareImages(image1, image2, false, options);\n\n return result.passed;\n}\n\n/**\n * Check if the comparison passed based on the options\n * @param percentDifference - Percentage difference between the images\n * @param numDiffPixels - Number of different pixels\n * @param options - Comparison options\n * @returns Whether the comparison passed\n */\nconst isPassed = (\n percentDifference: number,\n numDiffPixels: number,\n options: DiffConfig,\n) => {\n if (options.maxDiffPercentage && options.maxDiffPixels) {\n return (\n percentDifference <= options.maxDiffPercentage &&\n numDiffPixels <= options.maxDiffPixels\n );\n }\n\n if (options.maxDiffPercentage) {\n return percentDifference <= options.maxDiffPercentage;\n }\n\n if (options.maxDiffPixels) {\n return numDiffPixels <= options.maxDiffPixels;\n }\n\n // fallback to 0 pixels\n return numDiffPixels === 0;\n};\n\n/**\n * Create different size png image with all pixels red\n * @returns\n */\nexport function createDiffSizePngImage(width: number, height: number): Buffer {\n const png = new PNG({ width, height });\n\n for (let y = 0; y < height; y++) {\n for (let x = 0; x < width; x++) {\n const idx = (width * y + x) << 2;\n png.data[idx] = 255; // red\n png.data[idx + 1] = 0; // green\n png.data[idx + 2] = 0; // blue\n png.data[idx + 3] = 255; // alpha\n }\n }\n\n return PNG.sync.write(png);\n}\n","import path from \"node:path\";\nimport { getLogger, type Logger } from \"@cappa/logger\";\nimport {\n type Browser,\n type BrowserContext,\n chromium,\n firefox,\n type Page,\n type PageScreenshotOptions,\n webkit,\n} from \"playwright-core\";\nimport {\n type CompareResult,\n compareImages,\n createDiffSizePngImage,\n} from \"./compare\";\nimport { ScreenshotFileSystem } from \"./filesystem\";\nimport type {\n DiffConfig,\n ScreenshotOptions,\n ScreenshotSettings,\n ScreenshotVariant,\n} from \"./types\";\n\nconst defaultDiffConfig: DiffConfig = {\n threshold: 0.1,\n includeAA: false,\n fastBufferCheck: true,\n maxDiffPixels: 0,\n maxDiffPercentage: 0,\n};\n\nexport interface ScreenshotCaptureDetails {\n filename: string;\n filepath?: string;\n comparisonResult?: CompareResult;\n diffImagePath?: string;\n skipped?: boolean;\n}\n\nexport interface ScreenshotVariantCaptureDetails\n extends ScreenshotCaptureDetails {\n id: string;\n label?: string;\n}\n\nexport interface ScreenshotCaptureResult {\n base: ScreenshotCaptureDetails;\n variants: ScreenshotVariantCaptureDetails[];\n}\n\nexport interface ScreenshotCaptureExtras {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n variants?: Record<\n string,\n {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n }\n >;\n}\n\nclass ScreenshotTool {\n browserType: \"chromium\" | \"firefox\" | \"webkit\";\n headless: boolean;\n viewport: { width: number; height: number };\n outputDir: string;\n browser: Browser | null = null;\n context: BrowserContext | null = null;\n page: Page | null = null;\n concurrency: number;\n contexts: BrowserContext[] = [];\n pages: Page[] = [];\n diff: DiffConfig;\n logger: Logger;\n retries: number;\n filesystem: ScreenshotFileSystem;\n\n constructor(options: {\n browserType?: \"chromium\" | \"firefox\" | \"webkit\";\n headless?: boolean;\n viewport?: { width: number; height: number };\n outputDir?: string;\n diff?: DiffConfig;\n retries?: number;\n concurrency?: number;\n }) {\n this.browserType = options.browserType || \"chromium\";\n this.headless = options.headless !== false; // Default to headless\n this.viewport = options.viewport || { width: 1920, height: 1080 };\n this.outputDir = options.outputDir || \"./screenshots\";\n this.diff = { ...defaultDiffConfig, ...options.diff };\n this.concurrency = Math.max(1, options.concurrency || 1);\n\n this.logger = getLogger();\n this.retries = options.retries || 2;\n this.filesystem = new ScreenshotFileSystem(this.outputDir);\n }\n\n /**\n * Initialize playwright and page\n */\n async init() {\n // Launch browser based on type\n const browserMap = {\n chromium,\n firefox,\n webkit,\n };\n\n const browserClass = browserMap[this.browserType];\n if (!browserClass) {\n throw new Error(`Unsupported browser type: ${this.browserType}`);\n }\n\n this.browser = await browserClass.launch({\n headless: this.headless,\n });\n\n const defaultUserAgent = (await this.browser.newContext())\n .newPage()\n .then((page) => page.evaluate(() => navigator.userAgent));\n\n // Create N contexts and pages\n this.contexts = [];\n this.pages = [];\n\n for (let i = 0; i < this.concurrency; i++) {\n const context = await this.browser.newContext({\n reducedMotion: \"reduce\",\n deviceScaleFactor: 2,\n userAgent: `${await defaultUserAgent} CappaStorybook`,\n viewport: this.viewport,\n });\n const page = await context.newPage();\n\n this.contexts.push(context);\n this.pages.push(page);\n }\n\n // Backward compatibility\n this.context = this.contexts[0] ?? null;\n this.page = this.pages[0] ?? null;\n }\n\n /**\n * Closes browser\n * If not closed, the process will not exit\n */\n async close() {\n for (const context of this.contexts) {\n await context.close();\n }\n if (this.browser) {\n await this.browser.close();\n }\n }\n\n /**\n * Navigates to a URL with \"domcontentloaded\" waitUntil\n */\n async goTo(page: Page, url: string) {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n await page.goto(url, { waitUntil: \"domcontentloaded\" });\n }\n\n /**\n * Takes a screenshot of the page\n */\n async takeScreenshot(\n page: Page,\n filename: string,\n options: ScreenshotSettings,\n ) {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n if (options.skip) {\n return;\n }\n\n try {\n // Generate filename - save to actual directory\n const filepath = this.filesystem.getActualFilePath(filename);\n\n // Take screenshot\n const screenshotOptions = {\n path: filepath,\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n animations: \"disabled\",\n caret: \"hide\",\n } satisfies PageScreenshotOptions;\n\n await this.withViewport(page, options.viewport, async () => {\n if (options.delay) {\n await page.waitForTimeout(options.delay);\n }\n\n await page.screenshot(screenshotOptions);\n });\n\n return filepath;\n } catch (error) {\n this.logger.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n /**\n * Sets the viewport size and executes the action, ensuring the original viewport is restored\n * even if the action throws an error.\n */\n private async withViewport<T>(\n page: Page,\n viewport: ScreenshotSettings[\"viewport\"],\n action: () => Promise<T>,\n ): Promise<T> {\n if (!viewport) {\n return action();\n }\n\n const previousViewport = page.viewportSize();\n\n // Check if the new viewport is the same as the current one\n if (\n previousViewport &&\n previousViewport.width === viewport.width &&\n previousViewport.height === viewport.height\n ) {\n return action();\n }\n\n await page.setViewportSize(viewport);\n\n try {\n return await action();\n } finally {\n // Always restore the original viewport, even if the action throws an error\n if (previousViewport) {\n await page.setViewportSize(previousViewport);\n }\n }\n }\n\n /**\n * Takes a screenshot and returns the buffer (for comparison purposes)\n */\n async takeScreenshotBuffer(\n page: Page,\n options: ScreenshotSettings,\n ): Promise<Buffer> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n if (options.skip) {\n throw new Error(\"Screenshot skipped\");\n }\n\n try {\n // Take screenshot without saving to file\n const screenshotOptions = {\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n animations: \"disabled\",\n caret: \"hide\",\n } satisfies PageScreenshotOptions;\n\n const buffer = await this.withViewport(\n page,\n options.viewport,\n async () => {\n if (options.delay) {\n await page.waitForTimeout(options.delay);\n }\n\n return page.screenshot(screenshotOptions);\n },\n );\n\n return buffer;\n } catch (error) {\n this.logger.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n async takeScreenshotWithComparison(\n page: Page,\n filename: string,\n referenceImage: Buffer,\n options: ScreenshotSettings & {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n },\n ): Promise<{\n screenshotPath: string;\n comparisonResult: CompareResult;\n diffImagePath?: string;\n }> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n try {\n // Generate filename and take screenshot - save to actual directory\n const filepath = this.filesystem.getActualFilePath(filename);\n\n const screenshotSettings: ScreenshotSettings = {\n fullPage: options.fullPage,\n mask: options.mask,\n omitBackground: options.omitBackground,\n delay: options.delay,\n viewport: options.viewport,\n };\n\n const retryScreenshot = await this.retryScreenshot(\n page,\n referenceImage,\n screenshotSettings,\n );\n\n if (!retryScreenshot.screenshotPath) {\n throw new Error(\"Screenshot buffer is undefined\");\n }\n\n // Save screenshot to actual directory\n this.filesystem.writeActualFile(filename, retryScreenshot.screenshotPath);\n this.logger.success(`Screenshot saved: ${filepath}`);\n\n if (retryScreenshot.passed) {\n this.logger.success(`Screenshot passed visual comparison`);\n } else {\n this.logger.error(`Screenshot failed visual comparison`);\n }\n\n let diffImagePath: string | undefined;\n\n // Save diff image if requested and there are differences\n if (\n options.saveDiffImage &&\n retryScreenshot.comparisonResult.diffBuffer &&\n !retryScreenshot.passed\n ) {\n const diffFilename = options.diffImageFilename || filename;\n // Save to diff directory\n diffImagePath = this.filesystem.getDiffFilePath(diffFilename);\n\n this.filesystem.writeDiffFile(\n diffFilename,\n retryScreenshot.comparisonResult.diffBuffer,\n );\n\n this.logger.debug(`Diff image saved: ${diffImagePath}`);\n }\n\n // different sizes diff image\n if (retryScreenshot.comparisonResult.differentSizes) {\n this.logger.warn(`Screenshot has different sizes than reference image`);\n\n const diffFilename = options.diffImageFilename || filename;\n diffImagePath = this.filesystem.getDiffFilePath(diffFilename);\n const diffBuffer = createDiffSizePngImage(200, 200);\n\n this.filesystem.writeDiffFile(diffFilename, diffBuffer);\n\n this.logger.debug(`Diff Size image saved: ${diffImagePath}`);\n }\n\n return {\n screenshotPath: filepath,\n comparisonResult: retryScreenshot.comparisonResult,\n diffImagePath,\n };\n } catch (error) {\n this.logger.error(\n `Error taking screenshot with comparison of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n async retryScreenshot(\n page: Page,\n referenceImage: Buffer,\n options: ScreenshotSettings,\n ) {\n for (let i = 0; i < this.retries; i++) {\n try {\n const screenshotBuffer = await this.takeScreenshotBuffer(page, {\n ...options,\n delay: this.getIncBackoffDelay(i, options.delay || 0),\n });\n\n if (!screenshotBuffer) {\n throw new Error(\"Screenshot buffer is undefined\");\n }\n\n const comparisonResult = await compareImages(\n screenshotBuffer,\n referenceImage,\n true,\n this.diff,\n );\n\n // bail early if the images are different sizes\n if (comparisonResult.differentSizes) {\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: false,\n };\n }\n\n // last retry\n if (i === this.retries - 1 && !comparisonResult.passed) {\n this.logger.error(\n `Failed to match screenshot after ${this.retries} retries`,\n );\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: false,\n };\n }\n\n if (comparisonResult.passed) {\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: true,\n };\n } else {\n this.logger.warn(\n `Comparison did not match. Retrying in ${this.getIncBackoffDelay(i + 1, options.delay || 0)}ms... \\nComparison result: ${comparisonResult.numDiffPixels} pixels different (${comparisonResult.percentDifference.toFixed(2)}%)`,\n );\n }\n } catch (error) {\n this.logger.error(\n `Error taking screenshot after ${i + 1} retries:`,\n (error as Error).message,\n );\n }\n }\n\n return {\n screenshotPath: null,\n comparisonResult: null,\n passed: false,\n };\n }\n\n getVariantFilename(filename: string, variant: ScreenshotVariant): string {\n if (variant.filename) {\n return variant.filename;\n }\n\n const parsed = path.parse(filename);\n const sanitizedId = variant.id\n .trim()\n .replace(/[^a-zA-Z0-9_-]+/g, \"-\")\n .replace(/-+/g, \"-\")\n .replace(/^-|-$/g, \"\");\n const suffix = sanitizedId || \"variant\";\n const ext = parsed.ext || \".png\";\n const variantName = `${parsed.name}--${suffix}${ext}`;\n\n return parsed.dir ? path.join(parsed.dir, variantName) : variantName;\n }\n\n async capture(\n page: Page,\n filename: string,\n options: ScreenshotOptions,\n extras: ScreenshotCaptureExtras = {},\n ): Promise<ScreenshotCaptureResult> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n const { variants = [], ...baseOptions } = options;\n const baseResult: ScreenshotCaptureDetails = { filename };\n const variantResults: ScreenshotVariantCaptureDetails[] = [];\n\n const baseSaveDiff = extras.saveDiffImage ?? false;\n const baseDiffFilename = extras.diffImageFilename ?? filename;\n\n if (baseOptions.skip) {\n return {\n base: { ...baseResult, skipped: true },\n variants: variants.map((variant) => ({\n id: variant.id,\n label: variant.label,\n filename: this.getVariantFilename(filename, variant),\n skipped: true,\n })),\n };\n }\n\n if (this.filesystem.hasExpectedFile(filename)) {\n const { screenshotPath, comparisonResult, diffImagePath } =\n await this.takeScreenshotWithComparison(\n page,\n filename,\n this.filesystem.readExpectedFile(filename),\n {\n ...baseOptions,\n saveDiffImage: baseSaveDiff,\n diffImageFilename: baseDiffFilename,\n },\n );\n\n baseResult.filepath = screenshotPath;\n baseResult.comparisonResult = comparisonResult;\n baseResult.diffImagePath = diffImagePath;\n } else {\n baseResult.filepath = await this.takeScreenshot(\n page,\n filename,\n baseOptions,\n );\n }\n\n for (const variant of variants) {\n const variantFilename = this.getVariantFilename(filename, variant);\n const variantOptions: ScreenshotSettings = {\n ...baseOptions,\n ...variant.options,\n };\n\n const variantResult: ScreenshotVariantCaptureDetails = {\n id: variant.id,\n label: variant.label,\n filename: variantFilename,\n };\n\n if (variantOptions.skip) {\n variantResult.skipped = true;\n variantResults.push(variantResult);\n continue;\n }\n\n const variantExtra = extras.variants?.[variant.id];\n const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;\n const variantDiffFilename =\n variantExtra?.diffImageFilename ?? variantFilename;\n\n if (this.filesystem.hasExpectedFile(variantFilename)) {\n const { screenshotPath, comparisonResult, diffImagePath } =\n await this.takeScreenshotWithComparison(\n page,\n variantFilename,\n this.filesystem.readExpectedFile(variantFilename),\n {\n ...variantOptions,\n saveDiffImage: variantSaveDiff,\n diffImageFilename: variantDiffFilename,\n },\n );\n\n variantResult.filepath = screenshotPath;\n variantResult.comparisonResult = comparisonResult;\n variantResult.diffImagePath = diffImagePath;\n } else {\n variantResult.filepath = await this.takeScreenshot(\n page,\n variantFilename,\n variantOptions,\n );\n }\n\n variantResults.push(variantResult);\n }\n\n return {\n base: baseResult,\n variants: variantResults,\n };\n }\n\n /**\n * Gets the exponential backoff delay\n */\n getIncBackoffDelay(i: number, delay: number): number {\n return i > 0 ? 500 * 2 ** (i - 1) + (delay || 0) : 0;\n }\n\n /**\n * Get a page from the pool by index\n */\n getPageFromPool(index: number): Page {\n if (index < 0) {\n throw new Error(`Page index ${index} must be non-negative`);\n }\n\n if (index >= this.pages.length) {\n throw new Error(\n `Page index ${index} exceeds pool size ${this.pages.length}`,\n );\n }\n\n if (!this.pages[index]) {\n throw new Error(`Page index ${index} is undefined`);\n }\n\n return this.pages[index];\n }\n}\n\nexport default ScreenshotTool;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,SAAgB,aACd,SAGgB;AAChB,QAAO;;;;;;;;ACJT,IAAa,uBAAb,MAAkC;CAChC,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,WAAmB;AAC7B,OAAK,YAAYA,kBAAK,QAAQ,WAAW,SAAS;AAClD,OAAK,cAAcA,kBAAK,QAAQ,WAAW,WAAW;AACtD,OAAK,UAAUA,kBAAK,QAAQ,WAAW,OAAO;AAE9C,OAAK,gBAAgB,KAAK,UAAU;AACpC,OAAK,gBAAgB,KAAK,YAAY;AACtC,OAAK,gBAAgB,KAAK,QAAQ;;CAGpC,cAAc;AACZ,OAAK,UAAU,KAAK,UAAU;;CAGhC,YAAY;AACV,OAAK,UAAU,KAAK,QAAQ;;CAG9B,qBAAqB;AACnB,OAAK,aAAa;AAClB,OAAK,WAAW;;;;;;;CAQlB,sBAAsB,gBAAwB;EAC5C,MAAM,iBAAiB,KAAK,kBAAkB,eAAe;EAC7D,MAAM,eAAeA,kBAAK,SAAS,KAAK,WAAW,eAAe;AAElE,MAAI,aAAa,WAAW,KAAK,CAC/B,OAAM,IAAI,MACR,0DAA0D,iBAC3D;AAGH,SAAO,KAAK,gBAAgB,aAAa;;;;;;;CAQ3C,cAAc,MAAc;EAC1B,MAAM,wBAAwB,KAAK,SAAS,OAAO,GAAG,OAAO,G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,mBAAmB,UAAU,QAAQ;GAClE,MAAMC,iBAAqC;IACzC,GAAG;IACH,GAAG,QAAQ;IACZ;GAED,MAAMC,gBAAiD;IACrD,IAAI,QAAQ;IACZ,OAAO,QAAQ;IACf,UAAU;IACX;AAED,OAAI,eAAe,MAAM;AACvB,kBAAc,UAAU;AACxB,mBAAe,KAAK,cAAc;AAClC;;GAGF,MAAM,eAAe,OAAO,WAAW,QAAQ;GAC/C,MAAM,kBAAkB,cAAc,iBAAiB;GACvD,MAAM,sBACJ,cAAc,qBAAqB;AAErC,OAAI,KAAK,WAAW,gBAAgB,gBAAgB,EAAE;IACpD,MAAM,EAAE,gBAAgB,kBAAkB,kBACxC,MAAM,KAAK,6BACT,MACA,iBACA,KAAK,WAAW,iBAAiB,gBAAgB,EACjD;KACE,GAAG;KACH,eAAe;KACf,mBAAmB;KACpB,CACF;AAEH,kBAAc,WAAW;AACzB,kBAAc,mBAAmB;AACjC,kBAAc,gBAAgB;SAE9B,eAAc,WAAW,MAAM,KAAK,eAClC,MACA,iBACA,eACD;AAGH,kBAAe,KAAK,cAAc;;AAGpC,SAAO;GACL,MAAM;GACN,UAAU;GACX;;;;;CAMH,mBAAmB,GAAW,OAAuB;AACnD,SAAO,IAAI,IAAI,MAAM,MAAM,IAAI,MAAM,SAAS,KAAK;;;;;CAMrD,gBAAgB,OAAqB;AACnC,MAAI,QAAQ,EACV,OAAM,IAAI,MAAM,cAAc,MAAM,uBAAuB;AAG7D,MAAI,SAAS,KAAK,MAAM,OACtB,OAAM,IAAI,MACR,cAAc,MAAM,qBAAqB,KAAK,MAAM,SACrD;AAGH,MAAI,CAAC,KAAK,MAAM,OACd,OAAM,IAAI,MAAM,cAAc,MAAM,eAAe;AAGrD,SAAO,KAAK,MAAM;;;AAItB,yBAAe"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["path","fs","PNG","fs","defaultDiffConfig: DiffConfig","screenshotSettings: ScreenshotSettings","diffImagePath: string | undefined","path","result: ScreenshotCaptureDetails","baseResult: ScreenshotCaptureDetails","variantResults: ScreenshotVariantCaptureDetails[]","variantOptions: ScreenshotSettings","variantResult: ScreenshotVariantCaptureDetails"],"sources":["../src/config.ts","../src/filesystem.ts","../src/compare.ts","../src/screenshot.ts"],"sourcesContent":["import type { PossiblePromise, UserConfig } from \"./types\";\n\n/**\n * Type helper to make it easier to use cappa.config.ts, or a function that returns it. The function receives a ConfigEnv object.\n */\nexport function defineConfig(\n options:\n | PossiblePromise<UserConfig | Array<UserConfig>>\n | (() => PossiblePromise<UserConfig | Array<UserConfig>>),\n): typeof options {\n return options;\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\n\n/**\n * Class for interacting with the local file system to store the screenshots.\n */\nexport class ScreenshotFileSystem {\n private readonly actualDir: string;\n private readonly expectedDir: string;\n private readonly diffDir: string;\n\n constructor(outputDir: string) {\n this.actualDir = path.resolve(outputDir, \"actual\");\n this.expectedDir = path.resolve(outputDir, \"expected\");\n this.diffDir = path.resolve(outputDir, \"diff\");\n\n this.ensureParentDir(this.actualDir);\n this.ensureParentDir(this.expectedDir);\n this.ensureParentDir(this.diffDir);\n }\n\n clearActual() {\n this.removeDir(this.actualDir);\n }\n\n clearDiff() {\n this.removeDir(this.diffDir);\n }\n\n clearActualAndDiff() {\n this.clearActual();\n this.clearDiff();\n }\n\n /**\n * Approve a screenshot based on its actual file path.\n * @param actualFilePath - The path to the actual screenshot file.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n approveFromActualPath(actualFilePath: string) {\n const actualAbsolute = this.resolveActualPath(actualFilePath);\n const relativePath = path.relative(this.actualDir, actualAbsolute);\n\n if (relativePath.startsWith(\"..\")) {\n throw new Error(\n `Cannot approve screenshot outside of actual directory: ${actualFilePath}`,\n );\n }\n\n return this.approveRelative(relativePath);\n }\n\n /**\n * Approve a screenshot based on its name.\n * @param name - The name of the screenshot. Can include '.png' extension.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n approveByName(name: string) {\n const relativeWithExtension = name.endsWith(\".png\") ? name : `${name}.png`;\n\n return this.approveRelative(relativeWithExtension);\n }\n\n /**\n * Approve a screenshot based on a relative path.\n * @param relativePath - The relative path to the screenshot.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n private approveRelative(relativePath: string) {\n const actualPath = path.resolve(this.actualDir, relativePath);\n const expectedPath = path.resolve(this.expectedDir, relativePath);\n const diffPath = path.resolve(this.diffDir, relativePath);\n\n this.ensureParentDir(expectedPath);\n\n fs.copyFileSync(actualPath, expectedPath);\n\n if (fs.existsSync(diffPath)) {\n fs.unlinkSync(diffPath);\n }\n\n return {\n actualPath,\n expectedPath,\n diffPath,\n };\n }\n\n /**\n * Ensure the parent directory of a file exists. If not, it creates it.\n * @param filePath - The path to the file.\n */\n ensureParentDir(filePath: string) {\n const dir = path.dirname(filePath);\n\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n }\n\n /**\n * Remove a directory and all its contents.\n * @param dir - The path to the directory.\n */\n private removeDir(dir: string) {\n if (fs.existsSync(dir)) {\n fs.rmSync(dir, { recursive: true, force: true });\n }\n }\n\n /**\n * Get the actual directory path\n */\n getActualDir(): string {\n return this.actualDir;\n }\n\n /**\n * Get the expected directory path\n */\n getExpectedDir(): string {\n return this.expectedDir;\n }\n\n /**\n * Get the diff directory path\n */\n getDiffDir(): string {\n return this.diffDir;\n }\n\n /**\n * Get the path to an actual screenshot file\n */\n getActualFilePath(filename: string): string {\n return path.resolve(this.actualDir, filename);\n }\n\n /**\n * Get the path to an expected screenshot file\n */\n getExpectedFilePath(filename: string): string {\n return path.resolve(this.expectedDir, filename);\n }\n\n /**\n * Get the path to a diff screenshot file\n */\n getDiffFilePath(filename: string): string {\n return path.resolve(this.diffDir, filename);\n }\n\n /**\n * Write a file to the actual directory\n */\n writeActualFile(filename: string, data: Buffer): void {\n const filepath = this.getActualFilePath(filename);\n this.ensureParentDir(filepath);\n fs.writeFileSync(filepath, data);\n }\n\n /**\n * Write a file to the diff directory\n */\n writeDiffFile(filename: string, data: Buffer): void {\n const filepath = this.getDiffFilePath(filename);\n this.ensureParentDir(filepath);\n fs.writeFileSync(filepath, data);\n }\n\n /**\n * Check if an expected file exists\n */\n hasExpectedFile(filename: string): boolean {\n const expectedPath = this.getExpectedFilePath(filename);\n return fs.existsSync(expectedPath);\n }\n\n /**\n * Read an expected file\n */\n readExpectedFile(filename: string): Buffer {\n const expectedPath = this.getExpectedFilePath(filename);\n if (!fs.existsSync(expectedPath)) {\n throw new Error(`Expected image not found: ${expectedPath}`);\n }\n return fs.readFileSync(expectedPath);\n }\n\n /**\n * Resolve the actual path of a screenshot file.\n * @param filePath - The path to the screenshot file.\n * @returns The actual path of the screenshot file.\n */\n private resolveActualPath(filePath: string) {\n if (path.isAbsolute(filePath)) {\n return filePath;\n }\n\n return path.resolve(this.actualDir, filePath);\n }\n}\n","import fs from \"node:fs\";\nimport blazediff from \"@blazediff/core\";\nimport type { BlazeDiffOptions } from \"@blazediff/types\";\nimport { getLogger } from \"@cappa/logger\";\nimport { PNG } from \"pngjs\";\nimport type { DiffConfig } from \"./types\";\n\nexport type CompareOptions = BlazeDiffOptions;\n\nexport interface CompareResult {\n numDiffPixels: number;\n totalPixels: number;\n percentDifference: number;\n diffBuffer?: Buffer; // PNG buffer of the diff image\n passed: boolean; // Whether the comparison passed based on threshold\n error?: string; // Error message if the comparison failed\n differentSizes: boolean; // Whether the images have different sizes\n}\n\nconst compare = (\n image1: Uint8Array | Uint8ClampedArray,\n image2: Uint8Array | Uint8ClampedArray,\n diff: Uint8Array | Uint8ClampedArray | undefined,\n width: number,\n height: number,\n options?: BlazeDiffOptions,\n) => {\n return blazediff(image1, image2, diff, width, height, options);\n};\n\n/**\n * Compare two PNG images and return the difference\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param withDiff - Whether to create a diff image\n * @param options - Comparison options\n * @returns Comparison result\n */\nexport async function compareImages(\n image1: string | Buffer,\n image2: string | Buffer,\n withDiff: boolean = false,\n options: DiffConfig = {},\n): Promise<CompareResult> {\n const png1 = await loadPNG(image1);\n const png2 = await loadPNG(image2);\n\n const { width, height } = png1;\n\n if (width !== png2.width || height !== png2.height) {\n return {\n numDiffPixels: 0,\n totalPixels: 0,\n percentDifference: 0,\n passed: false,\n differentSizes: true,\n };\n }\n\n const diff = withDiff ? new PNG({ width, height }) : undefined;\n\n try {\n const numDiffPixels = compare(\n png1.data,\n png2.data,\n diff?.data,\n width,\n height,\n options,\n );\n\n const totalPixels = width * height;\n const percentDifference = (numDiffPixels / totalPixels) * 100;\n\n // Convert diff image to buffer\n const diffBuffer = diff ? PNG.sync.write(diff) : undefined;\n\n return {\n numDiffPixels,\n totalPixels,\n percentDifference,\n diffBuffer,\n passed: isPassed(percentDifference, numDiffPixels, options),\n differentSizes: false,\n };\n } catch (error) {\n getLogger().error((error as Error).message || \"Unknown error\");\n\n return {\n numDiffPixels: 0,\n totalPixels: 0,\n percentDifference: 0,\n passed: false,\n differentSizes: false,\n error: (error as Error).message || \"Unknown error\",\n };\n }\n}\n\n/**\n * Load a PNG image from file path or Buffer\n */\nasync function loadPNG(source: string | Buffer): Promise<PNG> {\n if (Buffer.isBuffer(source)) {\n return PNG.sync.read(source);\n } else {\n const buffer = fs.readFileSync(source);\n return PNG.sync.read(buffer);\n }\n}\n\n/**\n * Save a comparison result diff image to file\n * @param result - Comparison result containing diffBuffer\n * @param outputPath - Path to save the diff image\n */\nexport function saveDiffImage(result: CompareResult, outputPath: string): void {\n if (!result.diffBuffer) {\n throw new Error(\"No diff buffer available in comparison result\");\n }\n\n fs.writeFileSync(outputPath, result.diffBuffer);\n}\n\n/**\n * Quick comparison function that returns only pass/fail\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param options - Comparison options\n * @returns Whether the images match\n */\nexport async function imagesMatch(\n image1: string | Buffer,\n image2: string | Buffer,\n options: DiffConfig = {},\n): Promise<boolean> {\n const result = await compareImages(image1, image2, false, options);\n\n return result.passed;\n}\n\n/**\n * Check if the comparison passed based on the options\n * @param percentDifference - Percentage difference between the images\n * @param numDiffPixels - Number of different pixels\n * @param options - Comparison options\n * @returns Whether the comparison passed\n */\nconst isPassed = (\n percentDifference: number,\n numDiffPixels: number,\n options: DiffConfig,\n) => {\n if (options.maxDiffPercentage && options.maxDiffPixels) {\n return (\n percentDifference <= options.maxDiffPercentage &&\n numDiffPixels <= options.maxDiffPixels\n );\n }\n\n if (options.maxDiffPercentage) {\n return percentDifference <= options.maxDiffPercentage;\n }\n\n if (options.maxDiffPixels) {\n return numDiffPixels <= options.maxDiffPixels;\n }\n\n // fallback to 0 pixels\n return numDiffPixels === 0;\n};\n\n/**\n * Create different size png image with all pixels red\n * @returns\n */\nexport function createDiffSizePngImage(width: number, height: number): Buffer {\n const png = new PNG({ width, height });\n\n for (let y = 0; y < height; y++) {\n for (let x = 0; x < width; x++) {\n const idx = (width * y + x) << 2;\n png.data[idx] = 255; // red\n png.data[idx + 1] = 0; // green\n png.data[idx + 2] = 0; // blue\n png.data[idx + 3] = 255; // alpha\n }\n }\n\n return PNG.sync.write(png);\n}\n","import path from \"node:path\";\nimport { getLogger, type Logger } from \"@cappa/logger\";\nimport {\n type Browser,\n type BrowserContext,\n chromium,\n firefox,\n type Page,\n type PageScreenshotOptions,\n webkit,\n} from \"playwright-core\";\nimport {\n type CompareResult,\n compareImages,\n createDiffSizePngImage,\n} from \"./compare\";\nimport { ScreenshotFileSystem } from \"./filesystem\";\nimport type {\n DiffConfig,\n ScreenshotOptions,\n ScreenshotSettings,\n ScreenshotVariant,\n ScreenshotVariantWithUrl,\n} from \"./types\";\n\nconst defaultDiffConfig: DiffConfig = {\n threshold: 0.1,\n includeAA: false,\n fastBufferCheck: true,\n maxDiffPixels: 0,\n maxDiffPercentage: 0,\n};\n\nexport interface ScreenshotCaptureDetails {\n filename: string;\n filepath?: string;\n comparisonResult?: CompareResult;\n diffImagePath?: string;\n skipped?: boolean;\n}\n\nexport interface ScreenshotVariantCaptureDetails\n extends ScreenshotCaptureDetails {\n id: string;\n label?: string;\n}\n\nexport interface ScreenshotCaptureResult {\n base: ScreenshotCaptureDetails;\n variants: ScreenshotVariantCaptureDetails[];\n}\n\nexport interface ScreenshotCaptureExtras {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n variants?: Record<\n string,\n {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n }\n >;\n}\n\nclass ScreenshotTool {\n browserType: \"chromium\" | \"firefox\" | \"webkit\";\n headless: boolean;\n viewport: { width: number; height: number };\n outputDir: string;\n browser: Browser | null = null;\n context: BrowserContext | null = null;\n page: Page | null = null;\n concurrency: number;\n contexts: BrowserContext[] = [];\n pages: Page[] = [];\n diff: DiffConfig;\n logger: Logger;\n retries: number;\n filesystem: ScreenshotFileSystem;\n\n constructor(options: {\n browserType?: \"chromium\" | \"firefox\" | \"webkit\";\n headless?: boolean;\n viewport?: { width: number; height: number };\n outputDir?: string;\n diff?: DiffConfig;\n retries?: number;\n concurrency?: number;\n }) {\n this.browserType = options.browserType || \"chromium\";\n this.headless = options.headless !== false; // Default to headless\n this.viewport = options.viewport || { width: 1920, height: 1080 };\n this.outputDir = options.outputDir || \"./screenshots\";\n this.diff = { ...defaultDiffConfig, ...options.diff };\n this.concurrency = Math.max(1, options.concurrency || 1);\n\n this.logger = getLogger();\n this.retries = options.retries || 2;\n this.filesystem = new ScreenshotFileSystem(this.outputDir);\n }\n\n /**\n * Initialize playwright and page\n */\n async init() {\n // Launch browser based on type\n const browserMap = {\n chromium,\n firefox,\n webkit,\n };\n\n const browserClass = browserMap[this.browserType];\n if (!browserClass) {\n throw new Error(`Unsupported browser type: ${this.browserType}`);\n }\n\n this.browser = await browserClass.launch({\n headless: this.headless,\n });\n\n const defaultUserAgent = (await this.browser.newContext())\n .newPage()\n .then((page) => page.evaluate(() => navigator.userAgent));\n\n // Create N contexts and pages\n this.contexts = [];\n this.pages = [];\n\n for (let i = 0; i < this.concurrency; i++) {\n const context = await this.browser.newContext({\n reducedMotion: \"reduce\",\n deviceScaleFactor: 2,\n userAgent: `${await defaultUserAgent} CappaStorybook`,\n viewport: this.viewport,\n });\n const page = await context.newPage();\n\n this.contexts.push(context);\n this.pages.push(page);\n }\n\n // Backward compatibility\n this.context = this.contexts[0] ?? null;\n this.page = this.pages[0] ?? null;\n }\n\n /**\n * Closes browser\n * If not closed, the process will not exit\n */\n async close() {\n for (const context of this.contexts) {\n await context.close();\n }\n if (this.browser) {\n await this.browser.close();\n }\n }\n\n /**\n * Navigates to a URL with \"domcontentloaded\" waitUntil\n */\n async goTo(page: Page, url: string) {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n await page.goto(url, { waitUntil: \"domcontentloaded\" });\n }\n\n /**\n * Takes a screenshot of the page\n */\n async takeScreenshot(\n page: Page,\n filename: string,\n options: ScreenshotSettings,\n ) {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n if (options.skip) {\n return;\n }\n\n try {\n // Generate filename - save to actual directory\n const filepath = this.filesystem.getActualFilePath(filename);\n\n // Ensure parent directories exist before taking screenshot\n this.filesystem.ensureParentDir(filepath);\n\n // Take screenshot\n const screenshotOptions = {\n path: filepath,\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n animations: \"disabled\",\n caret: \"hide\",\n } satisfies PageScreenshotOptions;\n\n await this.withViewport(page, options.viewport, async () => {\n if (options.delay) {\n await page.waitForTimeout(options.delay);\n }\n\n await page.screenshot(screenshotOptions);\n });\n\n return filepath;\n } catch (error) {\n this.logger.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n /**\n * Sets the viewport size and executes the action, ensuring the original viewport is restored\n * even if the action throws an error.\n */\n private async withViewport<T>(\n page: Page,\n viewport: ScreenshotSettings[\"viewport\"],\n action: () => Promise<T>,\n ): Promise<T> {\n if (!viewport) {\n return action();\n }\n\n const previousViewport = page.viewportSize();\n\n // Check if the new viewport is the same as the current one\n if (\n previousViewport &&\n previousViewport.width === viewport.width &&\n previousViewport.height === viewport.height\n ) {\n return action();\n }\n\n await page.setViewportSize(viewport);\n\n try {\n return await action();\n } finally {\n // Always restore the original viewport, even if the action throws an error\n if (previousViewport) {\n await page.setViewportSize(previousViewport);\n }\n }\n }\n\n /**\n * Takes a screenshot and returns the buffer (for comparison purposes)\n */\n async takeScreenshotBuffer(\n page: Page,\n options: ScreenshotSettings,\n ): Promise<Buffer> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n if (options.skip) {\n throw new Error(\"Screenshot skipped\");\n }\n\n try {\n // Take screenshot without saving to file\n const screenshotOptions = {\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n animations: \"disabled\",\n caret: \"hide\",\n } satisfies PageScreenshotOptions;\n\n const buffer = await this.withViewport(\n page,\n options.viewport,\n async () => {\n if (options.delay) {\n await page.waitForTimeout(options.delay);\n }\n\n return page.screenshot(screenshotOptions);\n },\n );\n\n return buffer;\n } catch (error) {\n this.logger.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n async takeScreenshotWithComparison(\n page: Page,\n filename: string,\n referenceImage: Buffer,\n options: ScreenshotSettings & {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n },\n ): Promise<{\n screenshotPath: string;\n comparisonResult: CompareResult;\n diffImagePath?: string;\n }> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n try {\n // Generate filename and take screenshot - save to actual directory\n const filepath = this.filesystem.getActualFilePath(filename);\n\n const screenshotSettings: ScreenshotSettings = {\n fullPage: options.fullPage,\n mask: options.mask,\n omitBackground: options.omitBackground,\n delay: options.delay,\n viewport: options.viewport,\n };\n\n const retryScreenshot = await this.retryScreenshot(\n page,\n referenceImage,\n screenshotSettings,\n );\n\n if (!retryScreenshot.screenshotPath) {\n throw new Error(\"Screenshot buffer is undefined\");\n }\n\n // Save screenshot to actual directory\n this.filesystem.writeActualFile(filename, retryScreenshot.screenshotPath);\n this.logger.success(`Screenshot saved: ${filepath}`);\n\n if (retryScreenshot.passed) {\n this.logger.success(`Screenshot passed visual comparison`);\n } else {\n this.logger.error(`Screenshot failed visual comparison`);\n }\n\n let diffImagePath: string | undefined;\n\n // Save diff image if requested and there are differences\n if (\n options.saveDiffImage &&\n retryScreenshot.comparisonResult.diffBuffer &&\n !retryScreenshot.passed\n ) {\n const diffFilename = options.diffImageFilename || filename;\n // Save to diff directory\n diffImagePath = this.filesystem.getDiffFilePath(diffFilename);\n\n this.filesystem.writeDiffFile(\n diffFilename,\n retryScreenshot.comparisonResult.diffBuffer,\n );\n\n this.logger.debug(`Diff image saved: ${diffImagePath}`);\n }\n\n // different sizes diff image\n if (retryScreenshot.comparisonResult.differentSizes) {\n this.logger.warn(`Screenshot has different sizes than reference image`);\n\n const diffFilename = options.diffImageFilename || filename;\n diffImagePath = this.filesystem.getDiffFilePath(diffFilename);\n const diffBuffer = createDiffSizePngImage(200, 200);\n\n this.filesystem.writeDiffFile(diffFilename, diffBuffer);\n\n this.logger.debug(`Diff Size image saved: ${diffImagePath}`);\n }\n\n return {\n screenshotPath: filepath,\n comparisonResult: retryScreenshot.comparisonResult,\n diffImagePath,\n };\n } catch (error) {\n this.logger.error(\n `Error taking screenshot with comparison of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n async retryScreenshot(\n page: Page,\n referenceImage: Buffer,\n options: ScreenshotSettings,\n ) {\n for (let i = 0; i < this.retries; i++) {\n try {\n const screenshotBuffer = await this.takeScreenshotBuffer(page, {\n ...options,\n delay: this.getIncBackoffDelay(i, options.delay || 0),\n });\n\n if (!screenshotBuffer) {\n throw new Error(\"Screenshot buffer is undefined\");\n }\n\n const comparisonResult = await compareImages(\n screenshotBuffer,\n referenceImage,\n true,\n this.diff,\n );\n\n // bail early if the images are different sizes\n if (comparisonResult.differentSizes) {\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: false,\n };\n }\n\n // last retry\n if (i === this.retries - 1 && !comparisonResult.passed) {\n this.logger.error(\n `Failed to match screenshot after ${this.retries} retries`,\n );\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: false,\n };\n }\n\n if (comparisonResult.passed) {\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: true,\n };\n } else {\n this.logger.warn(\n `Comparison did not match. Retrying in ${this.getIncBackoffDelay(i + 1, options.delay || 0)}ms... \\nComparison result: ${comparisonResult.numDiffPixels} pixels different (${comparisonResult.percentDifference.toFixed(2)}%)`,\n );\n }\n } catch (error) {\n this.logger.error(\n `Error taking screenshot after ${i + 1} retries:`,\n (error as Error).message,\n );\n }\n }\n\n return {\n screenshotPath: null,\n comparisonResult: null,\n passed: false,\n };\n }\n\n getVariantFilename(filename: string, variant: ScreenshotVariant): string {\n if (variant.filename) {\n return variant.filename;\n }\n\n const parsed = path.parse(filename);\n const sanitizedId = variant.id\n .trim()\n .replace(/[^a-zA-Z0-9_-]+/g, \"-\")\n .replace(/-+/g, \"-\")\n .replace(/^-|-$/g, \"\");\n const suffix = sanitizedId || \"variant\";\n const ext = parsed.ext || \".png\";\n const variantName = `${parsed.name}--${suffix}${ext}`;\n\n return parsed.dir ? path.join(parsed.dir, variantName) : variantName;\n }\n\n /**\n * Captures a single screenshot with the given options\n */\n private async captureSingleScreenshot(\n page: Page,\n filename: string,\n options: ScreenshotSettings,\n extras: {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n } = {},\n ): Promise<ScreenshotCaptureDetails> {\n const result: ScreenshotCaptureDetails = { filename };\n\n if (options.skip) {\n result.skipped = true;\n return result;\n }\n\n const saveDiff = extras.saveDiffImage ?? false;\n const diffFilename = extras.diffImageFilename ?? filename;\n\n if (!this.filesystem.hasExpectedFile(filename)) {\n this.logger.debug(\n `Expected image not found for ${filename}, taking screenshot`,\n );\n\n const { screenshotPath, comparisonResult, diffImagePath } =\n await this.takeScreenshotWithComparison(\n page,\n filename,\n this.filesystem.readExpectedFile(filename),\n {\n ...options,\n saveDiffImage: saveDiff,\n diffImageFilename: diffFilename,\n },\n );\n\n result.filepath = screenshotPath;\n result.comparisonResult = comparisonResult;\n result.diffImagePath = diffImagePath;\n } else {\n result.filepath = await this.takeScreenshot(page, filename, options);\n }\n\n return result;\n }\n\n async capture(\n page: Page,\n filename: string,\n options: ScreenshotOptions,\n extras: ScreenshotCaptureExtras = {},\n ): Promise<ScreenshotCaptureResult> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n const { variants = [], ...baseOptions } = options;\n const baseResult: ScreenshotCaptureDetails = { filename };\n const variantResults: ScreenshotVariantCaptureDetails[] = [];\n\n const baseSaveDiff = extras.saveDiffImage ?? false;\n const baseDiffFilename = extras.diffImageFilename ?? filename;\n\n if (baseOptions.skip) {\n return {\n base: { ...baseResult, skipped: true },\n variants: variants.map((variant) => ({\n id: variant.id,\n label: variant.label,\n filename: this.getVariantFilename(filename, variant),\n skipped: true,\n })),\n };\n }\n\n // Capture base screenshot\n const baseCaptureResult = await this.captureSingleScreenshot(\n page,\n filename,\n baseOptions,\n {\n saveDiffImage: baseSaveDiff,\n diffImageFilename: baseDiffFilename,\n },\n );\n\n baseResult.filepath = baseCaptureResult.filepath;\n baseResult.comparisonResult = baseCaptureResult.comparisonResult;\n baseResult.diffImagePath = baseCaptureResult.diffImagePath;\n baseResult.skipped = baseCaptureResult.skipped;\n\n for (const variant of variants) {\n const variantFilename = this.getVariantFilename(filename, variant);\n const variantOptions: ScreenshotSettings = {\n ...baseOptions,\n ...variant.options,\n };\n\n const variantResult: ScreenshotVariantCaptureDetails = {\n id: variant.id,\n label: variant.label,\n filename: variantFilename,\n };\n\n if (variantOptions.skip) {\n variantResult.skipped = true;\n variantResults.push(variantResult);\n continue;\n }\n\n const variantExtra = extras.variants?.[variant.id];\n const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;\n const variantDiffFilename =\n variantExtra?.diffImageFilename ?? variantFilename;\n\n const variantCaptureResult = await this.captureSingleScreenshot(\n page,\n variantFilename,\n variantOptions,\n {\n saveDiffImage: variantSaveDiff,\n diffImageFilename: variantDiffFilename,\n },\n );\n\n variantResult.filepath = variantCaptureResult.filepath;\n variantResult.comparisonResult = variantCaptureResult.comparisonResult;\n variantResult.diffImagePath = variantCaptureResult.diffImagePath;\n variantResult.skipped = variantCaptureResult.skipped;\n\n variantResults.push(variantResult);\n }\n\n return {\n base: baseResult,\n variants: variantResults,\n };\n }\n\n /**\n * Applies SVG and UI optimizations for screenshots\n */\n private async applyScreenshotOptimizations(page: Page) {\n await page.addStyleTag({\n content: `\n *, *::before, *::after {\n -moz-animation: none !important;\n -moz-transition: none !important;\n transition: none !important; \n animation: none !important;\n }\n *, input, textarea { caret-color: transparent !important }\n *:focus { outline: none !important }\n *:focus-visible { outline: none !important }\n *:focus-within { outline: none !important }\n * { text-rendering: optimizeLegibility !important }\n ::-webkit-scrollbar { display: none !important }\n html { scroll-behavior: auto !important }\n svg, svg * {\n shape-rendering: crispEdges !important;\n text-rendering: geometricPrecision !important;\n vector-effect: non-scaling-stroke !important;\n }\n img { image-rendering: crisp-edges !important }\n `,\n });\n }\n\n /**\n * Captures screenshots for multiple variants, each with their own URL\n */\n async captureWithVariants(\n page: Page,\n baseFilename: string,\n baseUrl: string,\n baseOptions: ScreenshotSettings,\n variants: ScreenshotVariantWithUrl[],\n extras: ScreenshotCaptureExtras = {},\n ): Promise<ScreenshotCaptureResult> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n const baseResult: ScreenshotCaptureDetails = { filename: baseFilename };\n const variantResults: ScreenshotVariantCaptureDetails[] = [];\n\n const baseSaveDiff = extras.saveDiffImage ?? false;\n const baseDiffFilename = extras.diffImageFilename ?? baseFilename;\n\n // Capture base screenshot\n getLogger().debug(`Going to base URL: ${baseUrl}`);\n await page.goto(baseUrl);\n await this.applyScreenshotOptimizations(page);\n const baseCaptureResult = await this.captureSingleScreenshot(\n page,\n baseFilename,\n baseOptions,\n {\n saveDiffImage: baseSaveDiff,\n diffImageFilename: baseDiffFilename,\n },\n );\n\n baseResult.filepath = baseCaptureResult.filepath;\n baseResult.comparisonResult = baseCaptureResult.comparisonResult;\n baseResult.diffImagePath = baseCaptureResult.diffImagePath;\n baseResult.skipped = baseCaptureResult.skipped;\n\n // Capture variant screenshots\n for (const variant of variants) {\n const variantFilename =\n variant.filename || this.getVariantFilename(baseFilename, variant);\n const variantOptions: ScreenshotSettings = {\n ...baseOptions,\n ...variant.options,\n };\n\n const variantResult: ScreenshotVariantCaptureDetails = {\n id: variant.id,\n label: variant.label,\n filename: variantFilename,\n };\n\n if (variantOptions.skip) {\n variantResult.skipped = true;\n variantResults.push(variantResult);\n continue;\n }\n\n // Navigate to variant URL and apply optimizations\n getLogger().debug(`Going to variant URL: ${variant.url}`);\n await page.goto(variant.url);\n await this.applyScreenshotOptimizations(page);\n\n const variantExtra = extras.variants?.[variant.id];\n const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;\n const variantDiffFilename =\n variantExtra?.diffImageFilename ?? variantFilename;\n\n const variantCaptureResult = await this.captureSingleScreenshot(\n page,\n variantFilename,\n variantOptions,\n {\n saveDiffImage: variantSaveDiff,\n diffImageFilename: variantDiffFilename,\n },\n );\n\n variantResult.filepath = variantCaptureResult.filepath;\n variantResult.comparisonResult = variantCaptureResult.comparisonResult;\n variantResult.diffImagePath = variantCaptureResult.diffImagePath;\n variantResult.skipped = variantCaptureResult.skipped;\n\n variantResults.push(variantResult);\n }\n\n return {\n base: baseResult,\n variants: variantResults,\n };\n }\n\n /**\n * Gets the exponential backoff delay\n */\n getIncBackoffDelay(i: number, delay: number): number {\n return i > 0 ? 500 * 2 ** (i - 1) + (delay || 0) : 0;\n }\n\n /**\n * Get a page from the pool by index\n */\n getPageFromPool(index: number): Page {\n if (index < 0) {\n throw new Error(`Page index ${index} must be non-negative`);\n }\n\n if (index >= this.pages.length) {\n throw new Error(\n `Page index ${index} exceeds pool size ${this.pages.length}`,\n );\n }\n\n if (!this.pages[index]) {\n throw new Error(`Page index ${index} is undefined`);\n }\n\n return this.pages[index];\n }\n}\n\nexport default ScreenshotTool;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,SAAgB,aACd,SAGgB;AAChB,QAAO;;;;;;;;ACJT,IAAa,uBAAb,MAAkC;CAChC,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,WAAmB;AAC7B,OAAK,YAAYA,kBAAK,QAAQ,WAAW,SAAS;AAClD,OAAK,cAAcA,kBAAK,QAAQ,WAAW,WAAW;AACtD,OAAK,UAAUA,kBAAK,QAAQ,WAAW,OAAO;AAE9C,OAAK,gBAAgB,KAAK,UAAU;AACpC,OAAK,gBAAgB,KAAK,YAAY;AACtC,OAAK,gBAAgB,KAAK,QAAQ;;CAGpC,cAAc;AACZ,OAAK,UAAU,KAAK,UAAU;;CAGhC,YAAY;AACV,OAAK,UAAU,KAAK,QAAQ;;CAG9B,qBAAqB;AACnB,OAAK,aAAa;AAClB,OAAK,WAAW;;;;;;;CAQlB,sBAAsB,gBAAwB;EAC5C,MAAM,iBAAiB,KAAK,kBAAkB,eAAe;EAC7D,MAAM,eAAeA,kBAAK,SAAS,KAAK,WAAW,eAAe;AAElE,MAAI,aAAa,WAAW,KAAK,CAC/B,OAAM,IAAI,MACR,0DAA0D,iBAC3D;AAGH,SAAO,KAAK,gBAAgB,aAAa;;;;;;;CAQ3C,cAAc,MAAc;EAC1B,MAAM,wBAAwB,KAAK,SAAS,OAAO,GAAG,OAAO,GAAG,KAAK;AAErE,SAAO,KAAK,gBAAgB,sBAAsB;;;;;;;CAQpD,AAAQ,gBAAgB,cAAsB;EAC5C,MAAM,aAAaA,kBAAK,QAAQ,KAAK,WAAW,aAAa;EAC7D,MAAM,eAAeA,kBAAK,QA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O,IAAI,IAAI,MAAM,MAAM,IAAI,MAAM,SAAS,KAAK;;;;;CAMrD,gBAAgB,OAAqB;AACnC,MAAI,QAAQ,EACV,OAAM,IAAI,MAAM,cAAc,MAAM,uBAAuB;AAG7D,MAAI,SAAS,KAAK,MAAM,OACtB,OAAM,IAAI,MACR,cAAc,MAAM,qBAAqB,KAAK,MAAM,SACrD;AAGH,MAAI,CAAC,KAAK,MAAM,OACd,OAAM,IAAI,MAAM,cAAc,MAAM,eAAe;AAGrD,SAAO,KAAK,MAAM;;;AAItB,yBAAe"}
|
package/dist/index.mjs
CHANGED
|
@@ -360,6 +360,7 @@ var ScreenshotTool = class {
|
|
|
360
360
|
if (options.skip) return;
|
|
361
361
|
try {
|
|
362
362
|
const filepath = this.filesystem.getActualFilePath(filename);
|
|
363
|
+
this.filesystem.ensureParentDir(filepath);
|
|
363
364
|
const screenshotOptions = {
|
|
364
365
|
path: filepath,
|
|
365
366
|
fullPage: options.fullPage,
|
|
@@ -508,6 +509,30 @@ var ScreenshotTool = class {
|
|
|
508
509
|
const variantName = `${parsed.name}--${suffix}${ext}`;
|
|
509
510
|
return parsed.dir ? path.join(parsed.dir, variantName) : variantName;
|
|
510
511
|
}
|
|
512
|
+
/**
|
|
513
|
+
* Captures a single screenshot with the given options
|
|
514
|
+
*/
|
|
515
|
+
async captureSingleScreenshot(page, filename, options, extras = {}) {
|
|
516
|
+
const result = { filename };
|
|
517
|
+
if (options.skip) {
|
|
518
|
+
result.skipped = true;
|
|
519
|
+
return result;
|
|
520
|
+
}
|
|
521
|
+
const saveDiff = extras.saveDiffImage ?? false;
|
|
522
|
+
const diffFilename = extras.diffImageFilename ?? filename;
|
|
523
|
+
if (!this.filesystem.hasExpectedFile(filename)) {
|
|
524
|
+
this.logger.debug(`Expected image not found for ${filename}, taking screenshot`);
|
|
525
|
+
const { screenshotPath, comparisonResult, diffImagePath } = await this.takeScreenshotWithComparison(page, filename, this.filesystem.readExpectedFile(filename), {
|
|
526
|
+
...options,
|
|
527
|
+
saveDiffImage: saveDiff,
|
|
528
|
+
diffImageFilename: diffFilename
|
|
529
|
+
});
|
|
530
|
+
result.filepath = screenshotPath;
|
|
531
|
+
result.comparisonResult = comparisonResult;
|
|
532
|
+
result.diffImagePath = diffImagePath;
|
|
533
|
+
} else result.filepath = await this.takeScreenshot(page, filename, options);
|
|
534
|
+
return result;
|
|
535
|
+
}
|
|
511
536
|
async capture(page, filename, options, extras = {}) {
|
|
512
537
|
if (!this.browser) throw new Error("Browser not initialized");
|
|
513
538
|
const { variants = [],...baseOptions } = options;
|
|
@@ -527,18 +552,96 @@ var ScreenshotTool = class {
|
|
|
527
552
|
skipped: true
|
|
528
553
|
}))
|
|
529
554
|
};
|
|
530
|
-
|
|
531
|
-
|
|
555
|
+
const baseCaptureResult = await this.captureSingleScreenshot(page, filename, baseOptions, {
|
|
556
|
+
saveDiffImage: baseSaveDiff,
|
|
557
|
+
diffImageFilename: baseDiffFilename
|
|
558
|
+
});
|
|
559
|
+
baseResult.filepath = baseCaptureResult.filepath;
|
|
560
|
+
baseResult.comparisonResult = baseCaptureResult.comparisonResult;
|
|
561
|
+
baseResult.diffImagePath = baseCaptureResult.diffImagePath;
|
|
562
|
+
baseResult.skipped = baseCaptureResult.skipped;
|
|
563
|
+
for (const variant of variants) {
|
|
564
|
+
const variantFilename = this.getVariantFilename(filename, variant);
|
|
565
|
+
const variantOptions = {
|
|
532
566
|
...baseOptions,
|
|
533
|
-
|
|
534
|
-
|
|
567
|
+
...variant.options
|
|
568
|
+
};
|
|
569
|
+
const variantResult = {
|
|
570
|
+
id: variant.id,
|
|
571
|
+
label: variant.label,
|
|
572
|
+
filename: variantFilename
|
|
573
|
+
};
|
|
574
|
+
if (variantOptions.skip) {
|
|
575
|
+
variantResult.skipped = true;
|
|
576
|
+
variantResults.push(variantResult);
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
579
|
+
const variantExtra = extras.variants?.[variant.id];
|
|
580
|
+
const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;
|
|
581
|
+
const variantDiffFilename = variantExtra?.diffImageFilename ?? variantFilename;
|
|
582
|
+
const variantCaptureResult = await this.captureSingleScreenshot(page, variantFilename, variantOptions, {
|
|
583
|
+
saveDiffImage: variantSaveDiff,
|
|
584
|
+
diffImageFilename: variantDiffFilename
|
|
535
585
|
});
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
586
|
+
variantResult.filepath = variantCaptureResult.filepath;
|
|
587
|
+
variantResult.comparisonResult = variantCaptureResult.comparisonResult;
|
|
588
|
+
variantResult.diffImagePath = variantCaptureResult.diffImagePath;
|
|
589
|
+
variantResult.skipped = variantCaptureResult.skipped;
|
|
590
|
+
variantResults.push(variantResult);
|
|
591
|
+
}
|
|
592
|
+
return {
|
|
593
|
+
base: baseResult,
|
|
594
|
+
variants: variantResults
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Applies SVG and UI optimizations for screenshots
|
|
599
|
+
*/
|
|
600
|
+
async applyScreenshotOptimizations(page) {
|
|
601
|
+
await page.addStyleTag({ content: `
|
|
602
|
+
*, *::before, *::after {
|
|
603
|
+
-moz-animation: none !important;
|
|
604
|
+
-moz-transition: none !important;
|
|
605
|
+
transition: none !important;
|
|
606
|
+
animation: none !important;
|
|
607
|
+
}
|
|
608
|
+
*, input, textarea { caret-color: transparent !important }
|
|
609
|
+
*:focus { outline: none !important }
|
|
610
|
+
*:focus-visible { outline: none !important }
|
|
611
|
+
*:focus-within { outline: none !important }
|
|
612
|
+
* { text-rendering: optimizeLegibility !important }
|
|
613
|
+
::-webkit-scrollbar { display: none !important }
|
|
614
|
+
html { scroll-behavior: auto !important }
|
|
615
|
+
svg, svg * {
|
|
616
|
+
shape-rendering: crispEdges !important;
|
|
617
|
+
text-rendering: geometricPrecision !important;
|
|
618
|
+
vector-effect: non-scaling-stroke !important;
|
|
619
|
+
}
|
|
620
|
+
img { image-rendering: crisp-edges !important }
|
|
621
|
+
` });
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Captures screenshots for multiple variants, each with their own URL
|
|
625
|
+
*/
|
|
626
|
+
async captureWithVariants(page, baseFilename, baseUrl, baseOptions, variants, extras = {}) {
|
|
627
|
+
if (!this.browser) throw new Error("Browser not initialized");
|
|
628
|
+
const baseResult = { filename: baseFilename };
|
|
629
|
+
const variantResults = [];
|
|
630
|
+
const baseSaveDiff = extras.saveDiffImage ?? false;
|
|
631
|
+
const baseDiffFilename = extras.diffImageFilename ?? baseFilename;
|
|
632
|
+
getLogger().debug(`Going to base URL: ${baseUrl}`);
|
|
633
|
+
await page.goto(baseUrl);
|
|
634
|
+
await this.applyScreenshotOptimizations(page);
|
|
635
|
+
const baseCaptureResult = await this.captureSingleScreenshot(page, baseFilename, baseOptions, {
|
|
636
|
+
saveDiffImage: baseSaveDiff,
|
|
637
|
+
diffImageFilename: baseDiffFilename
|
|
638
|
+
});
|
|
639
|
+
baseResult.filepath = baseCaptureResult.filepath;
|
|
640
|
+
baseResult.comparisonResult = baseCaptureResult.comparisonResult;
|
|
641
|
+
baseResult.diffImagePath = baseCaptureResult.diffImagePath;
|
|
642
|
+
baseResult.skipped = baseCaptureResult.skipped;
|
|
540
643
|
for (const variant of variants) {
|
|
541
|
-
const variantFilename = this.getVariantFilename(
|
|
644
|
+
const variantFilename = variant.filename || this.getVariantFilename(baseFilename, variant);
|
|
542
645
|
const variantOptions = {
|
|
543
646
|
...baseOptions,
|
|
544
647
|
...variant.options
|
|
@@ -553,19 +656,20 @@ var ScreenshotTool = class {
|
|
|
553
656
|
variantResults.push(variantResult);
|
|
554
657
|
continue;
|
|
555
658
|
}
|
|
659
|
+
getLogger().debug(`Going to variant URL: ${variant.url}`);
|
|
660
|
+
await page.goto(variant.url);
|
|
661
|
+
await this.applyScreenshotOptimizations(page);
|
|
556
662
|
const variantExtra = extras.variants?.[variant.id];
|
|
557
663
|
const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;
|
|
558
664
|
const variantDiffFilename = variantExtra?.diffImageFilename ?? variantFilename;
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
variantResult.diffImagePath = diffImagePath;
|
|
568
|
-
} else variantResult.filepath = await this.takeScreenshot(page, variantFilename, variantOptions);
|
|
665
|
+
const variantCaptureResult = await this.captureSingleScreenshot(page, variantFilename, variantOptions, {
|
|
666
|
+
saveDiffImage: variantSaveDiff,
|
|
667
|
+
diffImageFilename: variantDiffFilename
|
|
668
|
+
});
|
|
669
|
+
variantResult.filepath = variantCaptureResult.filepath;
|
|
670
|
+
variantResult.comparisonResult = variantCaptureResult.comparisonResult;
|
|
671
|
+
variantResult.diffImagePath = variantCaptureResult.diffImagePath;
|
|
672
|
+
variantResult.skipped = variantCaptureResult.skipped;
|
|
569
673
|
variantResults.push(variantResult);
|
|
570
674
|
}
|
|
571
675
|
return {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["defaultDiffConfig: DiffConfig","screenshotSettings: ScreenshotSettings","diffImagePath: string | undefined","baseResult: ScreenshotCaptureDetails","variantResults: ScreenshotVariantCaptureDetails[]","variantOptions: ScreenshotSettings","variantResult: ScreenshotVariantCaptureDetails"],"sources":["../src/config.ts","../src/filesystem.ts","../src/compare.ts","../src/screenshot.ts"],"sourcesContent":["import type { PossiblePromise, UserConfig } from \"./types\";\n\n/**\n * Type helper to make it easier to use cappa.config.ts, or a function that returns it. The function receives a ConfigEnv object.\n */\nexport function defineConfig(\n options:\n | PossiblePromise<UserConfig | Array<UserConfig>>\n | (() => PossiblePromise<UserConfig | Array<UserConfig>>),\n): typeof options {\n return options;\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\n\n/**\n * Class for interacting with the local file system to store the screenshots.\n */\nexport class ScreenshotFileSystem {\n private readonly actualDir: string;\n private readonly expectedDir: string;\n private readonly diffDir: string;\n\n constructor(outputDir: string) {\n this.actualDir = path.resolve(outputDir, \"actual\");\n this.expectedDir = path.resolve(outputDir, \"expected\");\n this.diffDir = path.resolve(outputDir, \"diff\");\n\n this.ensureParentDir(this.actualDir);\n this.ensureParentDir(this.expectedDir);\n this.ensureParentDir(this.diffDir);\n }\n\n clearActual() {\n this.removeDir(this.actualDir);\n }\n\n clearDiff() {\n this.removeDir(this.diffDir);\n }\n\n clearActualAndDiff() {\n this.clearActual();\n this.clearDiff();\n }\n\n /**\n * Approve a screenshot based on its actual file path.\n * @param actualFilePath - The path to the actual screenshot file.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n approveFromActualPath(actualFilePath: string) {\n const actualAbsolute = this.resolveActualPath(actualFilePath);\n const relativePath = path.relative(this.actualDir, actualAbsolute);\n\n if (relativePath.startsWith(\"..\")) {\n throw new Error(\n `Cannot approve screenshot outside of actual directory: ${actualFilePath}`,\n );\n }\n\n return this.approveRelative(relativePath);\n }\n\n /**\n * Approve a screenshot based on its name.\n * @param name - The name of the screenshot. Can include '.png' extension.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n approveByName(name: string) {\n const relativeWithExtension = name.endsWith(\".png\") ? name : `${name}.png`;\n\n return this.approveRelative(relativeWithExtension);\n }\n\n /**\n * Approve a screenshot based on a relative path.\n * @param relativePath - The relative path to the screenshot.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n private approveRelative(relativePath: string) {\n const actualPath = path.resolve(this.actualDir, relativePath);\n const expectedPath = path.resolve(this.expectedDir, relativePath);\n const diffPath = path.resolve(this.diffDir, relativePath);\n\n this.ensureParentDir(expectedPath);\n\n fs.copyFileSync(actualPath, expectedPath);\n\n if (fs.existsSync(diffPath)) {\n fs.unlinkSync(diffPath);\n }\n\n return {\n actualPath,\n expectedPath,\n diffPath,\n };\n }\n\n /**\n * Ensure the parent directory of a file exists. If not, it creates it.\n * @param filePath - The path to the file.\n */\n private ensureParentDir(filePath: string) {\n const dir = path.dirname(filePath);\n\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n }\n\n /**\n * Remove a directory and all its contents.\n * @param dir - The path to the directory.\n */\n private removeDir(dir: string) {\n if (fs.existsSync(dir)) {\n fs.rmSync(dir, { recursive: true, force: true });\n }\n }\n\n /**\n * Get the actual directory path\n */\n getActualDir(): string {\n return this.actualDir;\n }\n\n /**\n * Get the expected directory path\n */\n getExpectedDir(): string {\n return this.expectedDir;\n }\n\n /**\n * Get the diff directory path\n */\n getDiffDir(): string {\n return this.diffDir;\n }\n\n /**\n * Get the path to an actual screenshot file\n */\n getActualFilePath(filename: string): string {\n return path.resolve(this.actualDir, filename);\n }\n\n /**\n * Get the path to an expected screenshot file\n */\n getExpectedFilePath(filename: string): string {\n return path.resolve(this.expectedDir, filename);\n }\n\n /**\n * Get the path to a diff screenshot file\n */\n getDiffFilePath(filename: string): string {\n return path.resolve(this.diffDir, filename);\n }\n\n /**\n * Write a file to the actual directory\n */\n writeActualFile(filename: string, data: Buffer): void {\n const filepath = this.getActualFilePath(filename);\n this.ensureParentDir(filepath);\n fs.writeFileSync(filepath, data);\n }\n\n /**\n * Write a file to the diff directory\n */\n writeDiffFile(filename: string, data: Buffer): void {\n const filepath = this.getDiffFilePath(filename);\n this.ensureParentDir(filepath);\n fs.writeFileSync(filepath, data);\n }\n\n /**\n * Check if an expected file exists\n */\n hasExpectedFile(filename: string): boolean {\n const expectedPath = this.getExpectedFilePath(filename);\n return fs.existsSync(expectedPath);\n }\n\n /**\n * Read an expected file\n */\n readExpectedFile(filename: string): Buffer {\n const expectedPath = this.getExpectedFilePath(filename);\n if (!fs.existsSync(expectedPath)) {\n throw new Error(`Expected image not found: ${expectedPath}`);\n }\n return fs.readFileSync(expectedPath);\n }\n\n /**\n * Resolve the actual path of a screenshot file.\n * @param filePath - The path to the screenshot file.\n * @returns The actual path of the screenshot file.\n */\n private resolveActualPath(filePath: string) {\n if (path.isAbsolute(filePath)) {\n return filePath;\n }\n\n return path.resolve(this.actualDir, filePath);\n }\n}\n","import fs from \"node:fs\";\nimport blazediff from \"@blazediff/core\";\nimport type { BlazeDiffOptions } from \"@blazediff/types\";\nimport { getLogger } from \"@cappa/logger\";\nimport { PNG } from \"pngjs\";\nimport type { DiffConfig } from \"./types\";\n\nexport type CompareOptions = BlazeDiffOptions;\n\nexport interface CompareResult {\n numDiffPixels: number;\n totalPixels: number;\n percentDifference: number;\n diffBuffer?: Buffer; // PNG buffer of the diff image\n passed: boolean; // Whether the comparison passed based on threshold\n error?: string; // Error message if the comparison failed\n differentSizes: boolean; // Whether the images have different sizes\n}\n\nconst compare = (\n image1: Uint8Array | Uint8ClampedArray,\n image2: Uint8Array | Uint8ClampedArray,\n diff: Uint8Array | Uint8ClampedArray | undefined,\n width: number,\n height: number,\n options?: BlazeDiffOptions,\n) => {\n return blazediff(image1, image2, diff, width, height, options);\n};\n\n/**\n * Compare two PNG images and return the difference\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param withDiff - Whether to create a diff image\n * @param options - Comparison options\n * @returns Comparison result\n */\nexport async function compareImages(\n image1: string | Buffer,\n image2: string | Buffer,\n withDiff: boolean = false,\n options: DiffConfig = {},\n): Promise<CompareResult> {\n const png1 = await loadPNG(image1);\n const png2 = await loadPNG(image2);\n\n const { width, height } = png1;\n\n if (width !== png2.width || height !== png2.height) {\n return {\n numDiffPixels: 0,\n totalPixels: 0,\n percentDifference: 0,\n passed: false,\n differentSizes: true,\n };\n }\n\n const diff = withDiff ? new PNG({ width, height }) : undefined;\n\n try {\n const numDiffPixels = compare(\n png1.data,\n png2.data,\n diff?.data,\n width,\n height,\n options,\n );\n\n const totalPixels = width * height;\n const percentDifference = (numDiffPixels / totalPixels) * 100;\n\n // Convert diff image to buffer\n const diffBuffer = diff ? PNG.sync.write(diff) : undefined;\n\n return {\n numDiffPixels,\n totalPixels,\n percentDifference,\n diffBuffer,\n passed: isPassed(percentDifference, numDiffPixels, options),\n differentSizes: false,\n };\n } catch (error) {\n getLogger().error((error as Error).message || \"Unknown error\");\n\n return {\n numDiffPixels: 0,\n totalPixels: 0,\n percentDifference: 0,\n passed: false,\n differentSizes: false,\n error: (error as Error).message || \"Unknown error\",\n };\n }\n}\n\n/**\n * Load a PNG image from file path or Buffer\n */\nasync function loadPNG(source: string | Buffer): Promise<PNG> {\n if (Buffer.isBuffer(source)) {\n return PNG.sync.read(source);\n } else {\n const buffer = fs.readFileSync(source);\n return PNG.sync.read(buffer);\n }\n}\n\n/**\n * Save a comparison result diff image to file\n * @param result - Comparison result containing diffBuffer\n * @param outputPath - Path to save the diff image\n */\nexport function saveDiffImage(result: CompareResult, outputPath: string): void {\n if (!result.diffBuffer) {\n throw new Error(\"No diff buffer available in comparison result\");\n }\n\n fs.writeFileSync(outputPath, result.diffBuffer);\n}\n\n/**\n * Quick comparison function that returns only pass/fail\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param options - Comparison options\n * @returns Whether the images match\n */\nexport async function imagesMatch(\n image1: string | Buffer,\n image2: string | Buffer,\n options: DiffConfig = {},\n): Promise<boolean> {\n const result = await compareImages(image1, image2, false, options);\n\n return result.passed;\n}\n\n/**\n * Check if the comparison passed based on the options\n * @param percentDifference - Percentage difference between the images\n * @param numDiffPixels - Number of different pixels\n * @param options - Comparison options\n * @returns Whether the comparison passed\n */\nconst isPassed = (\n percentDifference: number,\n numDiffPixels: number,\n options: DiffConfig,\n) => {\n if (options.maxDiffPercentage && options.maxDiffPixels) {\n return (\n percentDifference <= options.maxDiffPercentage &&\n numDiffPixels <= options.maxDiffPixels\n );\n }\n\n if (options.maxDiffPercentage) {\n return percentDifference <= options.maxDiffPercentage;\n }\n\n if (options.maxDiffPixels) {\n return numDiffPixels <= options.maxDiffPixels;\n }\n\n // fallback to 0 pixels\n return numDiffPixels === 0;\n};\n\n/**\n * Create different size png image with all pixels red\n * @returns\n */\nexport function createDiffSizePngImage(width: number, height: number): Buffer {\n const png = new PNG({ width, height });\n\n for (let y = 0; y < height; y++) {\n for (let x = 0; x < width; x++) {\n const idx = (width * y + x) << 2;\n png.data[idx] = 255; // red\n png.data[idx + 1] = 0; // green\n png.data[idx + 2] = 0; // blue\n png.data[idx + 3] = 255; // alpha\n }\n }\n\n return PNG.sync.write(png);\n}\n","import path from \"node:path\";\nimport { getLogger, type Logger } from \"@cappa/logger\";\nimport {\n type Browser,\n type BrowserContext,\n chromium,\n firefox,\n type Page,\n type PageScreenshotOptions,\n webkit,\n} from \"playwright-core\";\nimport {\n type CompareResult,\n compareImages,\n createDiffSizePngImage,\n} from \"./compare\";\nimport { ScreenshotFileSystem } from \"./filesystem\";\nimport type {\n DiffConfig,\n ScreenshotOptions,\n ScreenshotSettings,\n ScreenshotVariant,\n} from \"./types\";\n\nconst defaultDiffConfig: DiffConfig = {\n threshold: 0.1,\n includeAA: false,\n fastBufferCheck: true,\n maxDiffPixels: 0,\n maxDiffPercentage: 0,\n};\n\nexport interface ScreenshotCaptureDetails {\n filename: string;\n filepath?: string;\n comparisonResult?: CompareResult;\n diffImagePath?: string;\n skipped?: boolean;\n}\n\nexport interface ScreenshotVariantCaptureDetails\n extends ScreenshotCaptureDetails {\n id: string;\n label?: string;\n}\n\nexport interface ScreenshotCaptureResult {\n base: ScreenshotCaptureDetails;\n variants: ScreenshotVariantCaptureDetails[];\n}\n\nexport interface ScreenshotCaptureExtras {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n variants?: Record<\n string,\n {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n }\n >;\n}\n\nclass ScreenshotTool {\n browserType: \"chromium\" | \"firefox\" | \"webkit\";\n headless: boolean;\n viewport: { width: number; height: number };\n outputDir: string;\n browser: Browser | null = null;\n context: BrowserContext | null = null;\n page: Page | null = null;\n concurrency: number;\n contexts: BrowserContext[] = [];\n pages: Page[] = [];\n diff: DiffConfig;\n logger: Logger;\n retries: number;\n filesystem: ScreenshotFileSystem;\n\n constructor(options: {\n browserType?: \"chromium\" | \"firefox\" | \"webkit\";\n headless?: boolean;\n viewport?: { width: number; height: number };\n outputDir?: string;\n diff?: DiffConfig;\n retries?: number;\n concurrency?: number;\n }) {\n this.browserType = options.browserType || \"chromium\";\n this.headless = options.headless !== false; // Default to headless\n this.viewport = options.viewport || { width: 1920, height: 1080 };\n this.outputDir = options.outputDir || \"./screenshots\";\n this.diff = { ...defaultDiffConfig, ...options.diff };\n this.concurrency = Math.max(1, options.concurrency || 1);\n\n this.logger = getLogger();\n this.retries = options.retries || 2;\n this.filesystem = new ScreenshotFileSystem(this.outputDir);\n }\n\n /**\n * Initialize playwright and page\n */\n async init() {\n // Launch browser based on type\n const browserMap = {\n chromium,\n firefox,\n webkit,\n };\n\n const browserClass = browserMap[this.browserType];\n if (!browserClass) {\n throw new Error(`Unsupported browser type: ${this.browserType}`);\n }\n\n this.browser = await browserClass.launch({\n headless: this.headless,\n });\n\n const defaultUserAgent = (await this.browser.newContext())\n .newPage()\n .then((page) => page.evaluate(() => navigator.userAgent));\n\n // Create N contexts and pages\n this.contexts = [];\n this.pages = [];\n\n for (let i = 0; i < this.concurrency; i++) {\n const context = await this.browser.newContext({\n reducedMotion: \"reduce\",\n deviceScaleFactor: 2,\n userAgent: `${await defaultUserAgent} CappaStorybook`,\n viewport: this.viewport,\n });\n const page = await context.newPage();\n\n this.contexts.push(context);\n this.pages.push(page);\n }\n\n // Backward compatibility\n this.context = this.contexts[0] ?? null;\n this.page = this.pages[0] ?? null;\n }\n\n /**\n * Closes browser\n * If not closed, the process will not exit\n */\n async close() {\n for (const context of this.contexts) {\n await context.close();\n }\n if (this.browser) {\n await this.browser.close();\n }\n }\n\n /**\n * Navigates to a URL with \"domcontentloaded\" waitUntil\n */\n async goTo(page: Page, url: string) {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n await page.goto(url, { waitUntil: \"domcontentloaded\" });\n }\n\n /**\n * Takes a screenshot of the page\n */\n async takeScreenshot(\n page: Page,\n filename: string,\n options: ScreenshotSettings,\n ) {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n if (options.skip) {\n return;\n }\n\n try {\n // Generate filename - save to actual directory\n const filepath = this.filesystem.getActualFilePath(filename);\n\n // Take screenshot\n const screenshotOptions = {\n path: filepath,\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n animations: \"disabled\",\n caret: \"hide\",\n } satisfies PageScreenshotOptions;\n\n await this.withViewport(page, options.viewport, async () => {\n if (options.delay) {\n await page.waitForTimeout(options.delay);\n }\n\n await page.screenshot(screenshotOptions);\n });\n\n return filepath;\n } catch (error) {\n this.logger.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n /**\n * Sets the viewport size and executes the action, ensuring the original viewport is restored\n * even if the action throws an error.\n */\n private async withViewport<T>(\n page: Page,\n viewport: ScreenshotSettings[\"viewport\"],\n action: () => Promise<T>,\n ): Promise<T> {\n if (!viewport) {\n return action();\n }\n\n const previousViewport = page.viewportSize();\n\n // Check if the new viewport is the same as the current one\n if (\n previousViewport &&\n previousViewport.width === viewport.width &&\n previousViewport.height === viewport.height\n ) {\n return action();\n }\n\n await page.setViewportSize(viewport);\n\n try {\n return await action();\n } finally {\n // Always restore the original viewport, even if the action throws an error\n if (previousViewport) {\n await page.setViewportSize(previousViewport);\n }\n }\n }\n\n /**\n * Takes a screenshot and returns the buffer (for comparison purposes)\n */\n async takeScreenshotBuffer(\n page: Page,\n options: ScreenshotSettings,\n ): Promise<Buffer> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n if (options.skip) {\n throw new Error(\"Screenshot skipped\");\n }\n\n try {\n // Take screenshot without saving to file\n const screenshotOptions = {\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n animations: \"disabled\",\n caret: \"hide\",\n } satisfies PageScreenshotOptions;\n\n const buffer = await this.withViewport(\n page,\n options.viewport,\n async () => {\n if (options.delay) {\n await page.waitForTimeout(options.delay);\n }\n\n return page.screenshot(screenshotOptions);\n },\n );\n\n return buffer;\n } catch (error) {\n this.logger.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n async takeScreenshotWithComparison(\n page: Page,\n filename: string,\n referenceImage: Buffer,\n options: ScreenshotSettings & {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n },\n ): Promise<{\n screenshotPath: string;\n comparisonResult: CompareResult;\n diffImagePath?: string;\n }> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n try {\n // Generate filename and take screenshot - save to actual directory\n const filepath = this.filesystem.getActualFilePath(filename);\n\n const screenshotSettings: ScreenshotSettings = {\n fullPage: options.fullPage,\n mask: options.mask,\n omitBackground: options.omitBackground,\n delay: options.delay,\n viewport: options.viewport,\n };\n\n const retryScreenshot = await this.retryScreenshot(\n page,\n referenceImage,\n screenshotSettings,\n );\n\n if (!retryScreenshot.screenshotPath) {\n throw new Error(\"Screenshot buffer is undefined\");\n }\n\n // Save screenshot to actual directory\n this.filesystem.writeActualFile(filename, retryScreenshot.screenshotPath);\n this.logger.success(`Screenshot saved: ${filepath}`);\n\n if (retryScreenshot.passed) {\n this.logger.success(`Screenshot passed visual comparison`);\n } else {\n this.logger.error(`Screenshot failed visual comparison`);\n }\n\n let diffImagePath: string | undefined;\n\n // Save diff image if requested and there are differences\n if (\n options.saveDiffImage &&\n retryScreenshot.comparisonResult.diffBuffer &&\n !retryScreenshot.passed\n ) {\n const diffFilename = options.diffImageFilename || filename;\n // Save to diff directory\n diffImagePath = this.filesystem.getDiffFilePath(diffFilename);\n\n this.filesystem.writeDiffFile(\n diffFilename,\n retryScreenshot.comparisonResult.diffBuffer,\n );\n\n this.logger.debug(`Diff image saved: ${diffImagePath}`);\n }\n\n // different sizes diff image\n if (retryScreenshot.comparisonResult.differentSizes) {\n this.logger.warn(`Screenshot has different sizes than reference image`);\n\n const diffFilename = options.diffImageFilename || filename;\n diffImagePath = this.filesystem.getDiffFilePath(diffFilename);\n const diffBuffer = createDiffSizePngImage(200, 200);\n\n this.filesystem.writeDiffFile(diffFilename, diffBuffer);\n\n this.logger.debug(`Diff Size image saved: ${diffImagePath}`);\n }\n\n return {\n screenshotPath: filepath,\n comparisonResult: retryScreenshot.comparisonResult,\n diffImagePath,\n };\n } catch (error) {\n this.logger.error(\n `Error taking screenshot with comparison of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n async retryScreenshot(\n page: Page,\n referenceImage: Buffer,\n options: ScreenshotSettings,\n ) {\n for (let i = 0; i < this.retries; i++) {\n try {\n const screenshotBuffer = await this.takeScreenshotBuffer(page, {\n ...options,\n delay: this.getIncBackoffDelay(i, options.delay || 0),\n });\n\n if (!screenshotBuffer) {\n throw new Error(\"Screenshot buffer is undefined\");\n }\n\n const comparisonResult = await compareImages(\n screenshotBuffer,\n referenceImage,\n true,\n this.diff,\n );\n\n // bail early if the images are different sizes\n if (comparisonResult.differentSizes) {\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: false,\n };\n }\n\n // last retry\n if (i === this.retries - 1 && !comparisonResult.passed) {\n this.logger.error(\n `Failed to match screenshot after ${this.retries} retries`,\n );\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: false,\n };\n }\n\n if (comparisonResult.passed) {\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: true,\n };\n } else {\n this.logger.warn(\n `Comparison did not match. Retrying in ${this.getIncBackoffDelay(i + 1, options.delay || 0)}ms... \\nComparison result: ${comparisonResult.numDiffPixels} pixels different (${comparisonResult.percentDifference.toFixed(2)}%)`,\n );\n }\n } catch (error) {\n this.logger.error(\n `Error taking screenshot after ${i + 1} retries:`,\n (error as Error).message,\n );\n }\n }\n\n return {\n screenshotPath: null,\n comparisonResult: null,\n passed: false,\n };\n }\n\n getVariantFilename(filename: string, variant: ScreenshotVariant): string {\n if (variant.filename) {\n return variant.filename;\n }\n\n const parsed = path.parse(filename);\n const sanitizedId = variant.id\n .trim()\n .replace(/[^a-zA-Z0-9_-]+/g, \"-\")\n .replace(/-+/g, \"-\")\n .replace(/^-|-$/g, \"\");\n const suffix = sanitizedId || \"variant\";\n const ext = parsed.ext || \".png\";\n const variantName = `${parsed.name}--${suffix}${ext}`;\n\n return parsed.dir ? path.join(parsed.dir, variantName) : variantName;\n }\n\n async capture(\n page: Page,\n filename: string,\n options: ScreenshotOptions,\n extras: ScreenshotCaptureExtras = {},\n ): Promise<ScreenshotCaptureResult> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n const { variants = [], ...baseOptions } = options;\n const baseResult: ScreenshotCaptureDetails = { filename };\n const variantResults: ScreenshotVariantCaptureDetails[] = [];\n\n const baseSaveDiff = extras.saveDiffImage ?? false;\n const baseDiffFilename = extras.diffImageFilename ?? filename;\n\n if (baseOptions.skip) {\n return {\n base: { ...baseResult, skipped: true },\n variants: variants.map((variant) => ({\n id: variant.id,\n label: variant.label,\n filename: this.getVariantFilename(filename, variant),\n skipped: true,\n })),\n };\n }\n\n if (this.filesystem.hasExpectedFile(filename)) {\n const { screenshotPath, comparisonResult, diffImagePath } =\n await this.takeScreenshotWithComparison(\n page,\n filename,\n this.filesystem.readExpectedFile(filename),\n {\n ...baseOptions,\n saveDiffImage: baseSaveDiff,\n diffImageFilename: baseDiffFilename,\n },\n );\n\n baseResult.filepath = screenshotPath;\n baseResult.comparisonResult = comparisonResult;\n baseResult.diffImagePath = diffImagePath;\n } else {\n baseResult.filepath = await this.takeScreenshot(\n page,\n filename,\n baseOptions,\n );\n }\n\n for (const variant of variants) {\n const variantFilename = this.getVariantFilename(filename, variant);\n const variantOptions: ScreenshotSettings = {\n ...baseOptions,\n ...variant.options,\n };\n\n const variantResult: ScreenshotVariantCaptureDetails = {\n id: variant.id,\n label: variant.label,\n filename: variantFilename,\n };\n\n if (variantOptions.skip) {\n variantResult.skipped = true;\n variantResults.push(variantResult);\n continue;\n }\n\n const variantExtra = extras.variants?.[variant.id];\n const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;\n const variantDiffFilename =\n variantExtra?.diffImageFilename ?? variantFilename;\n\n if (this.filesystem.hasExpectedFile(variantFilename)) {\n const { screenshotPath, comparisonResult, diffImagePath } =\n await this.takeScreenshotWithComparison(\n page,\n variantFilename,\n this.filesystem.readExpectedFile(variantFilename),\n {\n ...variantOptions,\n saveDiffImage: variantSaveDiff,\n diffImageFilename: variantDiffFilename,\n },\n );\n\n variantResult.filepath = screenshotPath;\n variantResult.comparisonResult = comparisonResult;\n variantResult.diffImagePath = diffImagePath;\n } else {\n variantResult.filepath = await this.takeScreenshot(\n page,\n variantFilename,\n variantOptions,\n );\n }\n\n variantResults.push(variantResult);\n }\n\n return {\n base: baseResult,\n variants: variantResults,\n };\n }\n\n /**\n * Gets the exponential backoff delay\n */\n getIncBackoffDelay(i: number, delay: number): number {\n return i > 0 ? 500 * 2 ** (i - 1) + (delay || 0) : 0;\n }\n\n /**\n * Get a page from the pool by index\n */\n getPageFromPool(index: number): Page {\n if (index < 0) {\n throw new Error(`Page index ${index} must be non-negative`);\n }\n\n if (index >= this.pages.length) {\n throw new Error(\n `Page index ${index} exceeds pool size ${this.pages.length}`,\n );\n }\n\n if (!this.pages[index]) {\n throw new Error(`Page index ${index} is undefined`);\n }\n\n return this.pages[index];\n }\n}\n\nexport default ScreenshotTool;\n"],"mappings":";;;;;;;;;;;AAKA,SAAgB,aACd,SAGgB;AAChB,QAAO;;;;;;;;ACJT,IAAa,uBAAb,MAAkC;CAChC,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,WAAmB;AAC7B,OAAK,YAAY,KAAK,QAAQ,WAAW,SAAS;AAClD,OAAK,cAAc,KAAK,QAAQ,WAAW,WAAW;AACtD,OAAK,UAAU,KAAK,QAA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GACX;;;;;CAMH,mBAAmB,GAAW,OAAuB;AACnD,SAAO,IAAI,IAAI,MAAM,MAAM,IAAI,MAAM,SAAS,KAAK;;;;;CAMrD,gBAAgB,OAAqB;AACnC,MAAI,QAAQ,EACV,OAAM,IAAI,MAAM,cAAc,MAAM,uBAAuB;AAG7D,MAAI,SAAS,KAAK,MAAM,OACtB,OAAM,IAAI,MACR,cAAc,MAAM,qBAAqB,KAAK,MAAM,SACrD;AAGH,MAAI,CAAC,KAAK,MAAM,OACd,OAAM,IAAI,MAAM,cAAc,MAAM,eAAe;AAGrD,SAAO,KAAK,MAAM;;;AAItB,yBAAe"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["defaultDiffConfig: DiffConfig","screenshotSettings: ScreenshotSettings","diffImagePath: string | undefined","result: ScreenshotCaptureDetails","baseResult: ScreenshotCaptureDetails","variantResults: ScreenshotVariantCaptureDetails[]","variantOptions: ScreenshotSettings","variantResult: ScreenshotVariantCaptureDetails"],"sources":["../src/config.ts","../src/filesystem.ts","../src/compare.ts","../src/screenshot.ts"],"sourcesContent":["import type { PossiblePromise, UserConfig } from \"./types\";\n\n/**\n * Type helper to make it easier to use cappa.config.ts, or a function that returns it. The function receives a ConfigEnv object.\n */\nexport function defineConfig(\n options:\n | PossiblePromise<UserConfig | Array<UserConfig>>\n | (() => PossiblePromise<UserConfig | Array<UserConfig>>),\n): typeof options {\n return options;\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\n\n/**\n * Class for interacting with the local file system to store the screenshots.\n */\nexport class ScreenshotFileSystem {\n private readonly actualDir: string;\n private readonly expectedDir: string;\n private readonly diffDir: string;\n\n constructor(outputDir: string) {\n this.actualDir = path.resolve(outputDir, \"actual\");\n this.expectedDir = path.resolve(outputDir, \"expected\");\n this.diffDir = path.resolve(outputDir, \"diff\");\n\n this.ensureParentDir(this.actualDir);\n this.ensureParentDir(this.expectedDir);\n this.ensureParentDir(this.diffDir);\n }\n\n clearActual() {\n this.removeDir(this.actualDir);\n }\n\n clearDiff() {\n this.removeDir(this.diffDir);\n }\n\n clearActualAndDiff() {\n this.clearActual();\n this.clearDiff();\n }\n\n /**\n * Approve a screenshot based on its actual file path.\n * @param actualFilePath - The path to the actual screenshot file.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n approveFromActualPath(actualFilePath: string) {\n const actualAbsolute = this.resolveActualPath(actualFilePath);\n const relativePath = path.relative(this.actualDir, actualAbsolute);\n\n if (relativePath.startsWith(\"..\")) {\n throw new Error(\n `Cannot approve screenshot outside of actual directory: ${actualFilePath}`,\n );\n }\n\n return this.approveRelative(relativePath);\n }\n\n /**\n * Approve a screenshot based on its name.\n * @param name - The name of the screenshot. Can include '.png' extension.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n approveByName(name: string) {\n const relativeWithExtension = name.endsWith(\".png\") ? name : `${name}.png`;\n\n return this.approveRelative(relativeWithExtension);\n }\n\n /**\n * Approve a screenshot based on a relative path.\n * @param relativePath - The relative path to the screenshot.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n private approveRelative(relativePath: string) {\n const actualPath = path.resolve(this.actualDir, relativePath);\n const expectedPath = path.resolve(this.expectedDir, relativePath);\n const diffPath = path.resolve(this.diffDir, relativePath);\n\n this.ensureParentDir(expectedPath);\n\n fs.copyFileSync(actualPath, expectedPath);\n\n if (fs.existsSync(diffPath)) {\n fs.unlinkSync(diffPath);\n }\n\n return {\n actualPath,\n expectedPath,\n diffPath,\n };\n }\n\n /**\n * Ensure the parent directory of a file exists. If not, it creates it.\n * @param filePath - The path to the file.\n */\n ensureParentDir(filePath: string) {\n const dir = path.dirname(filePath);\n\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n }\n\n /**\n * Remove a directory and all its contents.\n * @param dir - The path to the directory.\n */\n private removeDir(dir: string) {\n if (fs.existsSync(dir)) {\n fs.rmSync(dir, { recursive: true, force: true });\n }\n }\n\n /**\n * Get the actual directory path\n */\n getActualDir(): string {\n return this.actualDir;\n }\n\n /**\n * Get the expected directory path\n */\n getExpectedDir(): string {\n return this.expectedDir;\n }\n\n /**\n * Get the diff directory path\n */\n getDiffDir(): string {\n return this.diffDir;\n }\n\n /**\n * Get the path to an actual screenshot file\n */\n getActualFilePath(filename: string): string {\n return path.resolve(this.actualDir, filename);\n }\n\n /**\n * Get the path to an expected screenshot file\n */\n getExpectedFilePath(filename: string): string {\n return path.resolve(this.expectedDir, filename);\n }\n\n /**\n * Get the path to a diff screenshot file\n */\n getDiffFilePath(filename: string): string {\n return path.resolve(this.diffDir, filename);\n }\n\n /**\n * Write a file to the actual directory\n */\n writeActualFile(filename: string, data: Buffer): void {\n const filepath = this.getActualFilePath(filename);\n this.ensureParentDir(filepath);\n fs.writeFileSync(filepath, data);\n }\n\n /**\n * Write a file to the diff directory\n */\n writeDiffFile(filename: string, data: Buffer): void {\n const filepath = this.getDiffFilePath(filename);\n this.ensureParentDir(filepath);\n fs.writeFileSync(filepath, data);\n }\n\n /**\n * Check if an expected file exists\n */\n hasExpectedFile(filename: string): boolean {\n const expectedPath = this.getExpectedFilePath(filename);\n return fs.existsSync(expectedPath);\n }\n\n /**\n * Read an expected file\n */\n readExpectedFile(filename: string): Buffer {\n const expectedPath = this.getExpectedFilePath(filename);\n if (!fs.existsSync(expectedPath)) {\n throw new Error(`Expected image not found: ${expectedPath}`);\n }\n return fs.readFileSync(expectedPath);\n }\n\n /**\n * Resolve the actual path of a screenshot file.\n * @param filePath - The path to the screenshot file.\n * @returns The actual path of the screenshot file.\n */\n private resolveActualPath(filePath: string) {\n if (path.isAbsolute(filePath)) {\n return filePath;\n }\n\n return path.resolve(this.actualDir, filePath);\n }\n}\n","import fs from \"node:fs\";\nimport blazediff from \"@blazediff/core\";\nimport type { BlazeDiffOptions } from \"@blazediff/types\";\nimport { getLogger } from \"@cappa/logger\";\nimport { PNG } from \"pngjs\";\nimport type { DiffConfig } from \"./types\";\n\nexport type CompareOptions = BlazeDiffOptions;\n\nexport interface CompareResult {\n numDiffPixels: number;\n totalPixels: number;\n percentDifference: number;\n diffBuffer?: Buffer; // PNG buffer of the diff image\n passed: boolean; // Whether the comparison passed based on threshold\n error?: string; // Error message if the comparison failed\n differentSizes: boolean; // Whether the images have different sizes\n}\n\nconst compare = (\n image1: Uint8Array | Uint8ClampedArray,\n image2: Uint8Array | Uint8ClampedArray,\n diff: Uint8Array | Uint8ClampedArray | undefined,\n width: number,\n height: number,\n options?: BlazeDiffOptions,\n) => {\n return blazediff(image1, image2, diff, width, height, options);\n};\n\n/**\n * Compare two PNG images and return the difference\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param withDiff - Whether to create a diff image\n * @param options - Comparison options\n * @returns Comparison result\n */\nexport async function compareImages(\n image1: string | Buffer,\n image2: string | Buffer,\n withDiff: boolean = false,\n options: DiffConfig = {},\n): Promise<CompareResult> {\n const png1 = await loadPNG(image1);\n const png2 = await loadPNG(image2);\n\n const { width, height } = png1;\n\n if (width !== png2.width || height !== png2.height) {\n return {\n numDiffPixels: 0,\n totalPixels: 0,\n percentDifference: 0,\n passed: false,\n differentSizes: true,\n };\n }\n\n const diff = withDiff ? new PNG({ width, height }) : undefined;\n\n try {\n const numDiffPixels = compare(\n png1.data,\n png2.data,\n diff?.data,\n width,\n height,\n options,\n );\n\n const totalPixels = width * height;\n const percentDifference = (numDiffPixels / totalPixels) * 100;\n\n // Convert diff image to buffer\n const diffBuffer = diff ? PNG.sync.write(diff) : undefined;\n\n return {\n numDiffPixels,\n totalPixels,\n percentDifference,\n diffBuffer,\n passed: isPassed(percentDifference, numDiffPixels, options),\n differentSizes: false,\n };\n } catch (error) {\n getLogger().error((error as Error).message || \"Unknown error\");\n\n return {\n numDiffPixels: 0,\n totalPixels: 0,\n percentDifference: 0,\n passed: false,\n differentSizes: false,\n error: (error as Error).message || \"Unknown error\",\n };\n }\n}\n\n/**\n * Load a PNG image from file path or Buffer\n */\nasync function loadPNG(source: string | Buffer): Promise<PNG> {\n if (Buffer.isBuffer(source)) {\n return PNG.sync.read(source);\n } else {\n const buffer = fs.readFileSync(source);\n return PNG.sync.read(buffer);\n }\n}\n\n/**\n * Save a comparison result diff image to file\n * @param result - Comparison result containing diffBuffer\n * @param outputPath - Path to save the diff image\n */\nexport function saveDiffImage(result: CompareResult, outputPath: string): void {\n if (!result.diffBuffer) {\n throw new Error(\"No diff buffer available in comparison result\");\n }\n\n fs.writeFileSync(outputPath, result.diffBuffer);\n}\n\n/**\n * Quick comparison function that returns only pass/fail\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param options - Comparison options\n * @returns Whether the images match\n */\nexport async function imagesMatch(\n image1: string | Buffer,\n image2: string | Buffer,\n options: DiffConfig = {},\n): Promise<boolean> {\n const result = await compareImages(image1, image2, false, options);\n\n return result.passed;\n}\n\n/**\n * Check if the comparison passed based on the options\n * @param percentDifference - Percentage difference between the images\n * @param numDiffPixels - Number of different pixels\n * @param options - Comparison options\n * @returns Whether the comparison passed\n */\nconst isPassed = (\n percentDifference: number,\n numDiffPixels: number,\n options: DiffConfig,\n) => {\n if (options.maxDiffPercentage && options.maxDiffPixels) {\n return (\n percentDifference <= options.maxDiffPercentage &&\n numDiffPixels <= options.maxDiffPixels\n );\n }\n\n if (options.maxDiffPercentage) {\n return percentDifference <= options.maxDiffPercentage;\n }\n\n if (options.maxDiffPixels) {\n return numDiffPixels <= options.maxDiffPixels;\n }\n\n // fallback to 0 pixels\n return numDiffPixels === 0;\n};\n\n/**\n * Create different size png image with all pixels red\n * @returns\n */\nexport function createDiffSizePngImage(width: number, height: number): Buffer {\n const png = new PNG({ width, height });\n\n for (let y = 0; y < height; y++) {\n for (let x = 0; x < width; x++) {\n const idx = (width * y + x) << 2;\n png.data[idx] = 255; // red\n png.data[idx + 1] = 0; // green\n png.data[idx + 2] = 0; // blue\n png.data[idx + 3] = 255; // alpha\n }\n }\n\n return PNG.sync.write(png);\n}\n","import path from \"node:path\";\nimport { getLogger, type Logger } from \"@cappa/logger\";\nimport {\n type Browser,\n type BrowserContext,\n chromium,\n firefox,\n type Page,\n type PageScreenshotOptions,\n webkit,\n} from \"playwright-core\";\nimport {\n type CompareResult,\n compareImages,\n createDiffSizePngImage,\n} from \"./compare\";\nimport { ScreenshotFileSystem } from \"./filesystem\";\nimport type {\n DiffConfig,\n ScreenshotOptions,\n ScreenshotSettings,\n ScreenshotVariant,\n ScreenshotVariantWithUrl,\n} from \"./types\";\n\nconst defaultDiffConfig: DiffConfig = {\n threshold: 0.1,\n includeAA: false,\n fastBufferCheck: true,\n maxDiffPixels: 0,\n maxDiffPercentage: 0,\n};\n\nexport interface ScreenshotCaptureDetails {\n filename: string;\n filepath?: string;\n comparisonResult?: CompareResult;\n diffImagePath?: string;\n skipped?: boolean;\n}\n\nexport interface ScreenshotVariantCaptureDetails\n extends ScreenshotCaptureDetails {\n id: string;\n label?: string;\n}\n\nexport interface ScreenshotCaptureResult {\n base: ScreenshotCaptureDetails;\n variants: ScreenshotVariantCaptureDetails[];\n}\n\nexport interface ScreenshotCaptureExtras {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n variants?: Record<\n string,\n {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n }\n >;\n}\n\nclass ScreenshotTool {\n browserType: \"chromium\" | \"firefox\" | \"webkit\";\n headless: boolean;\n viewport: { width: number; height: number };\n outputDir: string;\n browser: Browser | null = null;\n context: BrowserContext | null = null;\n page: Page | null = null;\n concurrency: number;\n contexts: BrowserContext[] = [];\n pages: Page[] = [];\n diff: DiffConfig;\n logger: Logger;\n retries: number;\n filesystem: ScreenshotFileSystem;\n\n constructor(options: {\n browserType?: \"chromium\" | \"firefox\" | \"webkit\";\n headless?: boolean;\n viewport?: { width: number; height: number };\n outputDir?: string;\n diff?: DiffConfig;\n retries?: number;\n concurrency?: number;\n }) {\n this.browserType = options.browserType || \"chromium\";\n this.headless = options.headless !== false; // Default to headless\n this.viewport = options.viewport || { width: 1920, height: 1080 };\n this.outputDir = options.outputDir || \"./screenshots\";\n this.diff = { ...defaultDiffConfig, ...options.diff };\n this.concurrency = Math.max(1, options.concurrency || 1);\n\n this.logger = getLogger();\n this.retries = options.retries || 2;\n this.filesystem = new ScreenshotFileSystem(this.outputDir);\n }\n\n /**\n * Initialize playwright and page\n */\n async init() {\n // Launch browser based on type\n const browserMap = {\n chromium,\n firefox,\n webkit,\n };\n\n const browserClass = browserMap[this.browserType];\n if (!browserClass) {\n throw new Error(`Unsupported browser type: ${this.browserType}`);\n }\n\n this.browser = await browserClass.launch({\n headless: this.headless,\n });\n\n const defaultUserAgent = (await this.browser.newContext())\n .newPage()\n .then((page) => page.evaluate(() => navigator.userAgent));\n\n // Create N contexts and pages\n this.contexts = [];\n this.pages = [];\n\n for (let i = 0; i < this.concurrency; i++) {\n const context = await this.browser.newContext({\n reducedMotion: \"reduce\",\n deviceScaleFactor: 2,\n userAgent: `${await defaultUserAgent} CappaStorybook`,\n viewport: this.viewport,\n });\n const page = await context.newPage();\n\n this.contexts.push(context);\n this.pages.push(page);\n }\n\n // Backward compatibility\n this.context = this.contexts[0] ?? null;\n this.page = this.pages[0] ?? null;\n }\n\n /**\n * Closes browser\n * If not closed, the process will not exit\n */\n async close() {\n for (const context of this.contexts) {\n await context.close();\n }\n if (this.browser) {\n await this.browser.close();\n }\n }\n\n /**\n * Navigates to a URL with \"domcontentloaded\" waitUntil\n */\n async goTo(page: Page, url: string) {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n await page.goto(url, { waitUntil: \"domcontentloaded\" });\n }\n\n /**\n * Takes a screenshot of the page\n */\n async takeScreenshot(\n page: Page,\n filename: string,\n options: ScreenshotSettings,\n ) {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n if (options.skip) {\n return;\n }\n\n try {\n // Generate filename - save to actual directory\n const filepath = this.filesystem.getActualFilePath(filename);\n\n // Ensure parent directories exist before taking screenshot\n this.filesystem.ensureParentDir(filepath);\n\n // Take screenshot\n const screenshotOptions = {\n path: filepath,\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n animations: \"disabled\",\n caret: \"hide\",\n } satisfies PageScreenshotOptions;\n\n await this.withViewport(page, options.viewport, async () => {\n if (options.delay) {\n await page.waitForTimeout(options.delay);\n }\n\n await page.screenshot(screenshotOptions);\n });\n\n return filepath;\n } catch (error) {\n this.logger.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n /**\n * Sets the viewport size and executes the action, ensuring the original viewport is restored\n * even if the action throws an error.\n */\n private async withViewport<T>(\n page: Page,\n viewport: ScreenshotSettings[\"viewport\"],\n action: () => Promise<T>,\n ): Promise<T> {\n if (!viewport) {\n return action();\n }\n\n const previousViewport = page.viewportSize();\n\n // Check if the new viewport is the same as the current one\n if (\n previousViewport &&\n previousViewport.width === viewport.width &&\n previousViewport.height === viewport.height\n ) {\n return action();\n }\n\n await page.setViewportSize(viewport);\n\n try {\n return await action();\n } finally {\n // Always restore the original viewport, even if the action throws an error\n if (previousViewport) {\n await page.setViewportSize(previousViewport);\n }\n }\n }\n\n /**\n * Takes a screenshot and returns the buffer (for comparison purposes)\n */\n async takeScreenshotBuffer(\n page: Page,\n options: ScreenshotSettings,\n ): Promise<Buffer> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n if (options.skip) {\n throw new Error(\"Screenshot skipped\");\n }\n\n try {\n // Take screenshot without saving to file\n const screenshotOptions = {\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n animations: \"disabled\",\n caret: \"hide\",\n } satisfies PageScreenshotOptions;\n\n const buffer = await this.withViewport(\n page,\n options.viewport,\n async () => {\n if (options.delay) {\n await page.waitForTimeout(options.delay);\n }\n\n return page.screenshot(screenshotOptions);\n },\n );\n\n return buffer;\n } catch (error) {\n this.logger.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n async takeScreenshotWithComparison(\n page: Page,\n filename: string,\n referenceImage: Buffer,\n options: ScreenshotSettings & {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n },\n ): Promise<{\n screenshotPath: string;\n comparisonResult: CompareResult;\n diffImagePath?: string;\n }> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n try {\n // Generate filename and take screenshot - save to actual directory\n const filepath = this.filesystem.getActualFilePath(filename);\n\n const screenshotSettings: ScreenshotSettings = {\n fullPage: options.fullPage,\n mask: options.mask,\n omitBackground: options.omitBackground,\n delay: options.delay,\n viewport: options.viewport,\n };\n\n const retryScreenshot = await this.retryScreenshot(\n page,\n referenceImage,\n screenshotSettings,\n );\n\n if (!retryScreenshot.screenshotPath) {\n throw new Error(\"Screenshot buffer is undefined\");\n }\n\n // Save screenshot to actual directory\n this.filesystem.writeActualFile(filename, retryScreenshot.screenshotPath);\n this.logger.success(`Screenshot saved: ${filepath}`);\n\n if (retryScreenshot.passed) {\n this.logger.success(`Screenshot passed visual comparison`);\n } else {\n this.logger.error(`Screenshot failed visual comparison`);\n }\n\n let diffImagePath: string | undefined;\n\n // Save diff image if requested and there are differences\n if (\n options.saveDiffImage &&\n retryScreenshot.comparisonResult.diffBuffer &&\n !retryScreenshot.passed\n ) {\n const diffFilename = options.diffImageFilename || filename;\n // Save to diff directory\n diffImagePath = this.filesystem.getDiffFilePath(diffFilename);\n\n this.filesystem.writeDiffFile(\n diffFilename,\n retryScreenshot.comparisonResult.diffBuffer,\n );\n\n this.logger.debug(`Diff image saved: ${diffImagePath}`);\n }\n\n // different sizes diff image\n if (retryScreenshot.comparisonResult.differentSizes) {\n this.logger.warn(`Screenshot has different sizes than reference image`);\n\n const diffFilename = options.diffImageFilename || filename;\n diffImagePath = this.filesystem.getDiffFilePath(diffFilename);\n const diffBuffer = createDiffSizePngImage(200, 200);\n\n this.filesystem.writeDiffFile(diffFilename, diffBuffer);\n\n this.logger.debug(`Diff Size image saved: ${diffImagePath}`);\n }\n\n return {\n screenshotPath: filepath,\n comparisonResult: retryScreenshot.comparisonResult,\n diffImagePath,\n };\n } catch (error) {\n this.logger.error(\n `Error taking screenshot with comparison of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n async retryScreenshot(\n page: Page,\n referenceImage: Buffer,\n options: ScreenshotSettings,\n ) {\n for (let i = 0; i < this.retries; i++) {\n try {\n const screenshotBuffer = await this.takeScreenshotBuffer(page, {\n ...options,\n delay: this.getIncBackoffDelay(i, options.delay || 0),\n });\n\n if (!screenshotBuffer) {\n throw new Error(\"Screenshot buffer is undefined\");\n }\n\n const comparisonResult = await compareImages(\n screenshotBuffer,\n referenceImage,\n true,\n this.diff,\n );\n\n // bail early if the images are different sizes\n if (comparisonResult.differentSizes) {\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: false,\n };\n }\n\n // last retry\n if (i === this.retries - 1 && !comparisonResult.passed) {\n this.logger.error(\n `Failed to match screenshot after ${this.retries} retries`,\n );\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: false,\n };\n }\n\n if (comparisonResult.passed) {\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: true,\n };\n } else {\n this.logger.warn(\n `Comparison did not match. Retrying in ${this.getIncBackoffDelay(i + 1, options.delay || 0)}ms... \\nComparison result: ${comparisonResult.numDiffPixels} pixels different (${comparisonResult.percentDifference.toFixed(2)}%)`,\n );\n }\n } catch (error) {\n this.logger.error(\n `Error taking screenshot after ${i + 1} retries:`,\n (error as Error).message,\n );\n }\n }\n\n return {\n screenshotPath: null,\n comparisonResult: null,\n passed: false,\n };\n }\n\n getVariantFilename(filename: string, variant: ScreenshotVariant): string {\n if (variant.filename) {\n return variant.filename;\n }\n\n const parsed = path.parse(filename);\n const sanitizedId = variant.id\n .trim()\n .replace(/[^a-zA-Z0-9_-]+/g, \"-\")\n .replace(/-+/g, \"-\")\n .replace(/^-|-$/g, \"\");\n const suffix = sanitizedId || \"variant\";\n const ext = parsed.ext || \".png\";\n const variantName = `${parsed.name}--${suffix}${ext}`;\n\n return parsed.dir ? path.join(parsed.dir, variantName) : variantName;\n }\n\n /**\n * Captures a single screenshot with the given options\n */\n private async captureSingleScreenshot(\n page: Page,\n filename: string,\n options: ScreenshotSettings,\n extras: {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n } = {},\n ): Promise<ScreenshotCaptureDetails> {\n const result: ScreenshotCaptureDetails = { filename };\n\n if (options.skip) {\n result.skipped = true;\n return result;\n }\n\n const saveDiff = extras.saveDiffImage ?? false;\n const diffFilename = extras.diffImageFilename ?? filename;\n\n if (!this.filesystem.hasExpectedFile(filename)) {\n this.logger.debug(\n `Expected image not found for ${filename}, taking screenshot`,\n );\n\n const { screenshotPath, comparisonResult, diffImagePath } =\n await this.takeScreenshotWithComparison(\n page,\n filename,\n this.filesystem.readExpectedFile(filename),\n {\n ...options,\n saveDiffImage: saveDiff,\n diffImageFilename: diffFilename,\n },\n );\n\n result.filepath = screenshotPath;\n result.comparisonResult = comparisonResult;\n result.diffImagePath = diffImagePath;\n } else {\n result.filepath = await this.takeScreenshot(page, filename, options);\n }\n\n return result;\n }\n\n async capture(\n page: Page,\n filename: string,\n options: ScreenshotOptions,\n extras: ScreenshotCaptureExtras = {},\n ): Promise<ScreenshotCaptureResult> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n const { variants = [], ...baseOptions } = options;\n const baseResult: ScreenshotCaptureDetails = { filename };\n const variantResults: ScreenshotVariantCaptureDetails[] = [];\n\n const baseSaveDiff = extras.saveDiffImage ?? false;\n const baseDiffFilename = extras.diffImageFilename ?? filename;\n\n if (baseOptions.skip) {\n return {\n base: { ...baseResult, skipped: true },\n variants: variants.map((variant) => ({\n id: variant.id,\n label: variant.label,\n filename: this.getVariantFilename(filename, variant),\n skipped: true,\n })),\n };\n }\n\n // Capture base screenshot\n const baseCaptureResult = await this.captureSingleScreenshot(\n page,\n filename,\n baseOptions,\n {\n saveDiffImage: baseSaveDiff,\n diffImageFilename: baseDiffFilename,\n },\n );\n\n baseResult.filepath = baseCaptureResult.filepath;\n baseResult.comparisonResult = baseCaptureResult.comparisonResult;\n baseResult.diffImagePath = baseCaptureResult.diffImagePath;\n baseResult.skipped = baseCaptureResult.skipped;\n\n for (const variant of variants) {\n const variantFilename = this.getVariantFilename(filename, variant);\n const variantOptions: ScreenshotSettings = {\n ...baseOptions,\n ...variant.options,\n };\n\n const variantResult: ScreenshotVariantCaptureDetails = {\n id: variant.id,\n label: variant.label,\n filename: variantFilename,\n };\n\n if (variantOptions.skip) {\n variantResult.skipped = true;\n variantResults.push(variantResult);\n continue;\n }\n\n const variantExtra = extras.variants?.[variant.id];\n const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;\n const variantDiffFilename =\n variantExtra?.diffImageFilename ?? variantFilename;\n\n const variantCaptureResult = await this.captureSingleScreenshot(\n page,\n variantFilename,\n variantOptions,\n {\n saveDiffImage: variantSaveDiff,\n diffImageFilename: variantDiffFilename,\n },\n );\n\n variantResult.filepath = variantCaptureResult.filepath;\n variantResult.comparisonResult = variantCaptureResult.comparisonResult;\n variantResult.diffImagePath = variantCaptureResult.diffImagePath;\n variantResult.skipped = variantCaptureResult.skipped;\n\n variantResults.push(variantResult);\n }\n\n return {\n base: baseResult,\n variants: variantResults,\n };\n }\n\n /**\n * Applies SVG and UI optimizations for screenshots\n */\n private async applyScreenshotOptimizations(page: Page) {\n await page.addStyleTag({\n content: `\n *, *::before, *::after {\n -moz-animation: none !important;\n -moz-transition: none !important;\n transition: none !important; \n animation: none !important;\n }\n *, input, textarea { caret-color: transparent !important }\n *:focus { outline: none !important }\n *:focus-visible { outline: none !important }\n *:focus-within { outline: none !important }\n * { text-rendering: optimizeLegibility !important }\n ::-webkit-scrollbar { display: none !important }\n html { scroll-behavior: auto !important }\n svg, svg * {\n shape-rendering: crispEdges !important;\n text-rendering: geometricPrecision !important;\n vector-effect: non-scaling-stroke !important;\n }\n img { image-rendering: crisp-edges !important }\n `,\n });\n }\n\n /**\n * Captures screenshots for multiple variants, each with their own URL\n */\n async captureWithVariants(\n page: Page,\n baseFilename: string,\n baseUrl: string,\n baseOptions: ScreenshotSettings,\n variants: ScreenshotVariantWithUrl[],\n extras: ScreenshotCaptureExtras = {},\n ): Promise<ScreenshotCaptureResult> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n const baseResult: ScreenshotCaptureDetails = { filename: baseFilename };\n const variantResults: ScreenshotVariantCaptureDetails[] = [];\n\n const baseSaveDiff = extras.saveDiffImage ?? false;\n const baseDiffFilename = extras.diffImageFilename ?? baseFilename;\n\n // Capture base screenshot\n getLogger().debug(`Going to base URL: ${baseUrl}`);\n await page.goto(baseUrl);\n await this.applyScreenshotOptimizations(page);\n const baseCaptureResult = await this.captureSingleScreenshot(\n page,\n baseFilename,\n baseOptions,\n {\n saveDiffImage: baseSaveDiff,\n diffImageFilename: baseDiffFilename,\n },\n );\n\n baseResult.filepath = baseCaptureResult.filepath;\n baseResult.comparisonResult = baseCaptureResult.comparisonResult;\n baseResult.diffImagePath = baseCaptureResult.diffImagePath;\n baseResult.skipped = baseCaptureResult.skipped;\n\n // Capture variant screenshots\n for (const variant of variants) {\n const variantFilename =\n variant.filename || this.getVariantFilename(baseFilename, variant);\n const variantOptions: ScreenshotSettings = {\n ...baseOptions,\n ...variant.options,\n };\n\n const variantResult: ScreenshotVariantCaptureDetails = {\n id: variant.id,\n label: variant.label,\n filename: variantFilename,\n };\n\n if (variantOptions.skip) {\n variantResult.skipped = true;\n variantResults.push(variantResult);\n continue;\n }\n\n // Navigate to variant URL and apply optimizations\n getLogger().debug(`Going to variant URL: ${variant.url}`);\n await page.goto(variant.url);\n await this.applyScreenshotOptimizations(page);\n\n const variantExtra = extras.variants?.[variant.id];\n const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;\n const variantDiffFilename =\n variantExtra?.diffImageFilename ?? variantFilename;\n\n const variantCaptureResult = await this.captureSingleScreenshot(\n page,\n variantFilename,\n variantOptions,\n {\n saveDiffImage: variantSaveDiff,\n diffImageFilename: variantDiffFilename,\n },\n );\n\n variantResult.filepath = variantCaptureResult.filepath;\n variantResult.comparisonResult = variantCaptureResult.comparisonResult;\n variantResult.diffImagePath = variantCaptureResult.diffImagePath;\n variantResult.skipped = variantCaptureResult.skipped;\n\n variantResults.push(variantResult);\n }\n\n return {\n base: baseResult,\n variants: variantResults,\n };\n }\n\n /**\n * Gets the exponential backoff delay\n */\n getIncBackoffDelay(i: number, delay: number): number {\n return i > 0 ? 500 * 2 ** (i - 1) + (delay || 0) : 0;\n }\n\n /**\n * Get a page from the pool by index\n */\n getPageFromPool(index: number): Page {\n if (index < 0) {\n throw new Error(`Page index ${index} must be non-negative`);\n }\n\n if (index >= this.pages.length) {\n throw new Error(\n `Page index ${index} exceeds pool size ${this.pages.length}`,\n );\n }\n\n if (!this.pages[index]) {\n throw new Error(`Page index ${index} is undefined`);\n }\n\n return this.pages[index];\n }\n}\n\nexport default ScreenshotTool;\n"],"mappings":";;;;;;;;;;;AAKA,SAAgB,aACd,SAGgB;AAChB,QAAO;;;;;;;;ACJT,IAAa,uBAAb,MAAkC;CAChC,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,WAAmB;AAC7B,OAAK,YAAY,KAAK,QAAQ,WAAW,SAAS;AAClD,OAAK,cAAc,KAAK,QAAQ,WAAW,WAAW;AACtD,OAAK,UAAU,KAAK,QAAQ,WAAW,OAAO;AAE9C,OAAK,gBAAgB,KAAK,UAAU;AACpC,OAAK,gBAAgB,KAAK,YAAY;AACtC,OAAK,gBAAgB,KAAK,QAAQ;;CAGpC,cAAc;AACZ,OAAK,UAAU,KAAK,UAAU;;CAGhC,YAAY;AACV,OAAK,UAAU,KAAK,QAAQ;;CAG9B,qBAAqB;AACnB,OAAK,aAAa;AAClB,OAAK,WAAW;;;;;;;CAQl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M,eAAe,OAAO,WAAW,QAAQ;GAC/C,MAAM,kBAAkB,cAAc,iBAAiB;GACvD,MAAM,sBACJ,cAAc,qBAAqB;GAErC,MAAM,uBAAuB,MAAM,KAAK,wBACtC,MACA,iBACA,gBACA;IACE,eAAe;IACf,mBAAmB;IACpB,CACF;AAED,iBAAc,WAAW,qBAAqB;AAC9C,iBAAc,mBAAmB,qBAAqB;AACtD,iBAAc,gBAAgB,qBAAqB;AACnD,iBAAc,UAAU,qBAAqB;AAE7C,kBAAe,KAAK,cAAc;;AAGpC,SAAO;GACL,MAAM;GACN,UAAU;GACX;;;;;CAMH,mBAAmB,GAAW,OAAuB;AACnD,SAAO,IAAI,IAAI,MAAM,MAAM,IAAI,MAAM,SAAS,KAAK;;;;;CAMrD,gBAAgB,OAAqB;AACnC,MAAI,QAAQ,EACV,OAAM,IAAI,MAAM,cAAc,MAAM,uBAAuB;AAG7D,MAAI,SAAS,KAAK,MAAM,OACtB,OAAM,IAAI,MACR,cAAc,MAAM,qBAAqB,KAAK,MAAM,SACrD;AAGH,MAAI,CAAC,KAAK,MAAM,OACd,OAAM,IAAI,MAAM,cAAc,MAAM,eAAe;AAGrD,SAAO,KAAK,MAAM;;;AAItB,yBAAe"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cappa/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Core Playwright screenshot functionality for Cappa",
|
|
5
5
|
"author": "Tim Richter",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"@blazediff/types": "^1.4.1",
|
|
23
23
|
"playwright-core": "^1.56.0",
|
|
24
24
|
"pngjs": "^7.0.0",
|
|
25
|
-
"@cappa/logger": "0.0.
|
|
25
|
+
"@cappa/logger": "0.0.4"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@types/node": "24.8.0",
|
package/src/filesystem.ts
CHANGED
|
@@ -90,7 +90,7 @@ export class ScreenshotFileSystem {
|
|
|
90
90
|
* Ensure the parent directory of a file exists. If not, it creates it.
|
|
91
91
|
* @param filePath - The path to the file.
|
|
92
92
|
*/
|
|
93
|
-
|
|
93
|
+
ensureParentDir(filePath: string) {
|
|
94
94
|
const dir = path.dirname(filePath);
|
|
95
95
|
|
|
96
96
|
if (!fs.existsSync(dir)) {
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
ScreenshotOptions,
|
|
13
13
|
ScreenshotSettings,
|
|
14
14
|
ScreenshotVariant,
|
|
15
|
+
ScreenshotVariantWithUrl,
|
|
15
16
|
UserConfig,
|
|
16
17
|
Viewport,
|
|
17
18
|
} from "./types";
|
|
@@ -27,6 +28,7 @@ export {
|
|
|
27
28
|
type ScreenshotOptions,
|
|
28
29
|
type ScreenshotSettings,
|
|
29
30
|
type ScreenshotVariant,
|
|
31
|
+
type ScreenshotVariantWithUrl,
|
|
30
32
|
type Screenshot,
|
|
31
33
|
type ScreenshotCaptureResult,
|
|
32
34
|
type ScreenshotCaptureDetails,
|
package/src/screenshot.ts
CHANGED
|
@@ -20,6 +20,7 @@ import type {
|
|
|
20
20
|
ScreenshotOptions,
|
|
21
21
|
ScreenshotSettings,
|
|
22
22
|
ScreenshotVariant,
|
|
23
|
+
ScreenshotVariantWithUrl,
|
|
23
24
|
} from "./types";
|
|
24
25
|
|
|
25
26
|
const defaultDiffConfig: DiffConfig = {
|
|
@@ -187,6 +188,9 @@ class ScreenshotTool {
|
|
|
187
188
|
// Generate filename - save to actual directory
|
|
188
189
|
const filepath = this.filesystem.getActualFilePath(filename);
|
|
189
190
|
|
|
191
|
+
// Ensure parent directories exist before taking screenshot
|
|
192
|
+
this.filesystem.ensureParentDir(filepath);
|
|
193
|
+
|
|
190
194
|
// Take screenshot
|
|
191
195
|
const screenshotOptions = {
|
|
192
196
|
path: filepath,
|
|
@@ -488,6 +492,55 @@ class ScreenshotTool {
|
|
|
488
492
|
return parsed.dir ? path.join(parsed.dir, variantName) : variantName;
|
|
489
493
|
}
|
|
490
494
|
|
|
495
|
+
/**
|
|
496
|
+
* Captures a single screenshot with the given options
|
|
497
|
+
*/
|
|
498
|
+
private async captureSingleScreenshot(
|
|
499
|
+
page: Page,
|
|
500
|
+
filename: string,
|
|
501
|
+
options: ScreenshotSettings,
|
|
502
|
+
extras: {
|
|
503
|
+
saveDiffImage?: boolean;
|
|
504
|
+
diffImageFilename?: string;
|
|
505
|
+
} = {},
|
|
506
|
+
): Promise<ScreenshotCaptureDetails> {
|
|
507
|
+
const result: ScreenshotCaptureDetails = { filename };
|
|
508
|
+
|
|
509
|
+
if (options.skip) {
|
|
510
|
+
result.skipped = true;
|
|
511
|
+
return result;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
const saveDiff = extras.saveDiffImage ?? false;
|
|
515
|
+
const diffFilename = extras.diffImageFilename ?? filename;
|
|
516
|
+
|
|
517
|
+
if (!this.filesystem.hasExpectedFile(filename)) {
|
|
518
|
+
this.logger.debug(
|
|
519
|
+
`Expected image not found for ${filename}, taking screenshot`,
|
|
520
|
+
);
|
|
521
|
+
|
|
522
|
+
const { screenshotPath, comparisonResult, diffImagePath } =
|
|
523
|
+
await this.takeScreenshotWithComparison(
|
|
524
|
+
page,
|
|
525
|
+
filename,
|
|
526
|
+
this.filesystem.readExpectedFile(filename),
|
|
527
|
+
{
|
|
528
|
+
...options,
|
|
529
|
+
saveDiffImage: saveDiff,
|
|
530
|
+
diffImageFilename: diffFilename,
|
|
531
|
+
},
|
|
532
|
+
);
|
|
533
|
+
|
|
534
|
+
result.filepath = screenshotPath;
|
|
535
|
+
result.comparisonResult = comparisonResult;
|
|
536
|
+
result.diffImagePath = diffImagePath;
|
|
537
|
+
} else {
|
|
538
|
+
result.filepath = await this.takeScreenshot(page, filename, options);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
return result;
|
|
542
|
+
}
|
|
543
|
+
|
|
491
544
|
async capture(
|
|
492
545
|
page: Page,
|
|
493
546
|
filename: string,
|
|
@@ -517,32 +570,143 @@ class ScreenshotTool {
|
|
|
517
570
|
};
|
|
518
571
|
}
|
|
519
572
|
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
573
|
+
// Capture base screenshot
|
|
574
|
+
const baseCaptureResult = await this.captureSingleScreenshot(
|
|
575
|
+
page,
|
|
576
|
+
filename,
|
|
577
|
+
baseOptions,
|
|
578
|
+
{
|
|
579
|
+
saveDiffImage: baseSaveDiff,
|
|
580
|
+
diffImageFilename: baseDiffFilename,
|
|
581
|
+
},
|
|
582
|
+
);
|
|
583
|
+
|
|
584
|
+
baseResult.filepath = baseCaptureResult.filepath;
|
|
585
|
+
baseResult.comparisonResult = baseCaptureResult.comparisonResult;
|
|
586
|
+
baseResult.diffImagePath = baseCaptureResult.diffImagePath;
|
|
587
|
+
baseResult.skipped = baseCaptureResult.skipped;
|
|
532
588
|
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
589
|
+
for (const variant of variants) {
|
|
590
|
+
const variantFilename = this.getVariantFilename(filename, variant);
|
|
591
|
+
const variantOptions: ScreenshotSettings = {
|
|
592
|
+
...baseOptions,
|
|
593
|
+
...variant.options,
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
const variantResult: ScreenshotVariantCaptureDetails = {
|
|
597
|
+
id: variant.id,
|
|
598
|
+
label: variant.label,
|
|
599
|
+
filename: variantFilename,
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
if (variantOptions.skip) {
|
|
603
|
+
variantResult.skipped = true;
|
|
604
|
+
variantResults.push(variantResult);
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
const variantExtra = extras.variants?.[variant.id];
|
|
609
|
+
const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;
|
|
610
|
+
const variantDiffFilename =
|
|
611
|
+
variantExtra?.diffImageFilename ?? variantFilename;
|
|
612
|
+
|
|
613
|
+
const variantCaptureResult = await this.captureSingleScreenshot(
|
|
538
614
|
page,
|
|
539
|
-
|
|
540
|
-
|
|
615
|
+
variantFilename,
|
|
616
|
+
variantOptions,
|
|
617
|
+
{
|
|
618
|
+
saveDiffImage: variantSaveDiff,
|
|
619
|
+
diffImageFilename: variantDiffFilename,
|
|
620
|
+
},
|
|
541
621
|
);
|
|
622
|
+
|
|
623
|
+
variantResult.filepath = variantCaptureResult.filepath;
|
|
624
|
+
variantResult.comparisonResult = variantCaptureResult.comparisonResult;
|
|
625
|
+
variantResult.diffImagePath = variantCaptureResult.diffImagePath;
|
|
626
|
+
variantResult.skipped = variantCaptureResult.skipped;
|
|
627
|
+
|
|
628
|
+
variantResults.push(variantResult);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
return {
|
|
632
|
+
base: baseResult,
|
|
633
|
+
variants: variantResults,
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Applies SVG and UI optimizations for screenshots
|
|
639
|
+
*/
|
|
640
|
+
private async applyScreenshotOptimizations(page: Page) {
|
|
641
|
+
await page.addStyleTag({
|
|
642
|
+
content: `
|
|
643
|
+
*, *::before, *::after {
|
|
644
|
+
-moz-animation: none !important;
|
|
645
|
+
-moz-transition: none !important;
|
|
646
|
+
transition: none !important;
|
|
647
|
+
animation: none !important;
|
|
648
|
+
}
|
|
649
|
+
*, input, textarea { caret-color: transparent !important }
|
|
650
|
+
*:focus { outline: none !important }
|
|
651
|
+
*:focus-visible { outline: none !important }
|
|
652
|
+
*:focus-within { outline: none !important }
|
|
653
|
+
* { text-rendering: optimizeLegibility !important }
|
|
654
|
+
::-webkit-scrollbar { display: none !important }
|
|
655
|
+
html { scroll-behavior: auto !important }
|
|
656
|
+
svg, svg * {
|
|
657
|
+
shape-rendering: crispEdges !important;
|
|
658
|
+
text-rendering: geometricPrecision !important;
|
|
659
|
+
vector-effect: non-scaling-stroke !important;
|
|
660
|
+
}
|
|
661
|
+
img { image-rendering: crisp-edges !important }
|
|
662
|
+
`,
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* Captures screenshots for multiple variants, each with their own URL
|
|
668
|
+
*/
|
|
669
|
+
async captureWithVariants(
|
|
670
|
+
page: Page,
|
|
671
|
+
baseFilename: string,
|
|
672
|
+
baseUrl: string,
|
|
673
|
+
baseOptions: ScreenshotSettings,
|
|
674
|
+
variants: ScreenshotVariantWithUrl[],
|
|
675
|
+
extras: ScreenshotCaptureExtras = {},
|
|
676
|
+
): Promise<ScreenshotCaptureResult> {
|
|
677
|
+
if (!this.browser) {
|
|
678
|
+
throw new Error("Browser not initialized");
|
|
542
679
|
}
|
|
543
680
|
|
|
681
|
+
const baseResult: ScreenshotCaptureDetails = { filename: baseFilename };
|
|
682
|
+
const variantResults: ScreenshotVariantCaptureDetails[] = [];
|
|
683
|
+
|
|
684
|
+
const baseSaveDiff = extras.saveDiffImage ?? false;
|
|
685
|
+
const baseDiffFilename = extras.diffImageFilename ?? baseFilename;
|
|
686
|
+
|
|
687
|
+
// Capture base screenshot
|
|
688
|
+
getLogger().debug(`Going to base URL: ${baseUrl}`);
|
|
689
|
+
await page.goto(baseUrl);
|
|
690
|
+
await this.applyScreenshotOptimizations(page);
|
|
691
|
+
const baseCaptureResult = await this.captureSingleScreenshot(
|
|
692
|
+
page,
|
|
693
|
+
baseFilename,
|
|
694
|
+
baseOptions,
|
|
695
|
+
{
|
|
696
|
+
saveDiffImage: baseSaveDiff,
|
|
697
|
+
diffImageFilename: baseDiffFilename,
|
|
698
|
+
},
|
|
699
|
+
);
|
|
700
|
+
|
|
701
|
+
baseResult.filepath = baseCaptureResult.filepath;
|
|
702
|
+
baseResult.comparisonResult = baseCaptureResult.comparisonResult;
|
|
703
|
+
baseResult.diffImagePath = baseCaptureResult.diffImagePath;
|
|
704
|
+
baseResult.skipped = baseCaptureResult.skipped;
|
|
705
|
+
|
|
706
|
+
// Capture variant screenshots
|
|
544
707
|
for (const variant of variants) {
|
|
545
|
-
const variantFilename =
|
|
708
|
+
const variantFilename =
|
|
709
|
+
variant.filename || this.getVariantFilename(baseFilename, variant);
|
|
546
710
|
const variantOptions: ScreenshotSettings = {
|
|
547
711
|
...baseOptions,
|
|
548
712
|
...variant.options,
|
|
@@ -560,34 +724,30 @@ class ScreenshotTool {
|
|
|
560
724
|
continue;
|
|
561
725
|
}
|
|
562
726
|
|
|
727
|
+
// Navigate to variant URL and apply optimizations
|
|
728
|
+
getLogger().debug(`Going to variant URL: ${variant.url}`);
|
|
729
|
+
await page.goto(variant.url);
|
|
730
|
+
await this.applyScreenshotOptimizations(page);
|
|
731
|
+
|
|
563
732
|
const variantExtra = extras.variants?.[variant.id];
|
|
564
733
|
const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;
|
|
565
734
|
const variantDiffFilename =
|
|
566
735
|
variantExtra?.diffImageFilename ?? variantFilename;
|
|
567
736
|
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
diffImageFilename: variantDiffFilename,
|
|
578
|
-
},
|
|
579
|
-
);
|
|
737
|
+
const variantCaptureResult = await this.captureSingleScreenshot(
|
|
738
|
+
page,
|
|
739
|
+
variantFilename,
|
|
740
|
+
variantOptions,
|
|
741
|
+
{
|
|
742
|
+
saveDiffImage: variantSaveDiff,
|
|
743
|
+
diffImageFilename: variantDiffFilename,
|
|
744
|
+
},
|
|
745
|
+
);
|
|
580
746
|
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
variantResult.filepath = await this.takeScreenshot(
|
|
586
|
-
page,
|
|
587
|
-
variantFilename,
|
|
588
|
-
variantOptions,
|
|
589
|
-
);
|
|
590
|
-
}
|
|
747
|
+
variantResult.filepath = variantCaptureResult.filepath;
|
|
748
|
+
variantResult.comparisonResult = variantCaptureResult.comparisonResult;
|
|
749
|
+
variantResult.diffImagePath = variantCaptureResult.diffImagePath;
|
|
750
|
+
variantResult.skipped = variantCaptureResult.skipped;
|
|
591
751
|
|
|
592
752
|
variantResults.push(variantResult);
|
|
593
753
|
}
|
package/src/types.ts
CHANGED
|
@@ -99,6 +99,14 @@ export type ScreenshotVariant = {
|
|
|
99
99
|
options?: ScreenshotSettings;
|
|
100
100
|
};
|
|
101
101
|
|
|
102
|
+
export type ScreenshotVariantWithUrl = {
|
|
103
|
+
id: string;
|
|
104
|
+
label?: string;
|
|
105
|
+
filename?: string;
|
|
106
|
+
url: string;
|
|
107
|
+
options?: ScreenshotSettings;
|
|
108
|
+
};
|
|
109
|
+
|
|
102
110
|
export type ScreenshotOptions = ScreenshotSettings & {
|
|
103
111
|
variants?: ScreenshotVariant[];
|
|
104
112
|
};
|