@softure-ai/marketing-kit 0.1.10 → 0.1.12
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 +21 -0
- package/README.md +77 -7
- package/dist/config/schema.d.ts +2428 -254
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config/schema.js +8 -1
- package/dist/config/schema.js.map +1 -1
- package/dist/config/shot-steps.d.ts +620 -0
- package/dist/config/shot-steps.d.ts.map +1 -1
- package/dist/config/shot-steps.js +31 -2
- package/dist/config/shot-steps.js.map +1 -1
- package/dist/screenshot/gates.d.ts +15 -6
- package/dist/screenshot/gates.d.ts.map +1 -1
- package/dist/screenshot/gates.js +24 -7
- package/dist/screenshot/gates.js.map +1 -1
- package/dist/screenshot/screenshot.d.ts.map +1 -1
- package/dist/screenshot/screenshot.js +107 -17
- package/dist/screenshot/screenshot.js.map +1 -1
- package/dist/screenshot/steps.d.ts +3 -2
- package/dist/screenshot/steps.d.ts.map +1 -1
- package/dist/screenshot/steps.js +60 -2
- package/dist/screenshot/steps.js.map +1 -1
- package/package.json +1 -1
- package/schema/marketing.schema.json +2026 -291
- package/src/config/schema.ts +8 -1
- package/src/config/shot-steps.ts +31 -2
- package/src/screenshot/gates.ts +24 -7
- package/src/screenshot/screenshot.ts +106 -17
- package/src/screenshot/steps.ts +58 -3
package/src/config/schema.ts
CHANGED
|
@@ -502,12 +502,19 @@ export const screenshotSchema = z.strictObject({
|
|
|
502
502
|
.describe("Done in order after the page loaded, before any gate reads it, e.g. a click that opens a collapsed section; a failing step refuses the shot."),
|
|
503
503
|
crop: cropSchema
|
|
504
504
|
.optional()
|
|
505
|
-
.describe("Capture one element at a fixed aspect ratio (e.g. 4:3 around a chart) instead of the viewport: as wide as the element, from its top edge. Not with full or scrollTo."),
|
|
505
|
+
.describe("Capture one element at a fixed aspect ratio (e.g. 4:3 around a chart) instead of the viewport: as wide as the element, from its top edge (or crop.top's). Not with full or scrollTo."),
|
|
506
|
+
hide: z
|
|
507
|
+
.array(z.string().regex(SELECTOR_PATTERN, "must be a CSS selector without { } ; < > \\ or /*"))
|
|
508
|
+
.default([])
|
|
509
|
+
.describe("CSS selectors hidden in this entry's shots only, on top of app.hideSelectors (a print state: hint buttons, disclosure arrows); the shot is refused when one still shows an element in the frame."),
|
|
506
510
|
}).superRefine((entry, context) => {
|
|
507
511
|
if (entry.full && entry.scrollTo !== undefined) {
|
|
508
512
|
context.addIssue({ code: "custom", path: ["scrollTo"], message: "is one viewport frame at a scroll position; a full-page shot has none, so drop full or scrollTo" });
|
|
509
513
|
}
|
|
510
514
|
if (entry.crop !== undefined && entry.full) context.addIssue({ code: "custom", path: ["crop"], message: "frames one element; a full-page shot has no element to frame, so drop full or crop" });
|
|
515
|
+
if (entry.crop?.fill === true && entry.crop.top !== undefined) {
|
|
516
|
+
context.addIssue({ code: "custom", path: ["crop", "fill"], message: "centres the target's content in the frame; crop.top would start the frame inside it, so drop one of them" });
|
|
517
|
+
}
|
|
511
518
|
if (entry.crop !== undefined && entry.scrollTo !== undefined) {
|
|
512
519
|
context.addIssue({ code: "custom", path: ["crop"], message: "places the frame on its element; scrollTo would place it too, so drop one of them" });
|
|
513
520
|
}
|
package/src/config/shot-steps.ts
CHANGED
|
@@ -4,14 +4,17 @@ import { locatorSchema } from "./actions-schema.js";
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* What a screenshot does on the page before it is judged: the sign-in form (`signIn.steps`) and an entry's own
|
|
7
|
-
* `steps` (open a
|
|
8
|
-
* records nothing in between. `crop` frames one element at a fixed aspect
|
|
7
|
+
* `steps` (open collapsed sections, hide what a still image should not show). Plain Playwright calls on a locator,
|
|
8
|
+
* with no camera and no timing: a screenshot records nothing in between. `crop` frames one element at a fixed aspect
|
|
9
|
+
* ratio instead of the viewport, optionally from the top edge of another one, or stretched to the frame (`fill`).
|
|
9
10
|
*/
|
|
10
11
|
|
|
11
12
|
/** A Playwright key name, modifiers joined with `+`; Playwright itself refuses a name it does not know. */
|
|
12
13
|
const KEY_PATTERN = /^\S+$/;
|
|
13
14
|
/** `W:H` with whole numbers from 1 to 9999, e.g. `4:3`. */
|
|
14
15
|
export const ASPECT_PATTERN = /^[1-9]\d{0,3}:[1-9]\d{0,3}$/;
|
|
16
|
+
/** More columns than a chart ever shows; the cap only keeps a typo from reading as "keep everything". */
|
|
17
|
+
const MAX_KEEP_LAST = 1000;
|
|
15
18
|
|
|
16
19
|
const target = locatorSchema.describe("The element, as a locator descriptor with exactly one of role, text, label, testId, css.");
|
|
17
20
|
|
|
@@ -33,6 +36,25 @@ export const shotStepSchema = z.discriminatedUnion("do", [
|
|
|
33
36
|
do: z.literal("press").describe("Presses a key on the focused element, e.g. Enter."),
|
|
34
37
|
key: z.string().regex(KEY_PATTERN, "must be a key name such as Enter or Escape").describe("A Playwright key name; modifiers joined with +."),
|
|
35
38
|
}),
|
|
39
|
+
z.strictObject({
|
|
40
|
+
do: z.literal("open").describe("Opens every matching <details> element (nth picks one), e.g. every disclosure of a card; fails when a match is not a <details> or does not stay open."),
|
|
41
|
+
target: locatorSchema.describe("The <details> elements, as a locator descriptor with exactly one of role, text, label, testId, css; every match is opened."),
|
|
42
|
+
}),
|
|
43
|
+
z.strictObject({
|
|
44
|
+
do: z.literal("hide").describe("Hides every match but the last keepLast (display: none), e.g. all but the last three columns of a horizontally scrolling chart."),
|
|
45
|
+
target: locatorSchema.describe("The elements, as a locator descriptor with exactly one of role, text, label, testId, css; every match counts."),
|
|
46
|
+
keepLast: z
|
|
47
|
+
.number()
|
|
48
|
+
.int()
|
|
49
|
+
.min(0)
|
|
50
|
+
.max(MAX_KEEP_LAST)
|
|
51
|
+
.default(0)
|
|
52
|
+
.describe(`How many of the last matches stay shown (0-${MAX_KEEP_LAST}, default 0); the step fails when there are not more matches than this.`),
|
|
53
|
+
}),
|
|
54
|
+
z.strictObject({
|
|
55
|
+
do: z.literal("flatten").describe("Removes the top border and top margin of every match, e.g. the line above the first row a print state still shows."),
|
|
56
|
+
target: locatorSchema.describe("The elements, as a locator descriptor with exactly one of role, text, label, testId, css; every match is flattened."),
|
|
57
|
+
}),
|
|
36
58
|
]);
|
|
37
59
|
|
|
38
60
|
export type ShotStep = z.output<typeof shotStepSchema>;
|
|
@@ -44,6 +66,9 @@ export interface Aspect {
|
|
|
44
66
|
|
|
45
67
|
export const cropSchema = z.strictObject({
|
|
46
68
|
target: locatorSchema.describe("The element framed: exactly one match (nth picks one of several)."),
|
|
69
|
+
top: locatorSchema
|
|
70
|
+
.optional()
|
|
71
|
+
.describe("An element inside target whose top edge starts the frame instead of target's (e.g. a row of a card); exactly one match. The width stays target's."),
|
|
47
72
|
aspect: z
|
|
48
73
|
.string()
|
|
49
74
|
.regex(ASPECT_PATTERN, "must be W:H with whole numbers, e.g. 4:3")
|
|
@@ -53,6 +78,10 @@ export const cropSchema = z.strictObject({
|
|
|
53
78
|
})
|
|
54
79
|
.describe("The frame's width to height, e.g. 4:3: as wide as the element (plus padding), from its top edge down."),
|
|
55
80
|
padding: z.number().int().min(0).max(200).default(0).describe("CSS pixels of the page around the element on the left, right and top (0-200, default 0)."),
|
|
81
|
+
fill: z
|
|
82
|
+
.boolean()
|
|
83
|
+
.default(false)
|
|
84
|
+
.describe("Stretches a target shorter than the frame down to the frame's bottom edge and centres its content vertically, so no page shows below it (default false). Not with top."),
|
|
56
85
|
});
|
|
57
86
|
|
|
58
87
|
export type Crop = z.output<typeof cropSchema>;
|
package/src/screenshot/gates.ts
CHANGED
|
@@ -3,12 +3,13 @@ import type { Aspect } from "../config/shot-steps.js";
|
|
|
3
3
|
/**
|
|
4
4
|
* The quality gates a screenshot passes before it is kept: the sign-in worked (signed-in entries), the page
|
|
5
5
|
* answered below HTTP 400, its steps ran, it reached the requested scroll offset, it shows the expected phrase, a
|
|
6
|
-
* crop found its one element and fits the page and the file has the frame's size,
|
|
7
|
-
*
|
|
6
|
+
* crop found its one element and fits the page and the file has the frame's size, none of the entry's hidden
|
|
7
|
+
* selectors still shows an element in the frame, the file is not suspiciously small, and no earlier file of the run
|
|
8
|
+
* has the same bytes. A blank page, an error page, a half-loaded one, an
|
|
8
9
|
* empty account or a frame of the wrong place fails at least one of them.
|
|
9
10
|
*/
|
|
10
11
|
|
|
11
|
-
export const SCREENSHOT_GATES = ["sign-in", "load", "status", "steps", "scroll", "phrase", "crop", "size", "duplicate"] as const;
|
|
12
|
+
export const SCREENSHOT_GATES = ["sign-in", "load", "status", "steps", "scroll", "phrase", "crop", "hide", "size", "duplicate"] as const;
|
|
12
13
|
|
|
13
14
|
export type ScreenshotGate = (typeof SCREENSHOT_GATES)[number];
|
|
14
15
|
|
|
@@ -50,6 +51,8 @@ export type CropFrameResult = { ok: true; frame: Rect } | { ok: false; message:
|
|
|
50
51
|
export interface CropFrameInput {
|
|
51
52
|
/** The element, in document coordinates. */
|
|
52
53
|
element: Rect;
|
|
54
|
+
/** `crop.top`'s top edge in document coordinates: where the frame starts instead of the element's top edge. */
|
|
55
|
+
top?: number;
|
|
53
56
|
/** The page's scrollable size. */
|
|
54
57
|
page: { width: number; height: number };
|
|
55
58
|
aspect: Aspect;
|
|
@@ -58,12 +61,17 @@ export interface CropFrameInput {
|
|
|
58
61
|
|
|
59
62
|
/**
|
|
60
63
|
* The frame of a crop in whole CSS pixels: as wide as the element plus the padding on each side, from the padding
|
|
61
|
-
* above its top edge, as tall as the aspect asks. Refused when
|
|
62
|
-
*
|
|
64
|
+
* above its top edge (or above `top`), as tall as the aspect asks. Refused when `top` lies outside the element, whose
|
|
65
|
+
* frame would then not show it, and when the frame would run past the page, which would leave part of the file empty
|
|
66
|
+
* or cut it short.
|
|
63
67
|
*/
|
|
64
|
-
export function findCropFrame({ element, page, aspect, padding }: CropFrameInput): CropFrameResult {
|
|
68
|
+
export function findCropFrame({ element, top, page, aspect, padding }: CropFrameInput): CropFrameResult {
|
|
69
|
+
if (top !== undefined && top < element.y) return { ok: false, message: `crop.top's top edge lies ${Math.round(element.y - top)} px above crop.target` };
|
|
70
|
+
if (top !== undefined && top >= element.y + element.height) {
|
|
71
|
+
return { ok: false, message: `crop.top's top edge lies ${Math.round(top - element.y - element.height)} px below crop.target's bottom edge` };
|
|
72
|
+
}
|
|
65
73
|
const x = Math.floor(element.x - padding);
|
|
66
|
-
const y = Math.floor(element.y - padding);
|
|
74
|
+
const y = Math.floor((top ?? element.y) - padding);
|
|
67
75
|
const width = Math.round(element.width + 2 * padding);
|
|
68
76
|
const height = Math.round((width * aspect.height) / aspect.width);
|
|
69
77
|
const ratio = `${aspect.width}:${aspect.height}`;
|
|
@@ -88,3 +96,12 @@ export function findDimensionFailure(file: { width: number; height: number }, fr
|
|
|
88
96
|
if (isWithin) return null;
|
|
89
97
|
return `the file is ${file.width}×${file.height} px, not the crop's ${width}×${height} px; deleted`;
|
|
90
98
|
}
|
|
99
|
+
|
|
100
|
+
/** Null when no hidden selector shows an element in the frame; otherwise the first that does, and how many. */
|
|
101
|
+
export function findHideFailure(counts: readonly { selector: string; shown: number | null }[]): string | null {
|
|
102
|
+
for (const { selector, shown } of counts) {
|
|
103
|
+
if (shown === null) return `hide "${selector}" is not a selector the browser can parse`;
|
|
104
|
+
if (shown > 0) return `hide "${selector}" still shows ${shown} element${shown === 1 ? "" : "s"} in the frame`;
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
@@ -4,12 +4,14 @@ import { basename, dirname, extname, join } from "node:path";
|
|
|
4
4
|
|
|
5
5
|
import { chromium, errors, type Browser, type Page } from "playwright";
|
|
6
6
|
|
|
7
|
+
import type { LocatorDescriptor } from "../config/actions-schema.js";
|
|
7
8
|
import type { ColorTheme } from "../config/colors.js";
|
|
8
9
|
import type { MarketingJson } from "../config/schema.js";
|
|
9
10
|
import { getScreenshotNames, getScreenshotShots } from "../config/screenshot-names.js";
|
|
11
|
+
import type { Crop } from "../config/shot-steps.js";
|
|
10
12
|
import { containsPhrase } from "../film.js";
|
|
11
13
|
import { getLocator } from "../record/locator.js";
|
|
12
|
-
import { findCropFrame, findDimensionFailure, findScrollFailure, findSizeFailure, findStatusFailure, type ScreenshotGate } from "./gates.js";
|
|
14
|
+
import { findCropFrame, findDimensionFailure, findHideFailure, findScrollFailure, findSizeFailure, findStatusFailure, type Rect, type ScreenshotGate } from "./gates.js";
|
|
13
15
|
import { readPageText, signIn, type SessionState, type SignInPlan } from "./sign-in.js";
|
|
14
16
|
import { runShotSteps } from "./steps.js";
|
|
15
17
|
|
|
@@ -109,18 +111,97 @@ function readPngSize(file: string): { width: number; height: number } {
|
|
|
109
111
|
return { width: bytes.readUInt32BE(16), height: bytes.readUInt32BE(20) };
|
|
110
112
|
}
|
|
111
113
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
114
|
+
type CropMeasure = { ok: true; frame: Rect } | { ok: false; message: string };
|
|
115
|
+
|
|
116
|
+
/** The one element a crop locator names, in document coordinates; `key` names it in the refusal. */
|
|
117
|
+
async function measureElement(page: Page, descriptor: LocatorDescriptor, key: string): Promise<{ ok: true; rect: Rect } | { ok: false; message: string }> {
|
|
118
|
+
const locator = getLocator(page, descriptor);
|
|
115
119
|
const count = await locator.count();
|
|
116
|
-
if (count === 0) return { ok: false, message:
|
|
117
|
-
if (count > 1) return { ok: false, message:
|
|
118
|
-
const
|
|
119
|
-
const
|
|
120
|
-
return { x:
|
|
120
|
+
if (count === 0) return { ok: false, message: `no element matches ${key}` };
|
|
121
|
+
if (count > 1) return { ok: false, message: `${key} matches ${count} elements; add nth to pick one` };
|
|
122
|
+
const rect = await locator.evaluate((node) => {
|
|
123
|
+
const box = node.getBoundingClientRect();
|
|
124
|
+
return { x: box.left + window.scrollX, y: box.top + window.scrollY, width: box.width, height: box.height };
|
|
121
125
|
});
|
|
126
|
+
return { ok: true, rect };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Rounding of the stretched element's edge against the frame's, in CSS pixels. */
|
|
130
|
+
const FILL_TOLERANCE_PX = 1;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* `crop.fill`: stretches a target shorter than its frame (minus the top padding) to the frame's height and centres its
|
|
134
|
+
* content vertically, with `!important` inline styles in this browser only.
|
|
135
|
+
*/
|
|
136
|
+
async function stretchToFrame(page: Page, crop: Crop, element: Rect): Promise<void> {
|
|
137
|
+
const frameHeight = Math.round(((element.width + 2 * crop.padding) * crop.aspect.height) / crop.aspect.width);
|
|
138
|
+
const height = frameHeight - crop.padding;
|
|
139
|
+
if (element.height >= height) return;
|
|
140
|
+
await getLocator(page, crop.target).evaluate((node, minHeight) => {
|
|
141
|
+
if (!(node instanceof HTMLElement)) return;
|
|
142
|
+
const styles: [string, string][] = [
|
|
143
|
+
["box-sizing", "border-box"],
|
|
144
|
+
["min-height", `${minHeight}px`],
|
|
145
|
+
["display", "flex"],
|
|
146
|
+
["flex-direction", "column"],
|
|
147
|
+
["justify-content", "center"],
|
|
148
|
+
];
|
|
149
|
+
for (const [property, value] of styles) node.style.setProperty(property, value, "important");
|
|
150
|
+
}, height);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The crop's frame from its element (and `crop.top`'s top edge) and the page's scrollable size. */
|
|
154
|
+
async function measureCrop(page: Page, crop: Crop): Promise<CropMeasure> {
|
|
155
|
+
let element = await measureElement(page, crop.target, "crop.target");
|
|
156
|
+
if (!element.ok) return element;
|
|
157
|
+
if (crop.fill) {
|
|
158
|
+
await stretchToFrame(page, crop, element.rect);
|
|
159
|
+
element = await measureElement(page, crop.target, "crop.target");
|
|
160
|
+
if (!element.ok) return element;
|
|
161
|
+
}
|
|
162
|
+
let top: number | undefined;
|
|
163
|
+
if (crop.top !== undefined) {
|
|
164
|
+
const topElement = await measureElement(page, crop.top, "crop.top");
|
|
165
|
+
if (!topElement.ok) return topElement;
|
|
166
|
+
top = topElement.rect.y;
|
|
167
|
+
}
|
|
122
168
|
const size = await page.evaluate(() => ({ width: document.documentElement.scrollWidth, height: document.documentElement.scrollHeight }));
|
|
123
|
-
|
|
169
|
+
const frame = findCropFrame({ element: element.rect, ...(top === undefined ? {} : { top }), page: size, aspect: crop.aspect, padding: crop.padding });
|
|
170
|
+
if (!frame.ok || !crop.fill) return frame;
|
|
171
|
+
const gap = Math.round(frame.frame.y + frame.frame.height - (element.rect.y + element.rect.height));
|
|
172
|
+
if (gap > FILL_TOLERANCE_PX) return { ok: false, message: `crop.fill left crop.target ${gap} px short of the frame's bottom edge (it is not an HTML element, or a script reset its style)` };
|
|
173
|
+
return frame;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* How many rendered elements each hidden selector still matches inside the frame (document coordinates; the
|
|
178
|
+
* viewport when null); `shown: null` for a selector the browser cannot parse.
|
|
179
|
+
*/
|
|
180
|
+
async function countShownHidden(page: Page, selectors: readonly string[], frame: Rect | null): Promise<{ selector: string; shown: number | null }[]> {
|
|
181
|
+
return page.evaluate(
|
|
182
|
+
({ selectors, frame }) => {
|
|
183
|
+
const area = frame ?? { x: window.scrollX, y: window.scrollY, width: window.innerWidth, height: window.innerHeight };
|
|
184
|
+
return selectors.map((selector) => {
|
|
185
|
+
let nodes: Element[];
|
|
186
|
+
try {
|
|
187
|
+
nodes = [...document.querySelectorAll(selector)];
|
|
188
|
+
} catch {
|
|
189
|
+
// A selector the browser cannot parse is reported by the gate as such.
|
|
190
|
+
return { selector, shown: null };
|
|
191
|
+
}
|
|
192
|
+
const shown = nodes.filter((node) => {
|
|
193
|
+
if (!node.checkVisibility({ opacityProperty: true, visibilityProperty: true })) return false;
|
|
194
|
+
const box = node.getBoundingClientRect();
|
|
195
|
+
if (box.width === 0 || box.height === 0) return false;
|
|
196
|
+
const left = box.left + window.scrollX;
|
|
197
|
+
const top = box.top + window.scrollY;
|
|
198
|
+
return left < area.x + area.width && left + box.width > area.x && top < area.y + area.height && top + box.height > area.y;
|
|
199
|
+
}).length;
|
|
200
|
+
return { selector, shown };
|
|
201
|
+
});
|
|
202
|
+
},
|
|
203
|
+
{ selectors, frame },
|
|
204
|
+
);
|
|
124
205
|
}
|
|
125
206
|
|
|
126
207
|
async function takeOne(page: Page, target: ShotTarget): Promise<ScreenshotResult> {
|
|
@@ -164,15 +245,22 @@ async function captureLoadedPage(page: Page, target: ShotTarget): Promise<Screen
|
|
|
164
245
|
if (!(await waitForPhrase(page, entry.expect))) {
|
|
165
246
|
return { ok: false, id: entry.id, name, gate: "phrase", message: `${url} does not show "${entry.expect}"` };
|
|
166
247
|
}
|
|
167
|
-
let frame:
|
|
168
|
-
if (entry.crop
|
|
248
|
+
let frame: Rect | null = null;
|
|
249
|
+
if (entry.crop !== undefined) {
|
|
250
|
+
const crop = await measureCrop(page, entry.crop);
|
|
251
|
+
if (!crop.ok) return { ok: false, id: entry.id, name, gate: "crop", message: crop.message };
|
|
252
|
+
frame = crop.frame;
|
|
253
|
+
}
|
|
254
|
+
if (entry.hide.length > 0) {
|
|
255
|
+
const area = frame ?? (entry.full ? await page.evaluate(() => ({ x: 0, y: 0, width: document.documentElement.scrollWidth, height: document.documentElement.scrollHeight })) : null);
|
|
256
|
+
const hideFailure = findHideFailure(await countShownHidden(page, entry.hide, area));
|
|
257
|
+
if (hideFailure !== null) return { ok: false, id: entry.id, name, gate: "hide", message: hideFailure };
|
|
258
|
+
}
|
|
259
|
+
if (frame === null) {
|
|
169
260
|
await page.screenshot({ path: file, fullPage: entry.full });
|
|
170
261
|
} else {
|
|
171
|
-
const crop = await measureCrop(page, { ...entry, crop: entry.crop });
|
|
172
|
-
if (!crop.ok) return { ok: false, id: entry.id, name, gate: "crop", message: crop.message };
|
|
173
262
|
// Document coordinates with fullPage: a sticky header stays at the page's top instead of over the element.
|
|
174
|
-
await page.screenshot({ path: file, fullPage: true, clip:
|
|
175
|
-
frame = crop.frame;
|
|
263
|
+
await page.screenshot({ path: file, fullPage: true, clip: frame });
|
|
176
264
|
}
|
|
177
265
|
if (frame !== null) {
|
|
178
266
|
const dimensionFailure = findDimensionFailure(readPngSize(file), frame, entry.scale);
|
|
@@ -223,7 +311,8 @@ async function takeShot(options: TakeShotOptions): Promise<ScreenshotResult> {
|
|
|
223
311
|
// One tag per selector, so a selector the browser cannot parse drops only its own rule;
|
|
224
312
|
// instant scrolling, because "smooth" would still be moving when the full page is captured.
|
|
225
313
|
// An init script, so the rules hold from the first paint, before the gates read the page.
|
|
226
|
-
const
|
|
314
|
+
const hidden = [...settings.hideSelectors, ...target.entry.hide];
|
|
315
|
+
const css = [...hidden.map((selector) => `${selector}{display:none!important}`), "html{scroll-behavior:auto!important}"];
|
|
227
316
|
await page.addInitScript((rules: string[]) => {
|
|
228
317
|
document.addEventListener("DOMContentLoaded", () => {
|
|
229
318
|
for (const rule of rules) {
|
package/src/screenshot/steps.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import type { Page } from "playwright";
|
|
1
|
+
import type { Locator, Page } from "playwright";
|
|
2
2
|
|
|
3
3
|
import type { ShotStep } from "../config/shot-steps.js";
|
|
4
4
|
import { getLocator } from "../record/locator.js";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Steps of the sign-in and of a screenshot entry, done in order on the page. A step that cannot be done (its
|
|
8
|
-
* element never appears, matches several, is not an input) is the page's or the config's
|
|
9
|
-
* a message naming the step; the message never carries a `fill` value, which may be a
|
|
8
|
+
* element never appears, matches several, is not an input, is not a `<details>`) is the page's or the config's
|
|
9
|
+
* fault, so it comes back as a message naming the step; the message never carries a `fill` value, which may be a
|
|
10
|
+
* password. `open`, `hide` and `flatten` act on every match of their target; the others need exactly one.
|
|
10
11
|
*/
|
|
11
12
|
|
|
12
13
|
/** Long enough for a client-side page to render the element, short enough that a typo does not hang the run. */
|
|
@@ -27,9 +28,63 @@ async function runStep(page: Page, step: ShotStep): Promise<void> {
|
|
|
27
28
|
return getLocator(page, step.target).check({ timeout });
|
|
28
29
|
case "press":
|
|
29
30
|
return page.keyboard.press(step.key);
|
|
31
|
+
case "open":
|
|
32
|
+
return openEvery(getLocator(page, step.target));
|
|
33
|
+
case "hide":
|
|
34
|
+
return hideAllButLast(getLocator(page, step.target), step.keepLast);
|
|
35
|
+
case "flatten":
|
|
36
|
+
return flattenEvery(getLocator(page, step.target));
|
|
30
37
|
}
|
|
31
38
|
}
|
|
32
39
|
|
|
40
|
+
/** Waits for the first match like the other steps do; a step on every match needs at least one. */
|
|
41
|
+
async function waitForMatches(locator: Locator): Promise<void> {
|
|
42
|
+
await locator.first().waitFor({ state: "attached", timeout: STEP_TIMEOUT_MS });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Opens every match; refuses a match that is not a `<details>` and one that did not stay open. */
|
|
46
|
+
async function openEvery(locator: Locator): Promise<void> {
|
|
47
|
+
await waitForMatches(locator);
|
|
48
|
+
const outcome = await locator.evaluateAll((nodes) => {
|
|
49
|
+
const notDetails = nodes.filter((node) => !(node instanceof HTMLDetailsElement)).length;
|
|
50
|
+
if (notDetails > 0) return { total: nodes.length, notDetails, closed: 0 };
|
|
51
|
+
for (const node of nodes) (node as HTMLDetailsElement).open = true;
|
|
52
|
+
// Details sharing a name form an exclusive group: opening one closes the others.
|
|
53
|
+
const closed = nodes.filter((node) => !(node as HTMLDetailsElement).open).length;
|
|
54
|
+
return { total: nodes.length, notDetails, closed };
|
|
55
|
+
});
|
|
56
|
+
if (outcome.notDetails > 0) {
|
|
57
|
+
const verb = outcome.notDetails === 1 ? "is" : "are";
|
|
58
|
+
throw new Error(`${outcome.notDetails} of ${outcome.total} matches ${verb} not a <details> element`);
|
|
59
|
+
}
|
|
60
|
+
if (outcome.closed > 0) throw new Error(`${outcome.closed} of ${outcome.total} <details> did not stay open (details sharing a name show one at a time)`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Hides every match but the last `keepLast`; refuses when that would hide nothing (the selector is likely wrong). */
|
|
64
|
+
async function hideAllButLast(locator: Locator, keepLast: number): Promise<void> {
|
|
65
|
+
await waitForMatches(locator);
|
|
66
|
+
const total = await locator.evaluateAll((nodes, keep) => {
|
|
67
|
+
if (nodes.length <= keep) return nodes.length;
|
|
68
|
+
for (const node of nodes.slice(0, nodes.length - keep)) {
|
|
69
|
+
if (node instanceof HTMLElement || node instanceof SVGElement) node.style.setProperty("display", "none", "important");
|
|
70
|
+
}
|
|
71
|
+
return nodes.length;
|
|
72
|
+
}, keepLast);
|
|
73
|
+
if (total <= keepLast) throw new Error(`matches ${total} elements, not more than keepLast ${keepLast}, so nothing would be hidden`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Drops the top border and top margin of every match, with `!important` like `hide`. */
|
|
77
|
+
async function flattenEvery(locator: Locator): Promise<void> {
|
|
78
|
+
await waitForMatches(locator);
|
|
79
|
+
await locator.evaluateAll((nodes) => {
|
|
80
|
+
for (const node of nodes) {
|
|
81
|
+
if (!(node instanceof HTMLElement || node instanceof SVGElement)) continue;
|
|
82
|
+
node.style.setProperty("border-top", "none", "important");
|
|
83
|
+
node.style.setProperty("margin-top", "0", "important");
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
33
88
|
/** Null when every step was done; otherwise which one failed (`<where>[<index>] (<do>)`) and why. */
|
|
34
89
|
export async function runShotSteps(page: Page, steps: readonly ShotStep[], where: string): Promise<string | null> {
|
|
35
90
|
for (const [index, step] of steps.entries()) {
|