@rind-ui/testing 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +15 -2
- package/dist/adapter-UPhu17S6.d.mts +152 -0
- package/dist/compose-C7RIqRoi.mjs +111 -0
- package/dist/dom-BxnIjEBk.d.mts +28 -0
- package/dist/dom-Ds2Cv9wy.mjs +834 -0
- package/dist/dom.d.mts +2 -0
- package/dist/dom.mjs +1 -0
- package/dist/index.d.mts +2978 -0
- package/dist/index.mjs +1988 -0
- package/dist/inpage.iife.js +4 -0
- package/dist/playwright-CNUCI2Xe.mjs +525 -0
- package/dist/playwright-CtIxjMKr.d.mts +14 -0
- package/dist/playwright.d.mts +29 -0
- package/dist/playwright.mjs +1 -0
- package/dist/setup.d.mts +9 -0
- package/dist/setup.mjs +100 -0
- package/package.json +92 -3
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
import { o as poll, r as describeBy, t as composeInPage } from "./compose-C7RIqRoi.mjs";
|
|
2
|
+
//#region src/adapters/chromium-tree.ts
|
|
3
|
+
const toNode = (n) => ({
|
|
4
|
+
role: n.role?.value ?? "",
|
|
5
|
+
name: n.name?.value ?? "",
|
|
6
|
+
description: n.description?.value ?? "",
|
|
7
|
+
ignored: n.ignored === true,
|
|
8
|
+
ignoredReasons: (n.ignoredReasons ?? []).map((r) => r.name)
|
|
9
|
+
});
|
|
10
|
+
const sessions = /* @__PURE__ */ new WeakMap();
|
|
11
|
+
function session(page) {
|
|
12
|
+
let s = sessions.get(page);
|
|
13
|
+
if (!s) {
|
|
14
|
+
s = (async () => {
|
|
15
|
+
if (page.context().browser()?.browserType().name() !== "chromium") return null;
|
|
16
|
+
const cdp = await page.context().newCDPSession(page);
|
|
17
|
+
await cdp.send("Accessibility.enable");
|
|
18
|
+
await cdp.send("DOM.enable");
|
|
19
|
+
return cdp;
|
|
20
|
+
})().catch(() => null);
|
|
21
|
+
sessions.set(page, s);
|
|
22
|
+
}
|
|
23
|
+
return s;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Chromium's full accessibility tree, retried until it satisfies `until` (the tree updates
|
|
27
|
+
* after the DOM, so a fresh change may not be in it yet); `{ unavailable: true }` in other
|
|
28
|
+
* engines. Throws when `until` still fails after `timeout` ms.
|
|
29
|
+
*/
|
|
30
|
+
async function chromiumTree(page, { until = (nodes) => nodes.length > 0, timeout = 5e3 } = {}) {
|
|
31
|
+
const cdp = await session(page);
|
|
32
|
+
if (!cdp) return { unavailable: true };
|
|
33
|
+
const start = Date.now();
|
|
34
|
+
for (;;) {
|
|
35
|
+
const { nodes } = await cdp.send("Accessibility.getFullAXTree").catch(() => ({ nodes: [] }));
|
|
36
|
+
const tree = nodes.map(toNode);
|
|
37
|
+
if (until(tree)) return tree;
|
|
38
|
+
if (Date.now() - start >= timeout) throw new Error(`chromiumTree: the accessibility tree did not match within ${timeout} ms`);
|
|
39
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** The accessibility node of the element a page expression evaluates to. */
|
|
43
|
+
async function axNodeOf(page, expression) {
|
|
44
|
+
const cdp = await session(page);
|
|
45
|
+
if (!cdp) return { unavailable: true };
|
|
46
|
+
await cdp.send("DOM.getDocument", { depth: 0 });
|
|
47
|
+
const { result } = await cdp.send("Runtime.evaluate", { expression });
|
|
48
|
+
if (!result.objectId) return null;
|
|
49
|
+
const { nodeId } = await cdp.send("DOM.requestNode", { objectId: result.objectId });
|
|
50
|
+
const { nodes } = await cdp.send("Accessibility.getPartialAXTree", {
|
|
51
|
+
nodeId,
|
|
52
|
+
fetchRelatives: false
|
|
53
|
+
});
|
|
54
|
+
const first = nodes[0];
|
|
55
|
+
return first ? toNode(first) : null;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/adapters/playwright.ts
|
|
59
|
+
const PW_KEYS = {
|
|
60
|
+
Enter: "Enter",
|
|
61
|
+
Escape: "Escape",
|
|
62
|
+
Space: " ",
|
|
63
|
+
Tab: "Tab",
|
|
64
|
+
Home: "Home",
|
|
65
|
+
End: "End",
|
|
66
|
+
PageUp: "PageUp",
|
|
67
|
+
PageDown: "PageDown",
|
|
68
|
+
ArrowUp: "ArrowUp",
|
|
69
|
+
ArrowDown: "ArrowDown",
|
|
70
|
+
ArrowLeft: "ArrowLeft",
|
|
71
|
+
ArrowRight: "ArrowRight",
|
|
72
|
+
Backspace: "Backspace",
|
|
73
|
+
Delete: "Delete",
|
|
74
|
+
F10: "F10",
|
|
75
|
+
ContextMenu: "ContextMenu",
|
|
76
|
+
A: "a"
|
|
77
|
+
};
|
|
78
|
+
const keyName = (key, o = {}, apple) => [
|
|
79
|
+
o.shift && "Shift",
|
|
80
|
+
o.alt && "Alt",
|
|
81
|
+
o.mod && (apple === void 0 ? "ControlOrMeta" : apple ? "Meta" : "Control"),
|
|
82
|
+
PW_KEYS[key]
|
|
83
|
+
].filter(Boolean).join("+");
|
|
84
|
+
/** In the page: keydown and keyup with isComposing and keyCode 229 on `el`. */
|
|
85
|
+
const composingKeys = (el, init) => {
|
|
86
|
+
for (const type of ["keydown", "keyup"]) {
|
|
87
|
+
const e = new KeyboardEvent(type, {
|
|
88
|
+
bubbles: true,
|
|
89
|
+
cancelable: true,
|
|
90
|
+
isComposing: true,
|
|
91
|
+
...init
|
|
92
|
+
});
|
|
93
|
+
Object.defineProperty(e, "keyCode", { value: 229 });
|
|
94
|
+
el.dispatchEvent(e);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
let inpageSource;
|
|
98
|
+
/** The in-page bundle, read once from next to this module (dist) or from the built package. */
|
|
99
|
+
function loadInPage() {
|
|
100
|
+
inpageSource ??= (async () => {
|
|
101
|
+
const fs = await import("node:fs/promises");
|
|
102
|
+
for (const candidate of ["./inpage.iife.js", "../../dist/inpage.iife.js"]) try {
|
|
103
|
+
return await fs.readFile(new URL(candidate, import.meta.url), "utf8");
|
|
104
|
+
} catch {}
|
|
105
|
+
throw new Error("rind-ui: dist/inpage.iife.js is missing; build @rind-ui/testing");
|
|
106
|
+
})();
|
|
107
|
+
return inpageSource;
|
|
108
|
+
}
|
|
109
|
+
async function ensureInPage(root) {
|
|
110
|
+
if (!await root.evaluate(() => "__rindTesting" in globalThis)) await root.evaluate(await loadInPage());
|
|
111
|
+
}
|
|
112
|
+
/** A Playwright Page, by duck typing (no runtime import). */
|
|
113
|
+
const isPage = (t) => typeof t === "object" && t !== null && "mainFrame" in t && "on" in t;
|
|
114
|
+
async function baseOf(target) {
|
|
115
|
+
if (isPage(target)) return {
|
|
116
|
+
page: target,
|
|
117
|
+
root: target,
|
|
118
|
+
anchorRoot: target
|
|
119
|
+
};
|
|
120
|
+
if ("page" in target) {
|
|
121
|
+
const locator = target;
|
|
122
|
+
const frame = await (await locator.elementHandle())?.ownerFrame() ?? locator.page().mainFrame();
|
|
123
|
+
return {
|
|
124
|
+
page: frame.page(),
|
|
125
|
+
root: frame,
|
|
126
|
+
anchorRoot: locator
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const frame = await (await target.owner().elementHandle())?.contentFrame();
|
|
130
|
+
if (!frame) throw new Error("rind-ui: the FrameLocator's frame is not attached");
|
|
131
|
+
return {
|
|
132
|
+
page: frame.page(),
|
|
133
|
+
root: frame,
|
|
134
|
+
anchorRoot: frame
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const logged = /* @__PURE__ */ new WeakSet();
|
|
138
|
+
/** Records every announcement in the page, from its first script on (and on the current page). */
|
|
139
|
+
async function installPageAnnouncementLog(page) {
|
|
140
|
+
if (logged.has(page)) return;
|
|
141
|
+
logged.add(page);
|
|
142
|
+
const install = () => {
|
|
143
|
+
const key = Symbol.for("rind-ui.announcements");
|
|
144
|
+
if (!Reflect.get(globalThis, key)) Reflect.set(globalThis, key, []);
|
|
145
|
+
};
|
|
146
|
+
await page.addInitScript(install);
|
|
147
|
+
await page.evaluate(install).catch(() => {});
|
|
148
|
+
}
|
|
149
|
+
const errorLogs = /* @__PURE__ */ new WeakMap();
|
|
150
|
+
/** Records the page's uncaught errors and console.error calls; installed once per page. */
|
|
151
|
+
function watchPageErrors(page) {
|
|
152
|
+
let log = errorLogs.get(page);
|
|
153
|
+
if (!log) {
|
|
154
|
+
const entries = [];
|
|
155
|
+
page.on("pageerror", (e) => entries.push(String(e)));
|
|
156
|
+
page.on("console", (m) => {
|
|
157
|
+
if (m.type() === "error") entries.push(m.text());
|
|
158
|
+
});
|
|
159
|
+
errorLogs.set(page, entries);
|
|
160
|
+
log = entries;
|
|
161
|
+
}
|
|
162
|
+
return log;
|
|
163
|
+
}
|
|
164
|
+
/** The Playwright adapter. */
|
|
165
|
+
function playwrightEnv(target, options = {}) {
|
|
166
|
+
let base;
|
|
167
|
+
const getBase = () => {
|
|
168
|
+
base ??= baseOf(target).then(async (b) => {
|
|
169
|
+
watchPageErrors(b.page);
|
|
170
|
+
await installPageAnnouncementLog(b.page);
|
|
171
|
+
return b;
|
|
172
|
+
});
|
|
173
|
+
return base;
|
|
174
|
+
};
|
|
175
|
+
if (isPage(target)) {
|
|
176
|
+
watchPageErrors(target);
|
|
177
|
+
installPageAnnouncementLog(target);
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
"~rindEnvFactory": true,
|
|
181
|
+
create: (envOptions) => createPlaywrightEnv(getBase, envOptions, options)
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function createPlaywrightEnv(getBase, { scope, anchor, modality, platform }, options) {
|
|
185
|
+
const apple = platform?.apple;
|
|
186
|
+
const composingInit = (key, o = {}) => ({
|
|
187
|
+
key: key === "Space" ? " " : key,
|
|
188
|
+
shiftKey: o.shift === true,
|
|
189
|
+
altKey: o.alt === true,
|
|
190
|
+
ctrlKey: o.mod === true && apple !== true,
|
|
191
|
+
metaKey: o.mod === true && apple === true
|
|
192
|
+
});
|
|
193
|
+
const timeout = options.timeout ?? 5e3;
|
|
194
|
+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
195
|
+
const until = (read, accept) => poll(read, accept, {
|
|
196
|
+
timeout,
|
|
197
|
+
wait
|
|
198
|
+
});
|
|
199
|
+
const byLocator = (root, by) => "role" in by ? root.getByRole(by.role, { ...by.name === void 0 ? {} : {
|
|
200
|
+
name: by.name,
|
|
201
|
+
exact: true
|
|
202
|
+
} }) : root.getByText(by.text, { exact: true });
|
|
203
|
+
const describeAll = (l) => l.evaluateAll((els) => els.map((el) => el.outerHTML.slice(0, 160)).join("\n"));
|
|
204
|
+
/** What tells a navigation: the URL, the open pages, and the in-page log (fakeRouter). */
|
|
205
|
+
const navigationState = async () => {
|
|
206
|
+
const { page, root } = await getBase();
|
|
207
|
+
const log = await root.evaluate(() => (Reflect.get(globalThis, Symbol.for("rind-ui.navigations")) ?? []).map((n) => n.newTab)).catch(() => []);
|
|
208
|
+
return {
|
|
209
|
+
url: page.url(),
|
|
210
|
+
pages: page.context().pages().length,
|
|
211
|
+
log
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
let navigationsFrom;
|
|
215
|
+
const anchorLocator = async () => byLocator((await getBase()).anchorRoot, anchor);
|
|
216
|
+
const instance = async () => {
|
|
217
|
+
const a = await anchorLocator();
|
|
218
|
+
const n = await a.count();
|
|
219
|
+
if (n === 0) throw new Error(`${scope}: no element matches the anchor (${describeBy(anchor)})`);
|
|
220
|
+
if (n > 1) throw new Error(`${scope}: ${n} elements match the anchor (${describeBy(anchor)}):\n${await describeAll(a)}`);
|
|
221
|
+
const id = await a.evaluate((el) => el.closest("[data-instance]")?.getAttribute("data-instance"));
|
|
222
|
+
if (!id) throw new Error(`${scope}: the anchor has no data-instance`);
|
|
223
|
+
return id;
|
|
224
|
+
};
|
|
225
|
+
/** A lazy handle. Reads never wait; actions wait for the element to exist. */
|
|
226
|
+
const handle = (label, locate) => {
|
|
227
|
+
const single = async (l) => {
|
|
228
|
+
const n = await l.count();
|
|
229
|
+
if (n === 0) throw new Error(`${label}: not found`);
|
|
230
|
+
if (n > 1) throw new Error(`${label}: ${n} elements match:\n${await describeAll(l)}`);
|
|
231
|
+
return l;
|
|
232
|
+
};
|
|
233
|
+
const now = async () => single(await locate());
|
|
234
|
+
const waitFor = async () => {
|
|
235
|
+
const l = await locate();
|
|
236
|
+
await until(() => l.count(), (n) => n > 0).catch(() => 0);
|
|
237
|
+
return single(l);
|
|
238
|
+
};
|
|
239
|
+
const activate = async (touch, o = {}) => {
|
|
240
|
+
const l = await waitFor();
|
|
241
|
+
const mods = [o.shift ? "Shift" : null, o.mod ? apple === void 0 ? "ControlOrMeta" : apple ? "Meta" : "Control" : null].filter((m) => m !== null);
|
|
242
|
+
if (modality === "virtual") return l.dispatchEvent("click", {
|
|
243
|
+
detail: 0,
|
|
244
|
+
shiftKey: o.shift === true,
|
|
245
|
+
ctrlKey: o.mod === true && !apple,
|
|
246
|
+
metaKey: o.mod === true && apple
|
|
247
|
+
});
|
|
248
|
+
const force = await l.getAttribute("aria-disabled") === "true";
|
|
249
|
+
if (o.button === "middle") return l.click({
|
|
250
|
+
button: "middle",
|
|
251
|
+
modifiers: mods,
|
|
252
|
+
force
|
|
253
|
+
});
|
|
254
|
+
return touch ? l.tap({
|
|
255
|
+
modifiers: mods,
|
|
256
|
+
force
|
|
257
|
+
}) : l.click({
|
|
258
|
+
modifiers: mods,
|
|
259
|
+
force
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
const part = {
|
|
263
|
+
click: (options) => activate(false, options),
|
|
264
|
+
tap: () => activate(true),
|
|
265
|
+
hover: async (hoverOptions) => {
|
|
266
|
+
const l = await waitFor();
|
|
267
|
+
if (hoverOptions?.pointerType !== "touch") return l.hover();
|
|
268
|
+
await l.dispatchEvent("pointerover", {
|
|
269
|
+
pointerType: "touch",
|
|
270
|
+
bubbles: true
|
|
271
|
+
});
|
|
272
|
+
await l.dispatchEvent("pointerenter", { pointerType: "touch" });
|
|
273
|
+
},
|
|
274
|
+
focus: async () => (await waitFor()).focus(),
|
|
275
|
+
async press(key, keyOptions) {
|
|
276
|
+
const l = await waitFor();
|
|
277
|
+
if (!await l.evaluate((el) => el === document.activeElement)) await l.focus();
|
|
278
|
+
if (keyOptions?.composing) {
|
|
279
|
+
await l.evaluate(composingKeys, composingInit(key, keyOptions));
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
await l.press(keyName(key, keyOptions, apple));
|
|
283
|
+
},
|
|
284
|
+
async type(text) {
|
|
285
|
+
const l = await waitFor();
|
|
286
|
+
await l.evaluate((el) => {
|
|
287
|
+
if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
|
|
288
|
+
if (el.ownerDocument.activeElement !== el) el.focus();
|
|
289
|
+
const end = el.value.length;
|
|
290
|
+
try {
|
|
291
|
+
el.setSelectionRange(end, end);
|
|
292
|
+
} catch {}
|
|
293
|
+
} else if (el instanceof HTMLElement && el.isContentEditable) {
|
|
294
|
+
el.focus();
|
|
295
|
+
const range = el.ownerDocument.createRange();
|
|
296
|
+
range.selectNodeContents(el);
|
|
297
|
+
range.collapse(false);
|
|
298
|
+
el.ownerDocument.getSelection()?.removeAllRanges();
|
|
299
|
+
el.ownerDocument.getSelection()?.addRange(range);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
await l.pressSequentially(text);
|
|
303
|
+
},
|
|
304
|
+
async compose(updates, commit) {
|
|
305
|
+
const l = await waitFor();
|
|
306
|
+
if (!await l.evaluate((el) => el === document.activeElement)) await l.focus();
|
|
307
|
+
const { page } = await getBase();
|
|
308
|
+
if (page.context().browser()?.browserType().name() === "chromium") {
|
|
309
|
+
const cdp = await page.context().newCDPSession(page);
|
|
310
|
+
const imeKey = (type) => cdp.send("Input.dispatchKeyEvent", {
|
|
311
|
+
type,
|
|
312
|
+
key: "Process",
|
|
313
|
+
code: "",
|
|
314
|
+
windowsVirtualKeyCode: 229,
|
|
315
|
+
nativeVirtualKeyCode: 229
|
|
316
|
+
});
|
|
317
|
+
for (const text of updates) {
|
|
318
|
+
await imeKey("rawKeyDown");
|
|
319
|
+
await cdp.send("Input.imeSetComposition", {
|
|
320
|
+
text,
|
|
321
|
+
selectionStart: text.length,
|
|
322
|
+
selectionEnd: text.length
|
|
323
|
+
});
|
|
324
|
+
await imeKey("keyUp");
|
|
325
|
+
}
|
|
326
|
+
if (commit !== void 0) await cdp.send("Input.insertText", { text: commit });
|
|
327
|
+
await cdp.detach();
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
await l.evaluate(composeInPage, commit === void 0 ? { updates } : {
|
|
331
|
+
updates,
|
|
332
|
+
commit
|
|
333
|
+
});
|
|
334
|
+
},
|
|
335
|
+
async pressAtPosition() {
|
|
336
|
+
const box = await (await waitFor()).boundingBox();
|
|
337
|
+
if (!box) throw new Error("pressAtPosition: the part has no box");
|
|
338
|
+
const { page } = await getBase();
|
|
339
|
+
await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2);
|
|
340
|
+
},
|
|
341
|
+
waitForDetach: async () => {
|
|
342
|
+
await until(async () => (await locate()).count(), (n) => n === 0);
|
|
343
|
+
},
|
|
344
|
+
isInert: async () => (await now()).evaluate((el) => el.closest("[inert]") !== null),
|
|
345
|
+
attr: async (name) => (await now()).getAttribute(name),
|
|
346
|
+
tagName: async () => (await now()).evaluate((el) => el.tagName.toLowerCase()),
|
|
347
|
+
scoped: (scope, partName) => handle(`${scope}.${partName} in ${label}`, async () => (await now()).locator(`[data-scope="${scope}"][data-part="${partName}"]`)),
|
|
348
|
+
accessibleName: async () => {
|
|
349
|
+
const { root } = await getBase();
|
|
350
|
+
await ensureInPage(root);
|
|
351
|
+
return (await now()).evaluate((el) => globalThis.__rindTesting?.accessibleName(el) ?? "");
|
|
352
|
+
},
|
|
353
|
+
accessibleDescription: async () => {
|
|
354
|
+
const { root } = await getBase();
|
|
355
|
+
await ensureInPage(root);
|
|
356
|
+
return (await now()).evaluate((el) => globalThis.__rindTesting?.accessibleDescription(el) ?? "");
|
|
357
|
+
},
|
|
358
|
+
labelled: () => handle(`the control of ${label}`, async () => {
|
|
359
|
+
const text = (await (await now()).textContent() ?? "").replace(/\s+/g, " ").trim();
|
|
360
|
+
return (await getBase()).root.getByLabel(text, { exact: true });
|
|
361
|
+
}),
|
|
362
|
+
doubleClick: async () => (await waitFor()).dblclick(),
|
|
363
|
+
text: async () => (await (await now()).textContent() ?? "").replace(/\s+/g, " ").trim(),
|
|
364
|
+
value: async () => (await now()).inputValue(),
|
|
365
|
+
checked: async () => (await now()).evaluate((el) => el.checked === true),
|
|
366
|
+
indeterminate: async () => (await now()).evaluate((el) => el.indeterminate === true),
|
|
367
|
+
exists: async () => {
|
|
368
|
+
await ensureInPage((await getBase()).root);
|
|
369
|
+
return (await locate()).evaluateAll((els) => els.some((el) => globalThis.__rindTesting?.isVisible(el) === true));
|
|
370
|
+
},
|
|
371
|
+
isFocused: async () => (await now()).evaluate((el) => {
|
|
372
|
+
const active = document.activeElement;
|
|
373
|
+
return active === el || el.id !== "" && active?.getAttribute("aria-activedescendant") === el.id;
|
|
374
|
+
}),
|
|
375
|
+
count: async () => (await locate()).count(),
|
|
376
|
+
nth: (index) => handle(`${label}[${index}]`, async () => (await locate()).nth(index)),
|
|
377
|
+
all: async () => {
|
|
378
|
+
const n = await (await locate()).count();
|
|
379
|
+
return Array.from({ length: n }, (_, i) => part.nth(i));
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
return part;
|
|
383
|
+
};
|
|
384
|
+
const partLocator = async (name, by) => {
|
|
385
|
+
const { root } = await getBase();
|
|
386
|
+
const id = await instance();
|
|
387
|
+
const parts = root.locator(`[data-scope="${scope}"][data-part="${name}"][data-instance="${id}"]`);
|
|
388
|
+
return by ? parts.and(byLocator(root, by)) : parts;
|
|
389
|
+
};
|
|
390
|
+
return {
|
|
391
|
+
modality,
|
|
392
|
+
part: (name, by) => handle(`${scope}.${name}${by ? ` (${describeBy(by)})` : ""}`, () => partLocator(name, by)),
|
|
393
|
+
find: (by) => handle(describeBy(by), async () => byLocator((await getBase()).root, by)),
|
|
394
|
+
focused: () => handle("the focused element", async () => (await getBase()).root.locator("*:focus:not(body)")),
|
|
395
|
+
async keyboard(key, keyOptions) {
|
|
396
|
+
if (keyOptions?.composing) {
|
|
397
|
+
await (await getBase()).root.locator("*:focus").evaluate(composingKeys, composingInit(key, keyOptions));
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
await (await getBase()).page.keyboard.press(keyName(key, keyOptions, apple));
|
|
401
|
+
},
|
|
402
|
+
async pointerOutside() {
|
|
403
|
+
await (await getBase()).root.locator("body").click({
|
|
404
|
+
position: {
|
|
405
|
+
x: 1,
|
|
406
|
+
y: 1
|
|
407
|
+
},
|
|
408
|
+
force: true
|
|
409
|
+
});
|
|
410
|
+
},
|
|
411
|
+
until,
|
|
412
|
+
advance: async (ms) => {
|
|
413
|
+
if (options.advanceTimers) await options.advanceTimers(ms);
|
|
414
|
+
else await (await getBase()).page.waitForTimeout(Math.max(ms, 16));
|
|
415
|
+
},
|
|
416
|
+
instance,
|
|
417
|
+
anchorCount: async () => (await anchorLocator()).count(),
|
|
418
|
+
selectedText: async () => (await getBase()).root.evaluate(() => globalThis.getSelection()?.toString() ?? ""),
|
|
419
|
+
markNavigations: async () => {
|
|
420
|
+
navigationsFrom = await navigationState();
|
|
421
|
+
},
|
|
422
|
+
navigated: async (o) => {
|
|
423
|
+
const now = await navigationState();
|
|
424
|
+
const from = navigationsFrom ?? {
|
|
425
|
+
...now,
|
|
426
|
+
log: []
|
|
427
|
+
};
|
|
428
|
+
const newTab = o?.newTab === true;
|
|
429
|
+
if (now.log.slice(from.log.length).some((t) => t === newTab)) return true;
|
|
430
|
+
return newTab ? now.pages > from.pages : now.url !== from.url;
|
|
431
|
+
},
|
|
432
|
+
collectDocument: async () => {
|
|
433
|
+
const { root } = await getBase();
|
|
434
|
+
await ensureInPage(root);
|
|
435
|
+
return root.evaluate((a) => globalThis.__rindTesting?.collectDom(a), {
|
|
436
|
+
scope,
|
|
437
|
+
instance: "\0unmounted"
|
|
438
|
+
});
|
|
439
|
+
},
|
|
440
|
+
collect: async () => {
|
|
441
|
+
const { root } = await getBase();
|
|
442
|
+
await ensureInPage(root);
|
|
443
|
+
const arg = {
|
|
444
|
+
scope,
|
|
445
|
+
instance: await instance()
|
|
446
|
+
};
|
|
447
|
+
return root.evaluate((a) => globalThis.__rindTesting?.collectDom(a), arg);
|
|
448
|
+
},
|
|
449
|
+
announcements: async () => {
|
|
450
|
+
const { root } = await getBase();
|
|
451
|
+
await poll(() => root.evaluate(() => Reflect.get(globalThis, Symbol.for("rind-ui.announcements.pending")) ?? 0), (n) => n === 0, {
|
|
452
|
+
timeout: 1e3,
|
|
453
|
+
wait: (ms) => new Promise((r) => setTimeout(r, ms))
|
|
454
|
+
}).catch(() => {});
|
|
455
|
+
return root.evaluate(() => [...Reflect.get(globalThis, Symbol.for("rind-ui.announcements")) ?? []]);
|
|
456
|
+
},
|
|
457
|
+
takeErrors: async () => watchPageErrors((await getBase()).page).splice(0),
|
|
458
|
+
chromiumChecks: async () => {
|
|
459
|
+
const { page, root } = await getBase();
|
|
460
|
+
/**
|
|
461
|
+
* The node, polled until it exists and shows no issue (the tree updates after the DOM),
|
|
462
|
+
* then read once more: null when it never existed within the timeout.
|
|
463
|
+
*/
|
|
464
|
+
const settledNode = async (expression, issues) => {
|
|
465
|
+
const read = () => axNodeOf(page, expression);
|
|
466
|
+
const last = await until(read, (n) => n !== null && ("unavailable" in n || issues(n).length === 0)).catch(read);
|
|
467
|
+
if (last === null) return null;
|
|
468
|
+
return "unavailable" in last ? "unavailable" : last;
|
|
469
|
+
};
|
|
470
|
+
if (root !== page) return "unavailable";
|
|
471
|
+
await ensureInPage(page);
|
|
472
|
+
const id = await instance();
|
|
473
|
+
const snap = await page.evaluate((a) => globalThis.__rindTesting?.collectDom(a), {
|
|
474
|
+
scope,
|
|
475
|
+
instance: id
|
|
476
|
+
});
|
|
477
|
+
const partsSelector = JSON.stringify(`[data-scope="${scope}"][data-instance="${id}"][data-part]`);
|
|
478
|
+
const problems = [];
|
|
479
|
+
const exposedNow = (el) => !el.hidden && !el.inAriaHidden && !el.inInert;
|
|
480
|
+
for (const [i, el] of snap.parts.entries()) {
|
|
481
|
+
if (!exposedNow(el)) continue;
|
|
482
|
+
const expression = `document.querySelectorAll(${partsSelector})[${i}]`;
|
|
483
|
+
const labelled = el.attrs["aria-labelledby"] !== void 0 && el.name !== "";
|
|
484
|
+
const described = el.attrs["aria-describedby"] !== void 0 && el.description !== "";
|
|
485
|
+
const popup = [
|
|
486
|
+
"dialog",
|
|
487
|
+
"alertdialog",
|
|
488
|
+
"menu",
|
|
489
|
+
"listbox",
|
|
490
|
+
"tree",
|
|
491
|
+
"grid"
|
|
492
|
+
].includes(el.role ?? "");
|
|
493
|
+
if (!labelled && !described && !popup) continue;
|
|
494
|
+
const issues = (node) => [
|
|
495
|
+
...labelled && node.name === "" ? [`${el.describe}: Chromium computes no name (aria-labelledby target inert or ignored? RA#9737); in-page name "${el.name}"`] : [],
|
|
496
|
+
...described && node.description === "" ? [`${el.describe}: Chromium drops the description (target ignored? RA#8915)`] : [],
|
|
497
|
+
...popup && node.ignored ? [`${el.describe}: open popup is ignored by Chromium (${node.ignoredReasons.join(", ")}; RX#4115)`] : []
|
|
498
|
+
];
|
|
499
|
+
const node = await settledNode(expression, issues);
|
|
500
|
+
if (node === "unavailable") return "unavailable";
|
|
501
|
+
if (node === null) {
|
|
502
|
+
problems.push(`${el.describe}: Chromium's accessibility node never became available`);
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
problems.push(...issues(node));
|
|
506
|
+
}
|
|
507
|
+
if (snap.focus.element) {
|
|
508
|
+
const focused = snap.focus.element;
|
|
509
|
+
const issues = (node) => node.ignored ? [`the focused ${focused.describe} is ignored by Chromium (${node.ignoredReasons.join(", ")})`] : [];
|
|
510
|
+
const node = await settledNode("document.activeElement", issues);
|
|
511
|
+
if (node === null) problems.push(`the focused ${focused.describe}: Chromium's node never became available`);
|
|
512
|
+
else if (node !== "unavailable") problems.push(...issues(node));
|
|
513
|
+
}
|
|
514
|
+
return problems;
|
|
515
|
+
},
|
|
516
|
+
setDirection: async (direction) => {
|
|
517
|
+
await (await getBase()).root.evaluate((dir) => {
|
|
518
|
+
if (dir === null) document.documentElement.removeAttribute("dir");
|
|
519
|
+
else document.documentElement.dir = dir;
|
|
520
|
+
}, direction);
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
//#endregion
|
|
525
|
+
export { chromiumTree as a, axNodeOf as i, playwrightEnv as n, watchPageErrors as r, isPage as t };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { r as EnvFactory } from "./adapter-UPhu17S6.mjs";
|
|
2
|
+
import { FrameLocator, Locator, Page } from "playwright";
|
|
3
|
+
//#region src/adapters/playwright.d.ts
|
|
4
|
+
type PlaywrightTarget = Page | Locator | FrameLocator;
|
|
5
|
+
interface PlaywrightAdapterOptions {
|
|
6
|
+
/** `until` timeout in ms (default 5000, Playwright's expect timeout). */
|
|
7
|
+
readonly timeout?: number;
|
|
8
|
+
/** Advances time when the page uses `page.clock`; defaults to waiting. */
|
|
9
|
+
readonly advanceTimers?: (ms: number) => Promise<unknown>;
|
|
10
|
+
}
|
|
11
|
+
/** The Playwright adapter. */
|
|
12
|
+
declare function playwrightEnv(target: PlaywrightTarget, options?: PlaywrightAdapterOptions): EnvFactory;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { PlaywrightTarget as n, playwrightEnv as r, PlaywrightAdapterOptions as t };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { n as PlaywrightTarget, r as playwrightEnv, t as PlaywrightAdapterOptions } from "./playwright-CtIxjMKr.mjs";
|
|
2
|
+
import { Page } from "playwright";
|
|
3
|
+
//#region src/adapters/chromium-tree.d.ts
|
|
4
|
+
interface AxNode {
|
|
5
|
+
readonly role: string;
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly description: string;
|
|
8
|
+
readonly ignored: boolean;
|
|
9
|
+
readonly ignoredReasons: readonly string[];
|
|
10
|
+
}
|
|
11
|
+
interface Unavailable {
|
|
12
|
+
readonly unavailable: true;
|
|
13
|
+
}
|
|
14
|
+
interface ChromiumTreeOptions {
|
|
15
|
+
/** Retry until the tree satisfies this (default: Chromium returned a tree). */
|
|
16
|
+
readonly until?: (nodes: readonly AxNode[]) => boolean;
|
|
17
|
+
/** Default 5000 ms. */
|
|
18
|
+
readonly timeout?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Chromium's full accessibility tree, retried until it satisfies `until` (the tree updates
|
|
22
|
+
* after the DOM, so a fresh change may not be in it yet); `{ unavailable: true }` in other
|
|
23
|
+
* engines. Throws when `until` still fails after `timeout` ms.
|
|
24
|
+
*/
|
|
25
|
+
export declare function chromiumTree(page: Page, { until, timeout }?: ChromiumTreeOptions): Promise<readonly AxNode[] | Unavailable>;
|
|
26
|
+
/** The accessibility node of the element a page expression evaluates to. */
|
|
27
|
+
export declare function axNodeOf(page: Page, expression: string): Promise<AxNode | Unavailable | null>;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { type AxNode, type ChromiumTreeOptions, type PlaywrightAdapterOptions, type PlaywrightTarget, type Unavailable, playwrightEnv };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { i as axNodeOf, a as chromiumTree, n as playwrightEnv } from "./playwright-CNUCI2Xe.mjs";
|
package/dist/setup.d.mts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/setup.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Where rind-ui modules that keep document-level state (layer stack, scroll lock, focus
|
|
4
|
+
* trap stack, tooltip group) register how to reset it.
|
|
5
|
+
*/
|
|
6
|
+
export declare const RESETS: unique symbol;
|
|
7
|
+
/** Clears every piece of rind-ui document-level state. Call after each test. */
|
|
8
|
+
export declare function resetRindGlobals(): void;
|
|
9
|
+
//#endregion
|
package/dist/setup.mjs
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { a as navigationLog, n as installErrorLog, o as installAnnouncementLog } from "./dom-Ds2Cv9wy.mjs";
|
|
2
|
+
//#region src/setup.ts
|
|
3
|
+
function define(target, name, value) {
|
|
4
|
+
if (!target || Reflect.get(target, name) != null) return;
|
|
5
|
+
Object.defineProperty(target, name, {
|
|
6
|
+
value,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
const proto = typeof Element === "undefined" ? void 0 : Element.prototype;
|
|
13
|
+
var NoopObserver = class {
|
|
14
|
+
observe() {}
|
|
15
|
+
unobserve() {}
|
|
16
|
+
disconnect() {}
|
|
17
|
+
takeRecords() {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
define(g, "ResizeObserver", NoopObserver);
|
|
22
|
+
define(g, "IntersectionObserver", NoopObserver);
|
|
23
|
+
const noop = () => {};
|
|
24
|
+
define(proto, "scrollIntoView", noop);
|
|
25
|
+
define(proto, "scrollTo", noop);
|
|
26
|
+
define(proto, "scrollBy", noop);
|
|
27
|
+
if (typeof window !== "undefined") define(window, "scrollTo", noop);
|
|
28
|
+
define(proto, "getAnimations", () => []);
|
|
29
|
+
if (typeof document !== "undefined") define(document, "getAnimations", () => []);
|
|
30
|
+
if (typeof MouseEvent !== "undefined" && g["PointerEvent"] == null) {
|
|
31
|
+
class PointerEventPolyfill extends MouseEvent {
|
|
32
|
+
pointerId;
|
|
33
|
+
pointerType;
|
|
34
|
+
width;
|
|
35
|
+
height;
|
|
36
|
+
pressure;
|
|
37
|
+
isPrimary;
|
|
38
|
+
constructor(type, init = {}) {
|
|
39
|
+
super(type, init);
|
|
40
|
+
this.pointerId = init.pointerId ?? 1;
|
|
41
|
+
this.pointerType = init.pointerType ?? "mouse";
|
|
42
|
+
this.width = init.width ?? 1;
|
|
43
|
+
this.height = init.height ?? 1;
|
|
44
|
+
this.pressure = init.pressure ?? 0;
|
|
45
|
+
this.isPrimary = init.isPrimary ?? true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
define(g, "PointerEvent", PointerEventPolyfill);
|
|
49
|
+
}
|
|
50
|
+
define(proto, "setPointerCapture", noop);
|
|
51
|
+
define(proto, "releasePointerCapture", noop);
|
|
52
|
+
define(proto, "hasPointerCapture", () => false);
|
|
53
|
+
if (typeof window !== "undefined") {
|
|
54
|
+
define(window, "matchMedia", (query) => ({
|
|
55
|
+
matches: false,
|
|
56
|
+
media: query,
|
|
57
|
+
onchange: null,
|
|
58
|
+
addEventListener: noop,
|
|
59
|
+
removeEventListener: noop,
|
|
60
|
+
addListener: noop,
|
|
61
|
+
removeListener: noop,
|
|
62
|
+
dispatchEvent: () => false
|
|
63
|
+
}));
|
|
64
|
+
define(window, "visualViewport", {
|
|
65
|
+
get width() {
|
|
66
|
+
return window.innerWidth;
|
|
67
|
+
},
|
|
68
|
+
get height() {
|
|
69
|
+
return window.innerHeight;
|
|
70
|
+
},
|
|
71
|
+
offsetLeft: 0,
|
|
72
|
+
offsetTop: 0,
|
|
73
|
+
pageLeft: 0,
|
|
74
|
+
pageTop: 0,
|
|
75
|
+
scale: 1,
|
|
76
|
+
onresize: null,
|
|
77
|
+
onscroll: null,
|
|
78
|
+
addEventListener: noop,
|
|
79
|
+
removeEventListener: noop,
|
|
80
|
+
dispatchEvent: () => false
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Where rind-ui modules that keep document-level state (layer stack, scroll lock, focus
|
|
85
|
+
* trap stack, tooltip group) register how to reset it.
|
|
86
|
+
*/
|
|
87
|
+
const RESETS = Symbol.for("rind-ui.resets");
|
|
88
|
+
/** Clears every piece of rind-ui document-level state. Call after each test. */
|
|
89
|
+
function resetRindGlobals() {
|
|
90
|
+
const resets = Reflect.get(globalThis, RESETS);
|
|
91
|
+
for (const reset of resets ?? []) reset();
|
|
92
|
+
installAnnouncementLog().length = 0;
|
|
93
|
+
navigationLog().length = 0;
|
|
94
|
+
}
|
|
95
|
+
installErrorLog();
|
|
96
|
+
installAnnouncementLog();
|
|
97
|
+
const afterEachHook = g["afterEach"];
|
|
98
|
+
if (typeof afterEachHook === "function") afterEachHook(resetRindGlobals);
|
|
99
|
+
//#endregion
|
|
100
|
+
export { RESETS, resetRindGlobals };
|