@cappa/core 0.0.27 → 0.1.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/.turbo/turbo-build.log +17 -13
- package/CHANGELOG.md +12 -0
- package/dist/index.d.mts +96 -3
- package/dist/index.d.ts +96 -3
- package/dist/index.js +141 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +139 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -3
- package/src/compare.test.ts +358 -0
- package/src/compare.ts +118 -0
- package/src/index.ts +7 -1
- package/src/screenshot.ts +173 -27
- package/src/types.ts +53 -0
- package/vitest.config.ts +11 -0
- package/.turbo/turbo-tsc.log +0 -5
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @cappa/core@0.0
|
|
3
|
+
> @cappa/core@0.1.0 build /home/tim/cappa/packages/core
|
|
4
4
|
> tsdown
|
|
5
5
|
|
|
6
|
-
[34mℹ[39m tsdown [2mv0.14.1[22m powered by rolldown [2mv1.0.0-beta.
|
|
7
|
-
[34mℹ[39m Using tsdown config: [4m/home/tim/
|
|
6
|
+
[34mℹ[39m tsdown [2mv0.14.1[22m powered by rolldown [2mv1.0.0-beta.40[22m
|
|
7
|
+
[34mℹ[39m Using tsdown config: [4m/home/tim/cappa/packages/core/tsdown.config.ts[24m
|
|
8
|
+
(node:206323) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/tim/cappa/packages/core/tsdown.config.ts is not specified and it doesn't parse as CommonJS.
|
|
9
|
+
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
|
|
10
|
+
To eliminate this warning, add "type": "module" to /home/tim/cappa/packages/core/package.json.
|
|
11
|
+
(Use `node --trace-warnings ...` to show where the warning was created)
|
|
8
12
|
[34mℹ[39m entry: [34msrc/index.ts[39m
|
|
9
13
|
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
10
14
|
[34mℹ[39m Build start
|
|
@@ -13,15 +17,15 @@
|
|
|
13
17
|
- For the "resolve.tsconfigFilename". It is deprecated. Please use the top-level `tsconfig` option instead. [0m
|
|
14
18
|
[33mWarning: Invalid input options (1 issue found)
|
|
15
19
|
- For the "resolve.tsconfigFilename". It is deprecated. Please use the top-level `tsconfig` option instead. [0m
|
|
16
|
-
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mindex.js[22m [
|
|
17
|
-
[34mℹ[39m [33m[CJS][39m [2mdist/[22mindex.js.map [
|
|
18
|
-
[34mℹ[39m [33m[CJS][39m 2 files, total:
|
|
20
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mindex.js[22m [2m11.24 kB[22m [2m│ gzip: 3.19 kB[22m
|
|
21
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mindex.js.map [2m22.54 kB[22m [2m│ gzip: 5.95 kB[22m
|
|
22
|
+
[34mℹ[39m [33m[CJS][39m 2 files, total: 33.78 kB
|
|
19
23
|
[33mWarning: Invalid input options (1 issue found)
|
|
20
24
|
- For the "resolve.tsconfigFilename". It is deprecated. Please use the top-level `tsconfig` option instead. [0m
|
|
21
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mindex.mjs[22m [2m
|
|
22
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.mjs.map [
|
|
23
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m
|
|
24
|
-
[34mℹ[39m [34m[ESM][39m 3 files, total:
|
|
25
|
-
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mindex.d.ts[22m[39m [
|
|
26
|
-
[34mℹ[39m [33m[CJS][39m 1 files, total:
|
|
27
|
-
[32m✔[39m Build complete in [
|
|
25
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mindex.mjs[22m [2m 9.70 kB[22m [2m│ gzip: 2.72 kB[22m
|
|
26
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.mjs.map [2m22.46 kB[22m [2m│ gzip: 5.91 kB[22m
|
|
27
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 6.68 kB[22m [2m│ gzip: 2.27 kB[22m
|
|
28
|
+
[34mℹ[39m [34m[ESM][39m 3 files, total: 38.84 kB
|
|
29
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mindex.d.ts[22m[39m [2m6.68 kB[22m [2m│ gzip: 2.27 kB[22m
|
|
30
|
+
[34mℹ[39m [33m[CJS][39m 1 files, total: 6.68 kB
|
|
31
|
+
[32m✔[39m Build complete in [32m1364ms[39m
|
package/CHANGELOG.md
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
import { Browser, BrowserContext, Locator, Page } from "playwright-core";
|
|
2
2
|
import { ConsolaInstance } from "consola";
|
|
3
|
+
import { BlazeDiffOptions } from "@blazediff/types";
|
|
3
4
|
|
|
5
|
+
//#region src/compare.d.ts
|
|
6
|
+
type CompareOptions = BlazeDiffOptions;
|
|
7
|
+
interface CompareResult {
|
|
8
|
+
numDiffPixels: number;
|
|
9
|
+
totalPixels: number;
|
|
10
|
+
percentDifference: number;
|
|
11
|
+
diffBuffer?: Buffer;
|
|
12
|
+
passed: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Compare two PNG images and return the difference
|
|
16
|
+
* @param image1 - First image (file path or Buffer)
|
|
17
|
+
* @param image2 - Second image (file path or Buffer)
|
|
18
|
+
* @param withDiff - Whether to create a diff image
|
|
19
|
+
* @param options - Comparison options
|
|
20
|
+
* @param maxDifferencePercent - Maximum allowed difference percentage (0-100)
|
|
21
|
+
* @returns Comparison result
|
|
22
|
+
*/
|
|
23
|
+
//#endregion
|
|
4
24
|
//#region src/screenshot.d.ts
|
|
5
25
|
declare class ScreenshotTool {
|
|
6
26
|
browserType: string;
|
|
@@ -10,10 +30,14 @@ declare class ScreenshotTool {
|
|
|
10
30
|
height: number;
|
|
11
31
|
};
|
|
12
32
|
outputDir: string;
|
|
33
|
+
actualDir: string;
|
|
34
|
+
diffDir: string;
|
|
35
|
+
expectedDir: string;
|
|
13
36
|
fullPage: boolean;
|
|
14
37
|
browser: Browser | null;
|
|
15
38
|
context: BrowserContext | null;
|
|
16
39
|
page: Page | null;
|
|
40
|
+
diff: DiffConfig;
|
|
17
41
|
constructor(options: {
|
|
18
42
|
browserType?: string;
|
|
19
43
|
headless?: boolean;
|
|
@@ -23,15 +47,18 @@ declare class ScreenshotTool {
|
|
|
23
47
|
};
|
|
24
48
|
outputDir?: string;
|
|
25
49
|
fullPage?: boolean;
|
|
50
|
+
diff?: DiffConfig;
|
|
26
51
|
});
|
|
27
52
|
init(): Promise<void>;
|
|
28
53
|
close(): Promise<void>;
|
|
29
54
|
goTo(page: Page, url: string): Promise<void>;
|
|
55
|
+
private createDirectories;
|
|
56
|
+
getActualFilePath(filename: string): string;
|
|
57
|
+
getDiffFilePath(filename: string): string;
|
|
58
|
+
getExpectedFilePath(filename: string): string;
|
|
30
59
|
getFilePath(filename: string): string;
|
|
31
60
|
takeScreenshot(page: Page, filename: string, options?: {
|
|
32
61
|
fullPage?: boolean;
|
|
33
|
-
waitForSelector?: string;
|
|
34
|
-
waitForTimeout?: number;
|
|
35
62
|
mask?: Locator[];
|
|
36
63
|
omitBackground?: boolean;
|
|
37
64
|
}): Promise<string>;
|
|
@@ -39,6 +66,19 @@ declare class ScreenshotTool {
|
|
|
39
66
|
filename?: string;
|
|
40
67
|
fullPage?: boolean;
|
|
41
68
|
}): Promise<string>;
|
|
69
|
+
takeScreenshotWithComparison(page: Page, filename: string, referenceImage: Buffer, options?: {
|
|
70
|
+
fullPage?: boolean;
|
|
71
|
+
mask?: Locator[];
|
|
72
|
+
omitBackground?: boolean;
|
|
73
|
+
compareOptions?: CompareOptions;
|
|
74
|
+
maxDifferencePercent?: number;
|
|
75
|
+
saveDiffImage?: boolean;
|
|
76
|
+
diffImageFilename?: string;
|
|
77
|
+
}): Promise<{
|
|
78
|
+
screenshotPath: string;
|
|
79
|
+
comparisonResult: CompareResult;
|
|
80
|
+
diffImagePath?: string;
|
|
81
|
+
}>;
|
|
42
82
|
batchScreenshots(urls: string[], options?: {}): Promise<{
|
|
43
83
|
url: string;
|
|
44
84
|
success: boolean;
|
|
@@ -57,6 +97,8 @@ declare class ScreenshotTool {
|
|
|
57
97
|
filepath: string;
|
|
58
98
|
error?: string;
|
|
59
99
|
}[]>;
|
|
100
|
+
hasExpectedImage(filename: string): boolean;
|
|
101
|
+
getExpectedImageBuffer(filename: string): Buffer;
|
|
60
102
|
}
|
|
61
103
|
//#endregion
|
|
62
104
|
//#region src/plugin.d.ts
|
|
@@ -81,6 +123,47 @@ type Plugin<Config = any> = (config?: Config) => PluginDef;
|
|
|
81
123
|
//#endregion
|
|
82
124
|
//#region src/types.d.ts
|
|
83
125
|
type PossiblePromise<T> = Promise<T> | T;
|
|
126
|
+
interface DiffConfig {
|
|
127
|
+
/**
|
|
128
|
+
* Matching threshold (0-1). Lower = more sensitive
|
|
129
|
+
* Default = 0.1
|
|
130
|
+
*
|
|
131
|
+
* This affects how similar a pixel needs to be to the reference image to be considered the same.
|
|
132
|
+
*/
|
|
133
|
+
threshold?: number;
|
|
134
|
+
/**
|
|
135
|
+
* Include anti-aliased pixels in diff count
|
|
136
|
+
* Default = false
|
|
137
|
+
*
|
|
138
|
+
* For most users this should be false, because it makes comparisons very sensitive to sub-pixel differences of
|
|
139
|
+
* fonts and other UI elements.
|
|
140
|
+
*/
|
|
141
|
+
includeAA?: boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Use fast buffer comparison for identical images
|
|
144
|
+
* Default = true
|
|
145
|
+
*/
|
|
146
|
+
fastBufferCheck?: boolean;
|
|
147
|
+
/**
|
|
148
|
+
* Maximum number of different pixels
|
|
149
|
+
* Default = 0
|
|
150
|
+
*
|
|
151
|
+
* The amount of pixels that can be different before the comparison fails.
|
|
152
|
+
*/
|
|
153
|
+
maxDiffPixels?: number;
|
|
154
|
+
/**
|
|
155
|
+
* Maximum percentage of pixels different
|
|
156
|
+
* Default = 0
|
|
157
|
+
*
|
|
158
|
+
* The amount of pixels that can be different before the comparison fails as a percentage of the total pixels.
|
|
159
|
+
* 0 = no difference, 100 = 100% difference.
|
|
160
|
+
*
|
|
161
|
+
* Since this is a relative value, it is affected by the size of the images. A 100px image with a 10% difference
|
|
162
|
+
* is 10 pixels different, but a 1000px image with a 10% difference is 100 pixels different. On very large full page screenshots
|
|
163
|
+
* this can mean that small changes still slip through.
|
|
164
|
+
*/
|
|
165
|
+
maxDiffPercentage?: number;
|
|
166
|
+
}
|
|
84
167
|
/**
|
|
85
168
|
* Config used in `cappa.config.ts`
|
|
86
169
|
*
|
|
@@ -97,6 +180,7 @@ type UserConfig = {
|
|
|
97
180
|
*/
|
|
98
181
|
root?: string;
|
|
99
182
|
outputDir?: string;
|
|
183
|
+
diff?: DiffConfig;
|
|
100
184
|
/**
|
|
101
185
|
* An array of Cappa plugins used for generation. Each plugin may have additional configurable options (defined within the plugin itself). If a plugin relies on another plugin, an error will occur if the required dependency is missing. Refer to "pre" for more details.
|
|
102
186
|
*/
|
|
@@ -116,6 +200,15 @@ interface Exposed {
|
|
|
116
200
|
getBaseScreenshotOptions(): ScreenshotOptions;
|
|
117
201
|
waitBrowserMetricsStable(): Promise<void>;
|
|
118
202
|
}
|
|
203
|
+
interface Screenshot {
|
|
204
|
+
id: string;
|
|
205
|
+
name: string;
|
|
206
|
+
category: "new" | "deleted" | "changed" | "passed";
|
|
207
|
+
actualPath?: string;
|
|
208
|
+
expectedPath?: string;
|
|
209
|
+
diffPath?: string;
|
|
210
|
+
approved?: boolean;
|
|
211
|
+
}
|
|
119
212
|
//#endregion
|
|
120
213
|
//#region src/config.d.ts
|
|
121
214
|
/**
|
|
@@ -123,5 +216,5 @@ interface Exposed {
|
|
|
123
216
|
*/
|
|
124
217
|
declare function defineConfig(options: PossiblePromise<UserConfig | Array<UserConfig>> | (() => PossiblePromise<UserConfig | Array<UserConfig>>)): typeof options;
|
|
125
218
|
//#endregion
|
|
126
|
-
export { type Exposed, type Plugin, type PluginDef, type PluginFunction, type ScreenshotOptions, ScreenshotTool, type UserConfig, defineConfig };
|
|
219
|
+
export { type Exposed, type Plugin, type PluginDef, type PluginFunction, type Screenshot, type ScreenshotOptions, ScreenshotTool, type UserConfig, defineConfig };
|
|
127
220
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
import { ConsolaInstance } from "consola";
|
|
2
2
|
import { Browser, BrowserContext, Locator, Page } from "playwright-core";
|
|
3
|
+
import { BlazeDiffOptions } from "@blazediff/types";
|
|
3
4
|
|
|
5
|
+
//#region src/compare.d.ts
|
|
6
|
+
type CompareOptions = BlazeDiffOptions;
|
|
7
|
+
interface CompareResult {
|
|
8
|
+
numDiffPixels: number;
|
|
9
|
+
totalPixels: number;
|
|
10
|
+
percentDifference: number;
|
|
11
|
+
diffBuffer?: Buffer;
|
|
12
|
+
passed: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Compare two PNG images and return the difference
|
|
16
|
+
* @param image1 - First image (file path or Buffer)
|
|
17
|
+
* @param image2 - Second image (file path or Buffer)
|
|
18
|
+
* @param withDiff - Whether to create a diff image
|
|
19
|
+
* @param options - Comparison options
|
|
20
|
+
* @param maxDifferencePercent - Maximum allowed difference percentage (0-100)
|
|
21
|
+
* @returns Comparison result
|
|
22
|
+
*/
|
|
23
|
+
//#endregion
|
|
4
24
|
//#region src/screenshot.d.ts
|
|
5
25
|
declare class ScreenshotTool {
|
|
6
26
|
browserType: string;
|
|
@@ -10,10 +30,14 @@ declare class ScreenshotTool {
|
|
|
10
30
|
height: number;
|
|
11
31
|
};
|
|
12
32
|
outputDir: string;
|
|
33
|
+
actualDir: string;
|
|
34
|
+
diffDir: string;
|
|
35
|
+
expectedDir: string;
|
|
13
36
|
fullPage: boolean;
|
|
14
37
|
browser: Browser | null;
|
|
15
38
|
context: BrowserContext | null;
|
|
16
39
|
page: Page | null;
|
|
40
|
+
diff: DiffConfig;
|
|
17
41
|
constructor(options: {
|
|
18
42
|
browserType?: string;
|
|
19
43
|
headless?: boolean;
|
|
@@ -23,15 +47,18 @@ declare class ScreenshotTool {
|
|
|
23
47
|
};
|
|
24
48
|
outputDir?: string;
|
|
25
49
|
fullPage?: boolean;
|
|
50
|
+
diff?: DiffConfig;
|
|
26
51
|
});
|
|
27
52
|
init(): Promise<void>;
|
|
28
53
|
close(): Promise<void>;
|
|
29
54
|
goTo(page: Page, url: string): Promise<void>;
|
|
55
|
+
private createDirectories;
|
|
56
|
+
getActualFilePath(filename: string): string;
|
|
57
|
+
getDiffFilePath(filename: string): string;
|
|
58
|
+
getExpectedFilePath(filename: string): string;
|
|
30
59
|
getFilePath(filename: string): string;
|
|
31
60
|
takeScreenshot(page: Page, filename: string, options?: {
|
|
32
61
|
fullPage?: boolean;
|
|
33
|
-
waitForSelector?: string;
|
|
34
|
-
waitForTimeout?: number;
|
|
35
62
|
mask?: Locator[];
|
|
36
63
|
omitBackground?: boolean;
|
|
37
64
|
}): Promise<string>;
|
|
@@ -39,6 +66,19 @@ declare class ScreenshotTool {
|
|
|
39
66
|
filename?: string;
|
|
40
67
|
fullPage?: boolean;
|
|
41
68
|
}): Promise<string>;
|
|
69
|
+
takeScreenshotWithComparison(page: Page, filename: string, referenceImage: Buffer, options?: {
|
|
70
|
+
fullPage?: boolean;
|
|
71
|
+
mask?: Locator[];
|
|
72
|
+
omitBackground?: boolean;
|
|
73
|
+
compareOptions?: CompareOptions;
|
|
74
|
+
maxDifferencePercent?: number;
|
|
75
|
+
saveDiffImage?: boolean;
|
|
76
|
+
diffImageFilename?: string;
|
|
77
|
+
}): Promise<{
|
|
78
|
+
screenshotPath: string;
|
|
79
|
+
comparisonResult: CompareResult;
|
|
80
|
+
diffImagePath?: string;
|
|
81
|
+
}>;
|
|
42
82
|
batchScreenshots(urls: string[], options?: {}): Promise<{
|
|
43
83
|
url: string;
|
|
44
84
|
success: boolean;
|
|
@@ -57,6 +97,8 @@ declare class ScreenshotTool {
|
|
|
57
97
|
filepath: string;
|
|
58
98
|
error?: string;
|
|
59
99
|
}[]>;
|
|
100
|
+
hasExpectedImage(filename: string): boolean;
|
|
101
|
+
getExpectedImageBuffer(filename: string): Buffer;
|
|
60
102
|
}
|
|
61
103
|
//#endregion
|
|
62
104
|
//#region src/plugin.d.ts
|
|
@@ -81,6 +123,47 @@ type Plugin<Config = any> = (config?: Config) => PluginDef;
|
|
|
81
123
|
//#endregion
|
|
82
124
|
//#region src/types.d.ts
|
|
83
125
|
type PossiblePromise<T> = Promise<T> | T;
|
|
126
|
+
interface DiffConfig {
|
|
127
|
+
/**
|
|
128
|
+
* Matching threshold (0-1). Lower = more sensitive
|
|
129
|
+
* Default = 0.1
|
|
130
|
+
*
|
|
131
|
+
* This affects how similar a pixel needs to be to the reference image to be considered the same.
|
|
132
|
+
*/
|
|
133
|
+
threshold?: number;
|
|
134
|
+
/**
|
|
135
|
+
* Include anti-aliased pixels in diff count
|
|
136
|
+
* Default = false
|
|
137
|
+
*
|
|
138
|
+
* For most users this should be false, because it makes comparisons very sensitive to sub-pixel differences of
|
|
139
|
+
* fonts and other UI elements.
|
|
140
|
+
*/
|
|
141
|
+
includeAA?: boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Use fast buffer comparison for identical images
|
|
144
|
+
* Default = true
|
|
145
|
+
*/
|
|
146
|
+
fastBufferCheck?: boolean;
|
|
147
|
+
/**
|
|
148
|
+
* Maximum number of different pixels
|
|
149
|
+
* Default = 0
|
|
150
|
+
*
|
|
151
|
+
* The amount of pixels that can be different before the comparison fails.
|
|
152
|
+
*/
|
|
153
|
+
maxDiffPixels?: number;
|
|
154
|
+
/**
|
|
155
|
+
* Maximum percentage of pixels different
|
|
156
|
+
* Default = 0
|
|
157
|
+
*
|
|
158
|
+
* The amount of pixels that can be different before the comparison fails as a percentage of the total pixels.
|
|
159
|
+
* 0 = no difference, 100 = 100% difference.
|
|
160
|
+
*
|
|
161
|
+
* Since this is a relative value, it is affected by the size of the images. A 100px image with a 10% difference
|
|
162
|
+
* is 10 pixels different, but a 1000px image with a 10% difference is 100 pixels different. On very large full page screenshots
|
|
163
|
+
* this can mean that small changes still slip through.
|
|
164
|
+
*/
|
|
165
|
+
maxDiffPercentage?: number;
|
|
166
|
+
}
|
|
84
167
|
/**
|
|
85
168
|
* Config used in `cappa.config.ts`
|
|
86
169
|
*
|
|
@@ -97,6 +180,7 @@ type UserConfig = {
|
|
|
97
180
|
*/
|
|
98
181
|
root?: string;
|
|
99
182
|
outputDir?: string;
|
|
183
|
+
diff?: DiffConfig;
|
|
100
184
|
/**
|
|
101
185
|
* An array of Cappa plugins used for generation. Each plugin may have additional configurable options (defined within the plugin itself). If a plugin relies on another plugin, an error will occur if the required dependency is missing. Refer to "pre" for more details.
|
|
102
186
|
*/
|
|
@@ -116,6 +200,15 @@ interface Exposed {
|
|
|
116
200
|
getBaseScreenshotOptions(): ScreenshotOptions;
|
|
117
201
|
waitBrowserMetricsStable(): Promise<void>;
|
|
118
202
|
}
|
|
203
|
+
interface Screenshot {
|
|
204
|
+
id: string;
|
|
205
|
+
name: string;
|
|
206
|
+
category: "new" | "deleted" | "changed" | "passed";
|
|
207
|
+
actualPath?: string;
|
|
208
|
+
expectedPath?: string;
|
|
209
|
+
diffPath?: string;
|
|
210
|
+
approved?: boolean;
|
|
211
|
+
}
|
|
119
212
|
//#endregion
|
|
120
213
|
//#region src/config.d.ts
|
|
121
214
|
/**
|
|
@@ -123,5 +216,5 @@ interface Exposed {
|
|
|
123
216
|
*/
|
|
124
217
|
declare function defineConfig(options: PossiblePromise<UserConfig | Array<UserConfig>> | (() => PossiblePromise<UserConfig | Array<UserConfig>>)): typeof options;
|
|
125
218
|
//#endregion
|
|
126
|
-
export { type Exposed, type Plugin, type PluginDef, type PluginFunction, type ScreenshotOptions, ScreenshotTool, type UserConfig, defineConfig };
|
|
219
|
+
export { type Exposed, type Plugin, type PluginDef, type PluginFunction, type Screenshot, type ScreenshotOptions, ScreenshotTool, type UserConfig, defineConfig };
|
|
127
220
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -27,6 +27,10 @@ let node_path = require("node:path");
|
|
|
27
27
|
node_path = __toESM(node_path);
|
|
28
28
|
let playwright_core = require("playwright-core");
|
|
29
29
|
playwright_core = __toESM(playwright_core);
|
|
30
|
+
let __blazediff_core = require("@blazediff/core");
|
|
31
|
+
__blazediff_core = __toESM(__blazediff_core);
|
|
32
|
+
let pngjs = require("pngjs");
|
|
33
|
+
pngjs = __toESM(pngjs);
|
|
30
34
|
|
|
31
35
|
//#region src/config.ts
|
|
32
36
|
/**
|
|
@@ -36,17 +40,74 @@ function defineConfig(options) {
|
|
|
36
40
|
return options;
|
|
37
41
|
}
|
|
38
42
|
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/compare.ts
|
|
45
|
+
const compare = (image1, image2, diff, width, height, options) => {
|
|
46
|
+
return (0, __blazediff_core.default)(image1, image2, diff, width, height, options);
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Compare two PNG images and return the difference
|
|
50
|
+
* @param image1 - First image (file path or Buffer)
|
|
51
|
+
* @param image2 - Second image (file path or Buffer)
|
|
52
|
+
* @param withDiff - Whether to create a diff image
|
|
53
|
+
* @param options - Comparison options
|
|
54
|
+
* @param maxDifferencePercent - Maximum allowed difference percentage (0-100)
|
|
55
|
+
* @returns Comparison result
|
|
56
|
+
*/
|
|
57
|
+
async function compareImages(image1, image2, withDiff = false, options) {
|
|
58
|
+
const png1 = await loadPNG(image1);
|
|
59
|
+
const png2 = await loadPNG(image2);
|
|
60
|
+
const { width, height } = png1;
|
|
61
|
+
const diff = withDiff ? new pngjs.PNG({
|
|
62
|
+
width,
|
|
63
|
+
height
|
|
64
|
+
}) : void 0;
|
|
65
|
+
const numDiffPixels = compare(png1.data, png2.data, diff?.data, width, height, options);
|
|
66
|
+
const totalPixels = width * height;
|
|
67
|
+
const percentDifference = numDiffPixels / totalPixels * 100;
|
|
68
|
+
const passed = percentDifference <= (options.maxDiffPercentage || 0);
|
|
69
|
+
const diffBuffer = diff ? pngjs.PNG.sync.write(diff) : void 0;
|
|
70
|
+
return {
|
|
71
|
+
numDiffPixels,
|
|
72
|
+
totalPixels,
|
|
73
|
+
percentDifference,
|
|
74
|
+
diffBuffer,
|
|
75
|
+
passed
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Load a PNG image from file path or Buffer
|
|
80
|
+
*/
|
|
81
|
+
async function loadPNG(source) {
|
|
82
|
+
if (Buffer.isBuffer(source)) return pngjs.PNG.sync.read(source);
|
|
83
|
+
else {
|
|
84
|
+
const buffer = node_fs.default.readFileSync(source);
|
|
85
|
+
return pngjs.PNG.sync.read(buffer);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
39
89
|
//#endregion
|
|
40
90
|
//#region src/screenshot.ts
|
|
91
|
+
const defaultDiffConfig = {
|
|
92
|
+
threshold: .1,
|
|
93
|
+
includeAA: false,
|
|
94
|
+
fastBufferCheck: true,
|
|
95
|
+
maxDiffPixels: 0,
|
|
96
|
+
maxDiffPercentage: 0
|
|
97
|
+
};
|
|
41
98
|
var ScreenshotTool = class {
|
|
42
99
|
browserType;
|
|
43
100
|
headless;
|
|
44
101
|
viewport;
|
|
45
102
|
outputDir;
|
|
103
|
+
actualDir;
|
|
104
|
+
diffDir;
|
|
105
|
+
expectedDir;
|
|
46
106
|
fullPage;
|
|
47
107
|
browser = null;
|
|
48
108
|
context = null;
|
|
49
109
|
page = null;
|
|
110
|
+
diff;
|
|
50
111
|
constructor(options) {
|
|
51
112
|
this.browserType = options.browserType || "chromium";
|
|
52
113
|
this.headless = options.headless !== false;
|
|
@@ -56,9 +117,16 @@ var ScreenshotTool = class {
|
|
|
56
117
|
};
|
|
57
118
|
this.outputDir = options.outputDir || "./screenshots";
|
|
58
119
|
this.fullPage = options.fullPage !== false;
|
|
120
|
+
this.diff = {
|
|
121
|
+
...defaultDiffConfig,
|
|
122
|
+
...options.diff
|
|
123
|
+
};
|
|
124
|
+
this.actualDir = node_path.default.join(this.outputDir, "actual");
|
|
125
|
+
this.diffDir = node_path.default.join(this.outputDir, "diff");
|
|
126
|
+
this.expectedDir = node_path.default.join(this.outputDir, "expected");
|
|
59
127
|
}
|
|
60
128
|
async init() {
|
|
61
|
-
|
|
129
|
+
this.createDirectories();
|
|
62
130
|
const browserClass = {
|
|
63
131
|
chromium: playwright_core.chromium,
|
|
64
132
|
firefox: playwright_core.firefox,
|
|
@@ -83,22 +151,34 @@ var ScreenshotTool = class {
|
|
|
83
151
|
if (!this.context) throw new Error("Browser not initialized");
|
|
84
152
|
await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
85
153
|
}
|
|
154
|
+
createDirectories() {
|
|
155
|
+
const directories = [
|
|
156
|
+
this.outputDir,
|
|
157
|
+
this.actualDir,
|
|
158
|
+
this.diffDir,
|
|
159
|
+
this.expectedDir
|
|
160
|
+
];
|
|
161
|
+
for (const dir of directories) if (!node_fs.default.existsSync(dir)) {
|
|
162
|
+
node_fs.default.mkdirSync(dir, { recursive: true });
|
|
163
|
+
console.log(`Created directory: ${dir}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
getActualFilePath(filename) {
|
|
167
|
+
return node_path.default.join(this.actualDir, filename);
|
|
168
|
+
}
|
|
169
|
+
getDiffFilePath(filename) {
|
|
170
|
+
return node_path.default.join(this.diffDir, filename);
|
|
171
|
+
}
|
|
172
|
+
getExpectedFilePath(filename) {
|
|
173
|
+
return node_path.default.join(this.expectedDir, filename);
|
|
174
|
+
}
|
|
86
175
|
getFilePath(filename) {
|
|
87
|
-
return
|
|
176
|
+
return this.getActualFilePath(filename);
|
|
88
177
|
}
|
|
89
178
|
async takeScreenshot(page, filename, options = {}) {
|
|
90
179
|
if (!this.browser) throw new Error("Browser not initialized");
|
|
91
180
|
try {
|
|
92
|
-
|
|
93
|
-
console.log(`Waiting for selector: ${options.waitForSelector}`);
|
|
94
|
-
await page.waitForSelector(options.waitForSelector, { timeout: 1e4 });
|
|
95
|
-
}
|
|
96
|
-
const waitTime = options.waitForTimeout !== void 0 ? options.waitForTimeout : 0;
|
|
97
|
-
if (waitTime > 0) {
|
|
98
|
-
console.log(`Waiting for ${waitTime}ms for dynamic content`);
|
|
99
|
-
await page.waitForTimeout(waitTime);
|
|
100
|
-
}
|
|
101
|
-
const filepath = this.getFilePath(filename);
|
|
181
|
+
const filepath = this.getActualFilePath(filename);
|
|
102
182
|
const screenshotOptions = {
|
|
103
183
|
path: filepath,
|
|
104
184
|
fullPage: options.fullPage !== void 0 ? options.fullPage : this.fullPage,
|
|
@@ -123,7 +203,7 @@ var ScreenshotTool = class {
|
|
|
123
203
|
const element = await page.$(selector);
|
|
124
204
|
if (!element) throw new Error(`Element not found: ${selector}`);
|
|
125
205
|
const filename = options.filename || `${selector.replace(/[^a-zA-Z0-9]/g, "_")}.png`;
|
|
126
|
-
const filepath = this.
|
|
206
|
+
const filepath = this.getActualFilePath(filename);
|
|
127
207
|
const elementScreenshotOptions = {
|
|
128
208
|
path: filepath,
|
|
129
209
|
type: "png"
|
|
@@ -138,6 +218,44 @@ var ScreenshotTool = class {
|
|
|
138
218
|
await page.close();
|
|
139
219
|
}
|
|
140
220
|
}
|
|
221
|
+
async takeScreenshotWithComparison(page, filename, referenceImage, options = {}) {
|
|
222
|
+
if (!this.browser) throw new Error("Browser not initialized");
|
|
223
|
+
try {
|
|
224
|
+
const filepath = this.getActualFilePath(filename);
|
|
225
|
+
node_fs.default.mkdirSync(node_path.default.dirname(filepath), { recursive: true });
|
|
226
|
+
const screenshotOptions = {
|
|
227
|
+
fullPage: options.fullPage !== void 0 ? options.fullPage : this.fullPage,
|
|
228
|
+
type: "png",
|
|
229
|
+
timeout: 6e4,
|
|
230
|
+
mask: options.mask,
|
|
231
|
+
omitBackground: options.omitBackground,
|
|
232
|
+
scale: "css"
|
|
233
|
+
};
|
|
234
|
+
const screenshotBuffer = await page.screenshot(screenshotOptions);
|
|
235
|
+
node_fs.default.writeFileSync(filepath, screenshotBuffer);
|
|
236
|
+
console.log(`Screenshot saved: ${filepath}`);
|
|
237
|
+
const comparisonResult = await compareImages(screenshotBuffer, referenceImage, true, {
|
|
238
|
+
...this.diff,
|
|
239
|
+
...options.compareOptions
|
|
240
|
+
});
|
|
241
|
+
console.log(`Comparison result: ${comparisonResult.numDiffPixels} pixels different (${comparisonResult.percentDifference.toFixed(2)}%) (${comparisonResult.passed ? "PASSED" : "FAILED"})`);
|
|
242
|
+
let diffImagePath;
|
|
243
|
+
if (options.saveDiffImage && comparisonResult.diffBuffer && !comparisonResult.passed) {
|
|
244
|
+
const diffFilename = options.diffImageFilename || filename;
|
|
245
|
+
diffImagePath = this.getDiffFilePath(diffFilename);
|
|
246
|
+
node_fs.default.mkdirSync(node_path.default.dirname(diffImagePath), { recursive: true });
|
|
247
|
+
node_fs.default.writeFileSync(diffImagePath, comparisonResult.diffBuffer);
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
screenshotPath: filepath,
|
|
251
|
+
comparisonResult,
|
|
252
|
+
diffImagePath
|
|
253
|
+
};
|
|
254
|
+
} catch (error) {
|
|
255
|
+
console.error(`Error taking screenshot with comparison of ${page.url()}:`, error.message);
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
141
259
|
async batchScreenshots(urls, options = {}) {
|
|
142
260
|
const results = [];
|
|
143
261
|
for (const url of urls) try {
|
|
@@ -185,7 +303,7 @@ var ScreenshotTool = class {
|
|
|
185
303
|
await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
186
304
|
await page.waitForTimeout(2e3);
|
|
187
305
|
const filename = `${viewport.name}.png`;
|
|
188
|
-
const filepath = this.
|
|
306
|
+
const filepath = this.getActualFilePath(filename);
|
|
189
307
|
const responsiveScreenshotOptions = {
|
|
190
308
|
path: filepath,
|
|
191
309
|
fullPage: options.fullPage !== void 0 ? options.fullPage : this.fullPage,
|
|
@@ -210,6 +328,15 @@ var ScreenshotTool = class {
|
|
|
210
328
|
}
|
|
211
329
|
return results;
|
|
212
330
|
}
|
|
331
|
+
hasExpectedImage(filename) {
|
|
332
|
+
const expectedPath = this.getExpectedFilePath(filename);
|
|
333
|
+
return node_fs.default.existsSync(expectedPath);
|
|
334
|
+
}
|
|
335
|
+
getExpectedImageBuffer(filename) {
|
|
336
|
+
const expectedPath = this.getExpectedFilePath(filename);
|
|
337
|
+
if (!node_fs.default.existsSync(expectedPath)) throw new Error(`Expected image not found: ${expectedPath}`);
|
|
338
|
+
return node_fs.default.readFileSync(expectedPath);
|
|
339
|
+
}
|
|
213
340
|
};
|
|
214
341
|
var screenshot_default = ScreenshotTool;
|
|
215
342
|
|