@cappa/core 0.6.3 → 0.7.1
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/dist/index.cjs +81 -63
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +27 -5
- package/dist/index.d.mts +27 -7
- package/dist/index.mjs +74 -52
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import gmsd from "@blazediff/gmsd";
|
|
3
3
|
import { getLogger } from "@cappa/logger";
|
|
4
|
-
import { glob, readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import fsp, { glob, readFile, writeFile } from "node:fs/promises";
|
|
5
5
|
import { PNG } from "pngjs";
|
|
6
6
|
import blazediff from "@blazediff/core";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { chromium, firefox, webkit } from "playwright-core";
|
|
9
|
-
|
|
10
9
|
//#region src/features/png/util.ts
|
|
11
10
|
const pngSignature = Buffer.from([
|
|
12
11
|
137,
|
|
@@ -119,7 +118,6 @@ function extractTextMetadata(buffer) {
|
|
|
119
118
|
}
|
|
120
119
|
return metadata;
|
|
121
120
|
}
|
|
122
|
-
|
|
123
121
|
//#endregion
|
|
124
122
|
//#region src/features/png/png.ts
|
|
125
123
|
/**
|
|
@@ -186,7 +184,6 @@ var PNG$1 = class PNG$1 {
|
|
|
186
184
|
await writeFile(targetPath, this.toBuffer(options));
|
|
187
185
|
}
|
|
188
186
|
};
|
|
189
|
-
|
|
190
187
|
//#endregion
|
|
191
188
|
//#region src/compare/gmsd.ts
|
|
192
189
|
const compare$1 = (image1, image2, diff, width, height, options) => {
|
|
@@ -201,8 +198,7 @@ const compare$1 = (image1, image2, diff, width, height, options) => {
|
|
|
201
198
|
* @returns Comparison result
|
|
202
199
|
*/
|
|
203
200
|
async function compareImagesGMSD(image1, image2, withDiff = false, options = {}) {
|
|
204
|
-
const png1 = await loadPNG$1(image1);
|
|
205
|
-
const png2 = await loadPNG$1(image2);
|
|
201
|
+
const [png1, png2] = await Promise.all([loadPNG$1(image1), loadPNG$1(image2)]);
|
|
206
202
|
const { width, height } = png1;
|
|
207
203
|
if (width !== png2.width || height !== png2.height) return {
|
|
208
204
|
gmsd: Infinity,
|
|
@@ -252,7 +248,6 @@ function annotateDiffImage$1(image, options) {
|
|
|
252
248
|
async function imagesMatchGMSD(image1, image2, options = {}) {
|
|
253
249
|
return (await compareImagesGMSD(image1, image2, false, options)).passed;
|
|
254
250
|
}
|
|
255
|
-
|
|
256
251
|
//#endregion
|
|
257
252
|
//#region src/compare/pixel.ts
|
|
258
253
|
const compare = (image1, image2, diff, width, height, options) => {
|
|
@@ -267,8 +262,7 @@ const compare = (image1, image2, diff, width, height, options) => {
|
|
|
267
262
|
* @returns Comparison result
|
|
268
263
|
*/
|
|
269
264
|
async function compareImages(image1, image2, withDiff = false, options = {}) {
|
|
270
|
-
const png1 = await loadPNG(image1);
|
|
271
|
-
const png2 = await loadPNG(image2);
|
|
265
|
+
const [png1, png2] = await Promise.all([loadPNG(image1), loadPNG(image2)]);
|
|
272
266
|
const { width, height } = png1;
|
|
273
267
|
if (width !== png2.width || height !== png2.height) return {
|
|
274
268
|
numDiffPixels: 0,
|
|
@@ -354,7 +348,6 @@ function createDiffSizePngImage(width, height) {
|
|
|
354
348
|
}
|
|
355
349
|
return png.toBuffer();
|
|
356
350
|
}
|
|
357
|
-
|
|
358
351
|
//#endregion
|
|
359
352
|
//#region src/config.ts
|
|
360
353
|
/**
|
|
@@ -363,7 +356,6 @@ function createDiffSizePngImage(width, height) {
|
|
|
363
356
|
function defineConfig(options) {
|
|
364
357
|
return options;
|
|
365
358
|
}
|
|
366
|
-
|
|
367
359
|
//#endregion
|
|
368
360
|
//#region src/filesystem.ts
|
|
369
361
|
/**
|
|
@@ -377,9 +369,9 @@ var ScreenshotFileSystem = class {
|
|
|
377
369
|
this.actual = path.resolve(outputDir, "actual");
|
|
378
370
|
this.expected = path.resolve(outputDir, "expected");
|
|
379
371
|
this.diff = path.resolve(outputDir, "diff");
|
|
380
|
-
this.
|
|
381
|
-
this.
|
|
382
|
-
this.
|
|
372
|
+
this.ensureParentDirSync(this.actual);
|
|
373
|
+
this.ensureParentDirSync(this.expected);
|
|
374
|
+
this.ensureParentDirSync(this.diff);
|
|
383
375
|
}
|
|
384
376
|
clearActual() {
|
|
385
377
|
this.removeDir(this.actual);
|
|
@@ -429,7 +421,7 @@ var ScreenshotFileSystem = class {
|
|
|
429
421
|
const actualPath = path.resolve(this.actual, relativePath);
|
|
430
422
|
const expectedPath = path.resolve(this.expected, relativePath);
|
|
431
423
|
const diffPath = path.resolve(this.diff, relativePath);
|
|
432
|
-
this.
|
|
424
|
+
this.ensureParentDirSync(expectedPath);
|
|
433
425
|
fs.copyFileSync(actualPath, expectedPath);
|
|
434
426
|
if (fs.existsSync(diffPath)) {
|
|
435
427
|
this.copyDiffMetadataToExpected(diffPath, expectedPath);
|
|
@@ -460,7 +452,14 @@ var ScreenshotFileSystem = class {
|
|
|
460
452
|
* Ensure the parent directory of a file exists. If not, it creates it.
|
|
461
453
|
* @param filePath - The path to the file.
|
|
462
454
|
*/
|
|
463
|
-
ensureParentDir(filePath) {
|
|
455
|
+
async ensureParentDir(filePath) {
|
|
456
|
+
const dir = path.dirname(filePath);
|
|
457
|
+
await fsp.mkdir(dir, { recursive: true });
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Synchronous version used only during constructor initialization.
|
|
461
|
+
*/
|
|
462
|
+
ensureParentDirSync(filePath) {
|
|
464
463
|
const dir = path.dirname(filePath);
|
|
465
464
|
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
466
465
|
}
|
|
@@ -513,33 +512,41 @@ var ScreenshotFileSystem = class {
|
|
|
513
512
|
/**
|
|
514
513
|
* Write a file to the actual directory
|
|
515
514
|
*/
|
|
516
|
-
writeActualFile(filename, data) {
|
|
515
|
+
async writeActualFile(filename, data) {
|
|
517
516
|
const filepath = this.getActualFilePath(filename);
|
|
518
|
-
this.ensureParentDir(filepath);
|
|
519
|
-
|
|
517
|
+
await this.ensureParentDir(filepath);
|
|
518
|
+
await fsp.writeFile(filepath, data);
|
|
520
519
|
}
|
|
521
520
|
/**
|
|
522
521
|
* Write a file to the diff directory
|
|
523
522
|
*/
|
|
524
|
-
writeDiffFile(filename, data) {
|
|
523
|
+
async writeDiffFile(filename, data) {
|
|
525
524
|
const filepath = this.getDiffFilePath(filename);
|
|
526
|
-
this.ensureParentDir(filepath);
|
|
527
|
-
|
|
525
|
+
await this.ensureParentDir(filepath);
|
|
526
|
+
await fsp.writeFile(filepath, data);
|
|
528
527
|
}
|
|
529
528
|
/**
|
|
530
529
|
* Check if an expected file exists
|
|
531
530
|
*/
|
|
532
|
-
hasExpectedFile(filename) {
|
|
531
|
+
async hasExpectedFile(filename) {
|
|
533
532
|
const expectedPath = this.getExpectedFilePath(filename);
|
|
534
|
-
|
|
533
|
+
try {
|
|
534
|
+
await fsp.access(expectedPath);
|
|
535
|
+
return true;
|
|
536
|
+
} catch {
|
|
537
|
+
return false;
|
|
538
|
+
}
|
|
535
539
|
}
|
|
536
540
|
/**
|
|
537
541
|
* Read an expected file
|
|
538
542
|
*/
|
|
539
|
-
readExpectedFile(filename) {
|
|
543
|
+
async readExpectedFile(filename) {
|
|
540
544
|
const expectedPath = this.getExpectedFilePath(filename);
|
|
541
|
-
|
|
542
|
-
|
|
545
|
+
try {
|
|
546
|
+
return await fsp.readFile(expectedPath);
|
|
547
|
+
} catch {
|
|
548
|
+
throw new Error(`Expected image not found: ${expectedPath}`);
|
|
549
|
+
}
|
|
543
550
|
}
|
|
544
551
|
/**
|
|
545
552
|
* Resolve the actual path of a screenshot file.
|
|
@@ -556,7 +563,6 @@ var ScreenshotFileSystem = class {
|
|
|
556
563
|
return relativePath;
|
|
557
564
|
}
|
|
558
565
|
};
|
|
559
|
-
|
|
560
566
|
//#endregion
|
|
561
567
|
//#region src/screenshot.ts
|
|
562
568
|
const defaultPixelDiffConfig = {
|
|
@@ -642,20 +648,23 @@ var ScreenshotTool = class {
|
|
|
642
648
|
}[this.browserType];
|
|
643
649
|
if (!browserClass) throw new Error(`Unsupported browser type: ${this.browserType}`);
|
|
644
650
|
this.browser = await browserClass.launch({ headless: this.headless });
|
|
645
|
-
const
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
651
|
+
const tempCtx = await this.browser.newContext();
|
|
652
|
+
const defaultUserAgent = await (await tempCtx.newPage()).evaluate(() => navigator.userAgent);
|
|
653
|
+
await tempCtx.close();
|
|
654
|
+
const results = await Promise.all(Array.from({ length: this.concurrency }, async () => {
|
|
649
655
|
const context = await this.browser.newContext({
|
|
650
656
|
reducedMotion: "reduce",
|
|
651
657
|
deviceScaleFactor: 2,
|
|
652
|
-
userAgent: `${
|
|
658
|
+
userAgent: `${defaultUserAgent} CappaStorybook`,
|
|
653
659
|
viewport: this.viewport
|
|
654
660
|
});
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
661
|
+
return {
|
|
662
|
+
context,
|
|
663
|
+
page: await context.newPage()
|
|
664
|
+
};
|
|
665
|
+
}));
|
|
666
|
+
this.contexts = results.map((r) => r.context);
|
|
667
|
+
this.pages = results.map((r) => r.page);
|
|
659
668
|
this.context = this.contexts[0] ?? null;
|
|
660
669
|
this.page = this.pages[0] ?? null;
|
|
661
670
|
}
|
|
@@ -682,7 +691,7 @@ var ScreenshotTool = class {
|
|
|
682
691
|
if (options.skip) return;
|
|
683
692
|
try {
|
|
684
693
|
const filepath = this.filesystem.getActualFilePath(filename);
|
|
685
|
-
this.filesystem.ensureParentDir(filepath);
|
|
694
|
+
await this.filesystem.ensureParentDir(filepath);
|
|
686
695
|
const screenshotOptions = {
|
|
687
696
|
path: filepath,
|
|
688
697
|
fullPage: options.fullPage ?? this.fullPage,
|
|
@@ -765,23 +774,23 @@ var ScreenshotTool = class {
|
|
|
765
774
|
};
|
|
766
775
|
const retryScreenshot = await this.retryScreenshot(page, referenceImage, screenshotSettings, diffOverride);
|
|
767
776
|
if (!retryScreenshot.screenshotPath) throw new Error("Screenshot buffer is undefined");
|
|
768
|
-
this.filesystem.writeActualFile(filename, retryScreenshot.screenshotPath);
|
|
777
|
+
await this.filesystem.writeActualFile(filename, retryScreenshot.screenshotPath);
|
|
769
778
|
this.logger.debug(`Actual screenshot saved: ${filepath}`);
|
|
770
779
|
if (retryScreenshot.passed) this.logger.success(`Screenshot passed visual comparison`);
|
|
771
780
|
else this.logger.error(`Screenshot failed visual comparison`);
|
|
772
781
|
let diffImagePath;
|
|
773
|
-
if (options.saveDiffImage && retryScreenshot.comparisonResult
|
|
782
|
+
if (options.saveDiffImage && retryScreenshot.comparisonResult?.diffBuffer && !retryScreenshot.passed) {
|
|
774
783
|
const diffFilename = options.diffImageFilename || filename;
|
|
775
784
|
diffImagePath = this.filesystem.getDiffFilePath(diffFilename);
|
|
776
|
-
this.filesystem.writeDiffFile(diffFilename, retryScreenshot.comparisonResult.diffBuffer);
|
|
785
|
+
await this.filesystem.writeDiffFile(diffFilename, retryScreenshot.comparisonResult.diffBuffer);
|
|
777
786
|
this.logger.debug(`Diff image saved: ${diffImagePath}`);
|
|
778
787
|
}
|
|
779
|
-
if (typeof retryScreenshot.comparisonResult === "object" && "differentSizes" in retryScreenshot.comparisonResult && retryScreenshot.comparisonResult.differentSizes) {
|
|
788
|
+
if (retryScreenshot.comparisonResult !== null && typeof retryScreenshot.comparisonResult === "object" && "differentSizes" in retryScreenshot.comparisonResult && retryScreenshot.comparisonResult.differentSizes) {
|
|
780
789
|
this.logger.warn(`Screenshot has different sizes than reference image`);
|
|
781
790
|
const diffFilename = options.diffImageFilename || filename;
|
|
782
791
|
diffImagePath = this.filesystem.getDiffFilePath(diffFilename);
|
|
783
792
|
const diffBuffer = createDiffSizePngImage(200, 200);
|
|
784
|
-
this.filesystem.writeDiffFile(diffFilename, diffBuffer);
|
|
793
|
+
await this.filesystem.writeDiffFile(diffFilename, diffBuffer);
|
|
785
794
|
this.logger.debug(`Diff Size image saved: ${diffImagePath}`);
|
|
786
795
|
}
|
|
787
796
|
return {
|
|
@@ -846,14 +855,15 @@ var ScreenshotTool = class {
|
|
|
846
855
|
result.skipped = true;
|
|
847
856
|
return result;
|
|
848
857
|
}
|
|
858
|
+
const start = extras.captureStart ?? performance.now();
|
|
849
859
|
const saveDiff = extras.saveDiffImage ?? false;
|
|
850
860
|
const diffFilename = extras.diffImageFilename ?? filename;
|
|
851
861
|
const diffOverride = extras.diff;
|
|
852
|
-
if (!this.filesystem.hasExpectedFile(filename)) {
|
|
862
|
+
if (!await this.filesystem.hasExpectedFile(filename)) {
|
|
853
863
|
this.logger.debug(`Expected image not found for ${filename}, taking screenshot`);
|
|
854
864
|
result.filepath = await this.takeScreenshot(page, filename, options);
|
|
855
865
|
} else {
|
|
856
|
-
const { screenshotPath, comparisonResult, diffImagePath } = await this.takeScreenshotWithComparison(page, filename, this.filesystem.readExpectedFile(filename), {
|
|
866
|
+
const { screenshotPath, comparisonResult, diffImagePath } = await this.takeScreenshotWithComparison(page, filename, await this.filesystem.readExpectedFile(filename), {
|
|
857
867
|
...options,
|
|
858
868
|
saveDiffImage: saveDiff,
|
|
859
869
|
diffImageFilename: diffFilename
|
|
@@ -862,6 +872,8 @@ var ScreenshotTool = class {
|
|
|
862
872
|
result.comparisonResult = comparisonResult;
|
|
863
873
|
result.diffImagePath = diffImagePath;
|
|
864
874
|
}
|
|
875
|
+
const duration = Math.round(performance.now() - start);
|
|
876
|
+
this.logger.debug(`${filename} captured in ${duration}ms`);
|
|
865
877
|
return result;
|
|
866
878
|
}
|
|
867
879
|
async capture(page, filename, options, extras = {}) {
|
|
@@ -887,7 +899,8 @@ var ScreenshotTool = class {
|
|
|
887
899
|
const baseCaptureResult = await this.captureSingleScreenshot(page, filename, baseOptions, {
|
|
888
900
|
saveDiffImage: baseSaveDiff,
|
|
889
901
|
diffImageFilename: baseDiffFilename,
|
|
890
|
-
diff: baseDiffOverride
|
|
902
|
+
diff: baseDiffOverride,
|
|
903
|
+
captureStart: extras.captureStart
|
|
891
904
|
});
|
|
892
905
|
baseResult.filepath = baseCaptureResult.filepath;
|
|
893
906
|
baseResult.comparisonResult = baseCaptureResult.comparisonResult;
|
|
@@ -965,13 +978,19 @@ var ScreenshotTool = class {
|
|
|
965
978
|
const baseSaveDiff = extras.saveDiffImage ?? false;
|
|
966
979
|
const baseDiffFilename = extras.diffImageFilename ?? baseFilename;
|
|
967
980
|
const baseDiffOverride = extras.diff;
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
981
|
+
let baseStart;
|
|
982
|
+
if (!extras.skipBaseNavigation) {
|
|
983
|
+
baseStart = performance.now();
|
|
984
|
+
getLogger().debug(`Going to base URL: ${baseUrl}`);
|
|
985
|
+
await page.goto(baseUrl);
|
|
986
|
+
await this.applyScreenshotOptimizations(page);
|
|
987
|
+
await extras.waitForStability?.(page);
|
|
988
|
+
}
|
|
971
989
|
const baseCaptureResult = await this.captureSingleScreenshot(page, baseFilename, baseOptions, {
|
|
972
990
|
saveDiffImage: baseSaveDiff,
|
|
973
991
|
diffImageFilename: baseDiffFilename,
|
|
974
|
-
diff: baseDiffOverride
|
|
992
|
+
diff: baseDiffOverride,
|
|
993
|
+
captureStart: extras.captureStart ?? baseStart
|
|
975
994
|
});
|
|
976
995
|
baseResult.filepath = baseCaptureResult.filepath;
|
|
977
996
|
baseResult.comparisonResult = baseCaptureResult.comparisonResult;
|
|
@@ -993,9 +1012,11 @@ var ScreenshotTool = class {
|
|
|
993
1012
|
variantResults.push(variantResult);
|
|
994
1013
|
continue;
|
|
995
1014
|
}
|
|
1015
|
+
const variantStart = performance.now();
|
|
996
1016
|
getLogger().debug(`Going to variant URL: ${variant.url}`);
|
|
997
1017
|
await page.goto(variant.url);
|
|
998
1018
|
await this.applyScreenshotOptimizations(page);
|
|
1019
|
+
await extras.waitForStability?.(page);
|
|
999
1020
|
const variantExtra = extras.variants?.[variant.id];
|
|
1000
1021
|
const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;
|
|
1001
1022
|
const variantDiffFilename = variantExtra?.diffImageFilename ?? variantFilename;
|
|
@@ -1003,7 +1024,8 @@ var ScreenshotTool = class {
|
|
|
1003
1024
|
const variantCaptureResult = await this.captureSingleScreenshot(page, variantFilename, variantOptions, {
|
|
1004
1025
|
saveDiffImage: variantSaveDiff,
|
|
1005
1026
|
diffImageFilename: variantDiffFilename,
|
|
1006
|
-
diff: variantDiffOverride
|
|
1027
|
+
diff: variantDiffOverride,
|
|
1028
|
+
captureStart: variantStart
|
|
1007
1029
|
});
|
|
1008
1030
|
variantResult.filepath = variantCaptureResult.filepath;
|
|
1009
1031
|
variantResult.comparisonResult = variantCaptureResult.comparisonResult;
|
|
@@ -1032,7 +1054,7 @@ var ScreenshotTool = class {
|
|
|
1032
1054
|
return this.pages[index];
|
|
1033
1055
|
}
|
|
1034
1056
|
};
|
|
1035
|
-
|
|
1036
1057
|
//#endregion
|
|
1037
1058
|
export { ScreenshotFileSystem, ScreenshotTool, compareImages, compareImagesGMSD, defineConfig, imagesMatch, imagesMatchGMSD };
|
|
1059
|
+
|
|
1038
1060
|
//# sourceMappingURL=index.mjs.map
|