@softure-ai/marketing-kit 0.1.10 → 0.1.11
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 +12 -0
- package/README.md +47 -7
- package/dist/config/schema.d.ts +1832 -280
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config/schema.js +5 -1
- package/dist/config/schema.js.map +1 -1
- package/dist/config/shot-steps.d.ts +464 -0
- package/dist/config/shot-steps.d.ts.map +1 -1
- package/dist/config/shot-steps.js +23 -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 +70 -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 +46 -2
- package/dist/screenshot/steps.js.map +1 -1
- package/package.json +1 -1
- package/schema/marketing.schema.json +1436 -198
- package/src/config/schema.ts +5 -1
- package/src/config/shot-steps.ts +23 -2
- package/src/screenshot/gates.ts +24 -7
- package/src/screenshot/screenshot.ts +73 -17
- package/src/screenshot/steps.ts +44 -3
package/src/config/schema.ts
CHANGED
|
@@ -502,7 +502,11 @@ 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" });
|
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.
|
|
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,21 @@ 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
|
+
}),
|
|
36
54
|
]);
|
|
37
55
|
|
|
38
56
|
export type ShotStep = z.output<typeof shotStepSchema>;
|
|
@@ -44,6 +62,9 @@ export interface Aspect {
|
|
|
44
62
|
|
|
45
63
|
export const cropSchema = z.strictObject({
|
|
46
64
|
target: locatorSchema.describe("The element framed: exactly one match (nth picks one of several)."),
|
|
65
|
+
top: locatorSchema
|
|
66
|
+
.optional()
|
|
67
|
+
.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
68
|
aspect: z
|
|
48
69
|
.string()
|
|
49
70
|
.regex(ASPECT_PATTERN, "must be W:H with whole numbers, e.g. 4:3")
|
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,64 @@ 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
|
+
/** The crop's frame from its element (and `crop.top`'s top edge) and the page's scrollable size. */
|
|
130
|
+
async function measureCrop(page: Page, crop: Crop): Promise<CropMeasure> {
|
|
131
|
+
const element = await measureElement(page, crop.target, "crop.target");
|
|
132
|
+
if (!element.ok) return element;
|
|
133
|
+
let top: number | undefined;
|
|
134
|
+
if (crop.top !== undefined) {
|
|
135
|
+
const topElement = await measureElement(page, crop.top, "crop.top");
|
|
136
|
+
if (!topElement.ok) return topElement;
|
|
137
|
+
top = topElement.rect.y;
|
|
138
|
+
}
|
|
122
139
|
const size = await page.evaluate(() => ({ width: document.documentElement.scrollWidth, height: document.documentElement.scrollHeight }));
|
|
123
|
-
return findCropFrame({ element, page: size, aspect:
|
|
140
|
+
return findCropFrame({ element: element.rect, ...(top === undefined ? {} : { top }), page: size, aspect: crop.aspect, padding: crop.padding });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* How many rendered elements each hidden selector still matches inside the frame (document coordinates; the
|
|
145
|
+
* viewport when null); `shown: null` for a selector the browser cannot parse.
|
|
146
|
+
*/
|
|
147
|
+
async function countShownHidden(page: Page, selectors: readonly string[], frame: Rect | null): Promise<{ selector: string; shown: number | null }[]> {
|
|
148
|
+
return page.evaluate(
|
|
149
|
+
({ selectors, frame }) => {
|
|
150
|
+
const area = frame ?? { x: window.scrollX, y: window.scrollY, width: window.innerWidth, height: window.innerHeight };
|
|
151
|
+
return selectors.map((selector) => {
|
|
152
|
+
let nodes: Element[];
|
|
153
|
+
try {
|
|
154
|
+
nodes = [...document.querySelectorAll(selector)];
|
|
155
|
+
} catch {
|
|
156
|
+
// A selector the browser cannot parse is reported by the gate as such.
|
|
157
|
+
return { selector, shown: null };
|
|
158
|
+
}
|
|
159
|
+
const shown = nodes.filter((node) => {
|
|
160
|
+
if (!node.checkVisibility({ opacityProperty: true, visibilityProperty: true })) return false;
|
|
161
|
+
const box = node.getBoundingClientRect();
|
|
162
|
+
if (box.width === 0 || box.height === 0) return false;
|
|
163
|
+
const left = box.left + window.scrollX;
|
|
164
|
+
const top = box.top + window.scrollY;
|
|
165
|
+
return left < area.x + area.width && left + box.width > area.x && top < area.y + area.height && top + box.height > area.y;
|
|
166
|
+
}).length;
|
|
167
|
+
return { selector, shown };
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
{ selectors, frame },
|
|
171
|
+
);
|
|
124
172
|
}
|
|
125
173
|
|
|
126
174
|
async function takeOne(page: Page, target: ShotTarget): Promise<ScreenshotResult> {
|
|
@@ -164,15 +212,22 @@ async function captureLoadedPage(page: Page, target: ShotTarget): Promise<Screen
|
|
|
164
212
|
if (!(await waitForPhrase(page, entry.expect))) {
|
|
165
213
|
return { ok: false, id: entry.id, name, gate: "phrase", message: `${url} does not show "${entry.expect}"` };
|
|
166
214
|
}
|
|
167
|
-
let frame:
|
|
168
|
-
if (entry.crop
|
|
215
|
+
let frame: Rect | null = null;
|
|
216
|
+
if (entry.crop !== undefined) {
|
|
217
|
+
const crop = await measureCrop(page, entry.crop);
|
|
218
|
+
if (!crop.ok) return { ok: false, id: entry.id, name, gate: "crop", message: crop.message };
|
|
219
|
+
frame = crop.frame;
|
|
220
|
+
}
|
|
221
|
+
if (entry.hide.length > 0) {
|
|
222
|
+
const area = frame ?? (entry.full ? await page.evaluate(() => ({ x: 0, y: 0, width: document.documentElement.scrollWidth, height: document.documentElement.scrollHeight })) : null);
|
|
223
|
+
const hideFailure = findHideFailure(await countShownHidden(page, entry.hide, area));
|
|
224
|
+
if (hideFailure !== null) return { ok: false, id: entry.id, name, gate: "hide", message: hideFailure };
|
|
225
|
+
}
|
|
226
|
+
if (frame === null) {
|
|
169
227
|
await page.screenshot({ path: file, fullPage: entry.full });
|
|
170
228
|
} 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
229
|
// 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;
|
|
230
|
+
await page.screenshot({ path: file, fullPage: true, clip: frame });
|
|
176
231
|
}
|
|
177
232
|
if (frame !== null) {
|
|
178
233
|
const dimensionFailure = findDimensionFailure(readPngSize(file), frame, entry.scale);
|
|
@@ -223,7 +278,8 @@ async function takeShot(options: TakeShotOptions): Promise<ScreenshotResult> {
|
|
|
223
278
|
// One tag per selector, so a selector the browser cannot parse drops only its own rule;
|
|
224
279
|
// instant scrolling, because "smooth" would still be moving when the full page is captured.
|
|
225
280
|
// An init script, so the rules hold from the first paint, before the gates read the page.
|
|
226
|
-
const
|
|
281
|
+
const hidden = [...settings.hideSelectors, ...target.entry.hide];
|
|
282
|
+
const css = [...hidden.map((selector) => `${selector}{display:none!important}`), "html{scroll-behavior:auto!important}"];
|
|
227
283
|
await page.addInitScript((rules: string[]) => {
|
|
228
284
|
document.addEventListener("DOMContentLoaded", () => {
|
|
229
285
|
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` and `hide` 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,49 @@ 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);
|
|
30
35
|
}
|
|
31
36
|
}
|
|
32
37
|
|
|
38
|
+
/** Waits for the first match like the other steps do; a step on every match needs at least one. */
|
|
39
|
+
async function waitForMatches(locator: Locator): Promise<void> {
|
|
40
|
+
await locator.first().waitFor({ state: "attached", timeout: STEP_TIMEOUT_MS });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Opens every match; refuses a match that is not a `<details>` and one that did not stay open. */
|
|
44
|
+
async function openEvery(locator: Locator): Promise<void> {
|
|
45
|
+
await waitForMatches(locator);
|
|
46
|
+
const outcome = await locator.evaluateAll((nodes) => {
|
|
47
|
+
const notDetails = nodes.filter((node) => !(node instanceof HTMLDetailsElement)).length;
|
|
48
|
+
if (notDetails > 0) return { total: nodes.length, notDetails, closed: 0 };
|
|
49
|
+
for (const node of nodes) (node as HTMLDetailsElement).open = true;
|
|
50
|
+
// Details sharing a name form an exclusive group: opening one closes the others.
|
|
51
|
+
const closed = nodes.filter((node) => !(node as HTMLDetailsElement).open).length;
|
|
52
|
+
return { total: nodes.length, notDetails, closed };
|
|
53
|
+
});
|
|
54
|
+
if (outcome.notDetails > 0) {
|
|
55
|
+
const verb = outcome.notDetails === 1 ? "is" : "are";
|
|
56
|
+
throw new Error(`${outcome.notDetails} of ${outcome.total} matches ${verb} not a <details> element`);
|
|
57
|
+
}
|
|
58
|
+
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)`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Hides every match but the last `keepLast`; refuses when that would hide nothing (the selector is likely wrong). */
|
|
62
|
+
async function hideAllButLast(locator: Locator, keepLast: number): Promise<void> {
|
|
63
|
+
await waitForMatches(locator);
|
|
64
|
+
const total = await locator.evaluateAll((nodes, keep) => {
|
|
65
|
+
if (nodes.length <= keep) return nodes.length;
|
|
66
|
+
for (const node of nodes.slice(0, nodes.length - keep)) {
|
|
67
|
+
if (node instanceof HTMLElement || node instanceof SVGElement) node.style.setProperty("display", "none", "important");
|
|
68
|
+
}
|
|
69
|
+
return nodes.length;
|
|
70
|
+
}, keepLast);
|
|
71
|
+
if (total <= keepLast) throw new Error(`matches ${total} elements, not more than keepLast ${keepLast}, so nothing would be hidden`);
|
|
72
|
+
}
|
|
73
|
+
|
|
33
74
|
/** Null when every step was done; otherwise which one failed (`<where>[<index>] (<do>)`) and why. */
|
|
34
75
|
export async function runShotSteps(page: Page, steps: readonly ShotStep[], where: string): Promise<string | null> {
|
|
35
76
|
for (const [index, step] of steps.entries()) {
|