@cappa/plugin-storybook 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +13 -13
- package/CHANGELOG.md +25 -0
- package/LICENSE +21 -0
- package/README.md +1 -91
- package/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +192 -146
- package/dist/index.mjs +192 -146
- package/dist/preview.mjs +1 -1
- package/package.json +8 -8
- package/src/cappa/plugin.ts +213 -184
- package/src/cappa/storybook-url.test.ts +43 -0
- package/src/cappa/storybook-url.ts +55 -0
- package/src/storybook/addon/decorator.ts +1 -1
- package/src/storybook/util.ts +1 -1
- package/src/types.ts +29 -4
- package/tsconfig.json +13 -0
- package/src/tsconfig.json +0 -7
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @cappa/plugin-storybook@0.
|
|
3
|
+
> @cappa/plugin-storybook@0.2.0 build /home/tim/cappa/packages/plugins/plugin-storybook
|
|
4
4
|
> tsup
|
|
5
5
|
|
|
6
6
|
[34mCLI[39m Building entry: src/index.ts
|
|
7
|
-
[34mCLI[39m Using tsconfig:
|
|
7
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
8
8
|
[34mCLI[39m tsup v8.5.0
|
|
9
9
|
[34mCLI[39m Using tsup config: /home/tim/cappa/packages/plugins/plugin-storybook/tsup.config.ts
|
|
10
10
|
[34mCLI[39m Building entry: src/storybook/addon/preview.ts
|
|
11
|
-
[34mCLI[39m Using tsconfig:
|
|
11
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
12
12
|
[34mCLI[39m tsup v8.5.0
|
|
13
13
|
[34mCLI[39m Using tsup config: /home/tim/cappa/packages/plugins/plugin-storybook/tsup.config.ts
|
|
14
14
|
[34mCLI[39m Building entry: src/storybook/addon/browser.ts
|
|
15
|
-
[34mCLI[39m Using tsconfig:
|
|
15
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
16
16
|
[34mCLI[39m tsup v8.5.0
|
|
17
17
|
[34mCLI[39m Using tsup config: /home/tim/cappa/packages/plugins/plugin-storybook/tsup.config.ts
|
|
18
18
|
[34mCLI[39m Target: esnext
|
|
@@ -26,14 +26,14 @@
|
|
|
26
26
|
[34mCLI[39m Cleaning output folder
|
|
27
27
|
[34mESM[39m Build start
|
|
28
28
|
[32mESM[39m [1mdist/browser.mjs [22m[32m138.00 B[39m
|
|
29
|
-
[32mESM[39m ⚡️ Build success in 8ms
|
|
30
|
-
[32mESM[39m [1mdist/preview.mjs [22m[32m1.34 KB[39m
|
|
31
29
|
[32mESM[39m ⚡️ Build success in 10ms
|
|
32
|
-
[32mESM[39m [1mdist/
|
|
33
|
-
[32mESM[39m ⚡️ Build success in
|
|
34
|
-
[
|
|
35
|
-
[
|
|
30
|
+
[32mESM[39m [1mdist/preview.mjs [22m[32m1.35 KB[39m
|
|
31
|
+
[32mESM[39m ⚡️ Build success in 11ms
|
|
32
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m11.00 KB[39m
|
|
33
|
+
[32mESM[39m ⚡️ Build success in 16ms
|
|
34
|
+
[32mCJS[39m [1mdist/index.js [22m[32m12.33 KB[39m
|
|
35
|
+
[32mCJS[39m ⚡️ Build success in 16ms
|
|
36
36
|
DTS Build start
|
|
37
|
-
DTS ⚡️ Build success in
|
|
38
|
-
DTS dist/index.d.ts
|
|
39
|
-
DTS dist/index.d.mts
|
|
37
|
+
DTS ⚡️ Build success in 1283ms
|
|
38
|
+
DTS dist/index.d.ts 905.00 B
|
|
39
|
+
DTS dist/index.d.mts 905.00 B
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# @cappa/plugin-storybook
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 5b1f66f: Add support for capturing screenshot variants and configure Storybook stories to request multiple viewport screenshots.
|
|
8
|
+
- 4cd0ee2: Allow configuring Storybook args and globals for captured screenshots.
|
|
9
|
+
|
|
10
|
+
### Patch Changes
|
|
11
|
+
|
|
12
|
+
- a1d91c6: feat: add viewport options
|
|
13
|
+
- Updated dependencies [5b1f66f]
|
|
14
|
+
- Updated dependencies [87c8ab9]
|
|
15
|
+
- Updated dependencies [a1d91c6]
|
|
16
|
+
- Updated dependencies [88b3648]
|
|
17
|
+
- @cappa/core@0.2.0
|
|
18
|
+
|
|
19
|
+
## 0.1.3
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- f6456fb: feat: add retry functionality
|
|
24
|
+
- Updated dependencies [f6456fb]
|
|
25
|
+
- @cappa/core@0.1.3
|
|
26
|
+
- @cappa/logger@0.0.3
|
|
27
|
+
|
|
3
28
|
## 0.1.2
|
|
4
29
|
|
|
5
30
|
### Patch Changes
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tim Richter
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,94 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
This plugin allows to take screenshots of Storybook stories with Cappa.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
pnpm add -D @cappa/plugin-storybook
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Usage
|
|
12
|
-
|
|
13
|
-
Add the plugin to your cappa config:
|
|
14
|
-
|
|
15
|
-
```ts
|
|
16
|
-
import { defineConfig } from "@cappa/core";
|
|
17
|
-
import { cappaPluginStorybook } from "@cappa/plugin-storybook";
|
|
18
|
-
|
|
19
|
-
export default defineConfig({
|
|
20
|
-
plugins: [cappaPluginStorybook({ storybookUrl: "http://localhost:8080" })],
|
|
21
|
-
});
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
> Make sure to have a running Storybook server on the given url.
|
|
25
|
-
|
|
26
|
-
Add the plugin to your storybook config:
|
|
27
|
-
|
|
28
|
-
```ts
|
|
29
|
-
const config: StorybookConfig = {
|
|
30
|
-
addons: ["@cappa/plugin-storybook"],
|
|
31
|
-
};
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
Run cappa cli in the directory of your cappa config:
|
|
35
|
-
|
|
36
|
-
```bash
|
|
37
|
-
cappa
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## Options
|
|
41
|
-
|
|
42
|
-
## Storybook options
|
|
43
|
-
|
|
44
|
-
Cappa uses a storybook addon that allows you to pass options from storybook into cappa.
|
|
45
|
-
|
|
46
|
-
### Parameters
|
|
47
|
-
|
|
48
|
-
| Option | Description | Default |
|
|
49
|
-
| -------- | ----------------------------------------------- | --------------- |
|
|
50
|
-
| skip | Whether to skip the screenshot | false |
|
|
51
|
-
| delay | The delay to wait before taking the screenshot | null (no delay) |
|
|
52
|
-
| fullPage | Whether to take a full page screenshot | true |
|
|
53
|
-
| mask | An array of selectors to mask in the screenshot | [] |
|
|
54
|
-
| omitBackground | Whether to omit the background in the screenshot | false |
|
|
55
|
-
|
|
56
|
-
#### Example
|
|
57
|
-
|
|
58
|
-
```ts
|
|
59
|
-
export const Primary: Story = {
|
|
60
|
-
parameters: {
|
|
61
|
-
cappa: {
|
|
62
|
-
skip: true,
|
|
63
|
-
delay: 1000,
|
|
64
|
-
fullPage: false,
|
|
65
|
-
mask: ["#my-element", ".my-class"],
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
## Flakiness
|
|
72
|
-
|
|
73
|
-
To avoid flakiness, cappa disables css animations and transitions by default. For additional use cases or animation libraries, you can use the `isCappa` function to check if the current window is a cappa window. You can use it inside of your storybook preview file (`.storybook/preview.ts`):
|
|
74
|
-
|
|
75
|
-
```ts
|
|
76
|
-
import { isCappa } from '@cappa/plugin-storybook/browser';
|
|
77
|
-
|
|
78
|
-
if (isCappa()) {
|
|
79
|
-
import("disable-animations.css");
|
|
80
|
-
MotionGlobalConfig.disableAnimations = true;
|
|
81
|
-
}
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### DOM Mutations
|
|
85
|
-
|
|
86
|
-
Cappa waits for DOM mutations to be idle before taking the screenshot. It does so by querying the DOM for mutations and waiting for a certain amount of time to pass, until the page is considered idle.
|
|
87
|
-
|
|
88
|
-
### Fonts & Images
|
|
89
|
-
|
|
90
|
-
Cappa waits for fonts and images to be loaded before taking the screenshot.
|
|
91
|
-
|
|
92
|
-
### SVG's
|
|
93
|
-
|
|
94
|
-
Cappa sets global css styles to render svg images more consistently. You might experience
|
|
95
|
-
differences between actual browsers and cappa screenshots, but that is expected and wanted behavior, since browsers tend to render svg images differently.
|
|
5
|
+
For documentation, see the [Storybook Plugin documentation](https://tim-richter.github.io/cappa/storybook).
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { Plugin } from '@cappa/core';
|
|
2
2
|
|
|
3
|
+
interface StorybookRenderOptions {
|
|
4
|
+
viewMode?: "story" | "docs";
|
|
5
|
+
args?: Record<string, unknown>;
|
|
6
|
+
globals?: Record<string, string | number | boolean>;
|
|
7
|
+
query?: Record<string, string | number | boolean>;
|
|
8
|
+
fullscreen?: boolean;
|
|
9
|
+
singleStory?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
3
12
|
interface StorybookPluginOptions {
|
|
4
13
|
storybookUrl: string;
|
|
5
14
|
includeStories?: string[];
|
|
@@ -10,6 +19,7 @@ interface StorybookPluginOptions {
|
|
|
10
19
|
};
|
|
11
20
|
waitForSelector?: string;
|
|
12
21
|
waitForTimeout?: number;
|
|
22
|
+
storybook?: StorybookRenderOptions;
|
|
13
23
|
}
|
|
14
24
|
/**
|
|
15
25
|
* Storybook Plugin for Cappa
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { Plugin } from '@cappa/core';
|
|
2
2
|
|
|
3
|
+
interface StorybookRenderOptions {
|
|
4
|
+
viewMode?: "story" | "docs";
|
|
5
|
+
args?: Record<string, unknown>;
|
|
6
|
+
globals?: Record<string, string | number | boolean>;
|
|
7
|
+
query?: Record<string, string | number | boolean>;
|
|
8
|
+
fullscreen?: boolean;
|
|
9
|
+
singleStory?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
3
12
|
interface StorybookPluginOptions {
|
|
4
13
|
storybookUrl: string;
|
|
5
14
|
includeStories?: string[];
|
|
@@ -10,6 +19,7 @@ interface StorybookPluginOptions {
|
|
|
10
19
|
};
|
|
11
20
|
waitForSelector?: string;
|
|
12
21
|
waitForTimeout?: number;
|
|
22
|
+
storybook?: StorybookRenderOptions;
|
|
13
23
|
}
|
|
14
24
|
/**
|
|
15
25
|
* Storybook Plugin for Cappa
|
package/dist/index.js
CHANGED
|
@@ -37,7 +37,42 @@ module.exports = __toCommonJS(index_exports);
|
|
|
37
37
|
|
|
38
38
|
// src/cappa/plugin.ts
|
|
39
39
|
var import_logger = require("@cappa/logger");
|
|
40
|
-
|
|
40
|
+
|
|
41
|
+
// src/cappa/storybook-url.ts
|
|
42
|
+
function encodePairs(obj) {
|
|
43
|
+
if (!obj || !Object.keys(obj).length) return "";
|
|
44
|
+
return Object.entries(obj).filter(([, v]) => v !== void 0).map(([k, v]) => {
|
|
45
|
+
const val = typeof v === "string" ? encodeURIComponent(v) : typeof v === "number" || typeof v === "boolean" ? String(v) : encodeURIComponent(JSON.stringify(v));
|
|
46
|
+
return `${encodeURIComponent(k)}:${val}`;
|
|
47
|
+
}).join(";");
|
|
48
|
+
}
|
|
49
|
+
function buildStorybookIframeUrl({
|
|
50
|
+
baseUrl,
|
|
51
|
+
storyId,
|
|
52
|
+
viewMode = "story",
|
|
53
|
+
args,
|
|
54
|
+
globals,
|
|
55
|
+
query,
|
|
56
|
+
fullscreen = true,
|
|
57
|
+
singleStory = true
|
|
58
|
+
}) {
|
|
59
|
+
const u = new URL("/iframe.html", baseUrl);
|
|
60
|
+
u.searchParams.set("id", storyId);
|
|
61
|
+
u.searchParams.set("viewMode", viewMode);
|
|
62
|
+
const argPairs = encodePairs(args);
|
|
63
|
+
if (argPairs) u.searchParams.set("args", argPairs);
|
|
64
|
+
const globalPairs = encodePairs(globals);
|
|
65
|
+
if (globalPairs) u.searchParams.set("globals", globalPairs);
|
|
66
|
+
if (fullscreen) u.searchParams.set("full", "1");
|
|
67
|
+
if (singleStory) u.searchParams.set("singleStory", "true");
|
|
68
|
+
if (query) {
|
|
69
|
+
for (const [k, v] of Object.entries(query)) {
|
|
70
|
+
if (v === void 0) continue;
|
|
71
|
+
u.searchParams.set(k, String(v));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return u.toString();
|
|
75
|
+
}
|
|
41
76
|
|
|
42
77
|
// src/cappa/util.ts
|
|
43
78
|
var freezeUI = async (page) => {
|
|
@@ -123,8 +158,7 @@ var cappaPluginStorybook = (options) => {
|
|
|
123
158
|
return {
|
|
124
159
|
name: "StorybookPlugin",
|
|
125
160
|
description: "Takes screenshots of Storybook stories with configurable options",
|
|
126
|
-
|
|
127
|
-
execute: async (screenshotTool) => {
|
|
161
|
+
discover: async () => {
|
|
128
162
|
if (!options) {
|
|
129
163
|
throw new Error("Storybook plugin options are required");
|
|
130
164
|
}
|
|
@@ -135,7 +169,8 @@ var cappaPluginStorybook = (options) => {
|
|
|
135
169
|
const {
|
|
136
170
|
storybookUrl,
|
|
137
171
|
includeStories = [],
|
|
138
|
-
excludeStories = []
|
|
172
|
+
excludeStories = [],
|
|
173
|
+
storybook: defaultStorybookOptions
|
|
139
174
|
} = options;
|
|
140
175
|
const storiesUrl = `${storybookUrl}/index.json`;
|
|
141
176
|
logger.debug(`Fetching stories from: ${storiesUrl}`);
|
|
@@ -145,159 +180,170 @@ var cappaPluginStorybook = (options) => {
|
|
|
145
180
|
`Failed to fetch stories: ${response.status} ${response.statusText}`
|
|
146
181
|
);
|
|
147
182
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (includeStories.length > 0) {
|
|
153
|
-
filteredStories = filteredStories.filter(
|
|
154
|
-
(story) => includeStories.some(
|
|
155
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
156
|
-
)
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
if (excludeStories.length > 0) {
|
|
160
|
-
filteredStories = filteredStories.filter(
|
|
161
|
-
(story) => !excludeStories.some(
|
|
162
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
163
|
-
)
|
|
164
|
-
);
|
|
165
|
-
}
|
|
183
|
+
const data = await response.json();
|
|
184
|
+
const stories = Object.values(data.entries);
|
|
185
|
+
let filteredStories = stories;
|
|
186
|
+
if (includeStories.length > 0) {
|
|
166
187
|
filteredStories = filteredStories.filter(
|
|
167
|
-
(story) =>
|
|
188
|
+
(story) => includeStories.some(
|
|
189
|
+
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
190
|
+
)
|
|
168
191
|
);
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
await screenshotTool.page?.exposeFunction(
|
|
176
|
-
"__cappa_parameters",
|
|
177
|
-
async (payload) => {
|
|
178
|
-
currentLatch.resolve(payload);
|
|
179
|
-
}
|
|
192
|
+
}
|
|
193
|
+
if (excludeStories.length > 0) {
|
|
194
|
+
filteredStories = filteredStories.filter(
|
|
195
|
+
(story) => !excludeStories.some(
|
|
196
|
+
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
197
|
+
)
|
|
180
198
|
);
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
fullPage: options2.fullPage,
|
|
224
|
-
mask: options2.mask?.map(
|
|
225
|
-
(selector) => page.locator(selector)
|
|
226
|
-
),
|
|
227
|
-
omitBackground: options2.omitBackground,
|
|
228
|
-
saveDiffImage: true
|
|
229
|
-
}
|
|
230
|
-
);
|
|
231
|
-
actualFilepath = screenshotPath;
|
|
232
|
-
results.push({
|
|
233
|
-
storyId: story.id,
|
|
234
|
-
storyName: `${story.title} - ${story.name}`,
|
|
235
|
-
filepath: actualFilepath,
|
|
236
|
-
success: comparisonResult.passed
|
|
237
|
-
});
|
|
238
|
-
} else {
|
|
239
|
-
logger.info(
|
|
240
|
-
`Expected image not found for story ${story.title} - ${story.name}, taking screenshot`
|
|
241
|
-
);
|
|
242
|
-
actualFilepath = await screenshotTool.takeScreenshot(
|
|
243
|
-
page,
|
|
244
|
-
filename,
|
|
245
|
-
{
|
|
246
|
-
fullPage: options2.fullPage,
|
|
247
|
-
mask: options2.mask?.map((selector) => page.locator(selector)),
|
|
248
|
-
omitBackground: options2.omitBackground
|
|
249
|
-
}
|
|
250
|
-
);
|
|
251
|
-
results.push({
|
|
252
|
-
storyId: story.id,
|
|
253
|
-
storyName: `${story.title} - ${story.name}`,
|
|
254
|
-
filepath: actualFilepath,
|
|
255
|
-
success: true
|
|
256
|
-
});
|
|
199
|
+
}
|
|
200
|
+
filteredStories = filteredStories.filter(
|
|
201
|
+
(story) => story.type === "story"
|
|
202
|
+
);
|
|
203
|
+
logger.start(`Found ${filteredStories.length} stories to screenshot...`);
|
|
204
|
+
return filteredStories.map((story) => ({
|
|
205
|
+
id: story.id,
|
|
206
|
+
url: buildStorybookIframeUrl({
|
|
207
|
+
baseUrl: storybookUrl,
|
|
208
|
+
storyId: story.id,
|
|
209
|
+
viewMode: defaultStorybookOptions?.viewMode,
|
|
210
|
+
args: defaultStorybookOptions?.args,
|
|
211
|
+
globals: defaultStorybookOptions?.globals,
|
|
212
|
+
query: defaultStorybookOptions?.query,
|
|
213
|
+
fullscreen: defaultStorybookOptions?.fullscreen,
|
|
214
|
+
singleStory: defaultStorybookOptions?.singleStory
|
|
215
|
+
}),
|
|
216
|
+
data: {
|
|
217
|
+
story
|
|
218
|
+
}
|
|
219
|
+
}));
|
|
220
|
+
},
|
|
221
|
+
initPage: async () => {
|
|
222
|
+
return {
|
|
223
|
+
latchMap: /* @__PURE__ */ new Map()
|
|
224
|
+
};
|
|
225
|
+
},
|
|
226
|
+
// Phase 2: Execute single story
|
|
227
|
+
execute: async (task, page, screenshotTool, context) => {
|
|
228
|
+
const { url, data } = task;
|
|
229
|
+
const { story } = data;
|
|
230
|
+
const logger = (0, import_logger.getLogger)();
|
|
231
|
+
try {
|
|
232
|
+
const currentLatch = createLatch();
|
|
233
|
+
context.latchMap.set(story.id, currentLatch);
|
|
234
|
+
if (await page.evaluate(() => {
|
|
235
|
+
return window.__cappa_parameters === void 0;
|
|
236
|
+
})) {
|
|
237
|
+
await page.exposeFunction(
|
|
238
|
+
"__cappa_parameters",
|
|
239
|
+
async (id, payload) => {
|
|
240
|
+
context.latchMap.get(id)?.resolve?.(payload);
|
|
257
241
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
page.on("console", (message) => {
|
|
245
|
+
logger.debug("console", message.text());
|
|
246
|
+
});
|
|
247
|
+
page.on("pageerror", (error) => {
|
|
248
|
+
logger.debug("pageerror", error);
|
|
249
|
+
});
|
|
250
|
+
await page.goto(url);
|
|
251
|
+
const storyParameters = await context.latchMap.get(story.id)?.p;
|
|
252
|
+
context.latchMap.delete(story.id);
|
|
253
|
+
const variantParameters = storyParameters?.variants ?? [];
|
|
254
|
+
if (storyParameters?.skip) {
|
|
255
|
+
logger.warn(`Skipping story ${story.title} - ${story.name}`);
|
|
256
|
+
return {
|
|
257
|
+
storyId: story.id,
|
|
258
|
+
storyName: `${story.title} - ${story.name}`,
|
|
259
|
+
skipped: true
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
if (storyParameters && Object.keys(storyParameters).length > 0) {
|
|
263
|
+
logger.debug("Taking screenshot with options", storyParameters);
|
|
269
264
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
265
|
+
const resolvedViewport = storyParameters?.viewport ?? screenshotTool.viewport;
|
|
266
|
+
await page.setViewportSize(resolvedViewport);
|
|
267
|
+
await freezeUI(page);
|
|
268
|
+
await waitForVisualIdle(page);
|
|
269
|
+
const toLocatorMask = (mask) => mask?.map((selector) => page.locator(selector));
|
|
270
|
+
const screenshotVariants = variantParameters.map(
|
|
271
|
+
(variant) => ({
|
|
272
|
+
id: variant.id,
|
|
273
|
+
label: variant.label,
|
|
274
|
+
filename: variant.filename,
|
|
275
|
+
options: variant.options ? {
|
|
276
|
+
...variant.options,
|
|
277
|
+
mask: toLocatorMask(variant.options.mask)
|
|
278
|
+
} : void 0
|
|
279
|
+
})
|
|
280
|
+
);
|
|
281
|
+
const screenshotOptions = {
|
|
282
|
+
fullPage: storyParameters?.fullPage,
|
|
283
|
+
delay: storyParameters?.delay,
|
|
284
|
+
omitBackground: storyParameters?.omitBackground,
|
|
285
|
+
skip: storyParameters?.skip,
|
|
286
|
+
viewport: storyParameters?.viewport ?? screenshotTool.viewport,
|
|
287
|
+
mask: toLocatorMask(storyParameters?.mask),
|
|
288
|
+
variants: screenshotVariants.length ? screenshotVariants : void 0
|
|
289
|
+
};
|
|
290
|
+
const filename = buildFilename(story);
|
|
291
|
+
const expectedExists = screenshotTool.filesystem.hasExpectedFile(filename);
|
|
292
|
+
if (!expectedExists) {
|
|
293
|
+
logger.info(
|
|
294
|
+
`Expected image not found for story ${story.title} - ${story.name}, taking screenshot`
|
|
274
295
|
);
|
|
275
296
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
297
|
+
const variantFilenameMap = /* @__PURE__ */ new Map();
|
|
298
|
+
for (const variant of screenshotVariants) {
|
|
299
|
+
const variantFilename = screenshotTool.getVariantFilename(
|
|
300
|
+
filename,
|
|
301
|
+
variant
|
|
280
302
|
);
|
|
303
|
+
variantFilenameMap.set(variant.id, variantFilename);
|
|
304
|
+
if (!screenshotTool.filesystem.hasExpectedFile(variantFilename)) {
|
|
305
|
+
logger.info(
|
|
306
|
+
`Expected image not found for variant ${story.title} - ${story.name} [${variant.label ?? variant.id}], taking screenshot`
|
|
307
|
+
);
|
|
308
|
+
}
|
|
281
309
|
}
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
310
|
+
const variantExtrasEntries = screenshotVariants.map(
|
|
311
|
+
(variant) => [
|
|
312
|
+
variant.id,
|
|
313
|
+
{
|
|
314
|
+
saveDiffImage: true,
|
|
315
|
+
diffImageFilename: variantFilenameMap.get(variant.id) ?? filename
|
|
316
|
+
}
|
|
317
|
+
]
|
|
285
318
|
);
|
|
286
|
-
const
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
return
|
|
319
|
+
const captureExtras = {
|
|
320
|
+
saveDiffImage: true,
|
|
321
|
+
diffImageFilename: filename,
|
|
322
|
+
variants: variantExtrasEntries.length ? Object.fromEntries(variantExtrasEntries) : void 0
|
|
323
|
+
};
|
|
324
|
+
const captureResult = await screenshotTool.capture(
|
|
325
|
+
page,
|
|
326
|
+
filename,
|
|
327
|
+
screenshotOptions,
|
|
328
|
+
captureExtras
|
|
329
|
+
);
|
|
330
|
+
return {
|
|
331
|
+
storyId: story.id,
|
|
332
|
+
storyName: `${story.title} - ${story.name}`,
|
|
333
|
+
filepath: captureResult.base.filepath,
|
|
334
|
+
success: captureResult.base.skipped ? void 0 : captureResult.base.comparisonResult ? captureResult.base.comparisonResult.passed : Boolean(captureResult.base.filepath),
|
|
335
|
+
skipped: captureResult.base.skipped
|
|
336
|
+
};
|
|
298
337
|
} catch (error) {
|
|
299
|
-
logger.error(
|
|
300
|
-
|
|
338
|
+
logger.error(
|
|
339
|
+
`Error taking screenshot of story ${story.title} - ${story.name}:`,
|
|
340
|
+
error.message
|
|
341
|
+
);
|
|
342
|
+
return {
|
|
343
|
+
storyId: story.id,
|
|
344
|
+
storyName: `${story.title} - ${story.name}`,
|
|
345
|
+
error: error?.message ?? "Unknown error"
|
|
346
|
+
};
|
|
301
347
|
}
|
|
302
348
|
}
|
|
303
349
|
};
|