@harnessed-ts/resolve 0.0.0-stage → 0.7.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.
Files changed (43) hide show
  1. package/CHANGELOG.md +95 -0
  2. package/LICENSE +21 -0
  3. package/README.md +106 -2
  4. package/dist/box.d.ts +27 -0
  5. package/dist/box.d.ts.map +1 -0
  6. package/dist/chunk-BYG4HMLR.js +61 -0
  7. package/dist/chunk-BYG4HMLR.js.map +1 -0
  8. package/dist/files.d.ts +7 -0
  9. package/dist/files.d.ts.map +1 -0
  10. package/dist/global-name.d.ts +7 -0
  11. package/dist/global-name.d.ts.map +1 -0
  12. package/dist/index.cjs +315 -0
  13. package/dist/index.cjs.map +1 -0
  14. package/dist/index.d.ts +10 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +296 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/inject.cjs +68 -0
  19. package/dist/inject.cjs.map +1 -0
  20. package/dist/inject.d.ts +27 -0
  21. package/dist/inject.d.ts.map +1 -0
  22. package/dist/inject.global.d.ts +2 -0
  23. package/dist/inject.global.d.ts.map +1 -0
  24. package/dist/inject.global.js +70 -0
  25. package/dist/inject.js +3 -0
  26. package/dist/inject.js.map +1 -0
  27. package/dist/page-api.d.ts +57 -0
  28. package/dist/page-api.d.ts.map +1 -0
  29. package/dist/resolve.d.ts +38 -0
  30. package/dist/resolve.d.ts.map +1 -0
  31. package/dist/visible.d.ts +18 -0
  32. package/dist/visible.d.ts.map +1 -0
  33. package/dist/webdriver-page-script.d.ts +48 -0
  34. package/dist/webdriver-page-script.d.ts.map +1 -0
  35. package/dist/webdriver.cjs +272 -0
  36. package/dist/webdriver.cjs.map +1 -0
  37. package/dist/webdriver.d.ts +96 -0
  38. package/dist/webdriver.d.ts.map +1 -0
  39. package/dist/webdriver.js +226 -0
  40. package/dist/webdriver.js.map +1 -0
  41. package/dist/wire.d.ts +16 -0
  42. package/dist/wire.d.ts.map +1 -0
  43. package/package.json +79 -3
@@ -0,0 +1,226 @@
1
+ import { PAGE_API_GLOBAL, encodeSelector, injectSource } from './chunk-BYG4HMLR.js';
2
+ import { checkTestIds, getConfig, reviveStrictViolation } from '@harnessed-ts/core';
3
+
4
+ // src/webdriver-page-script.ts
5
+ var PageError = class extends Error {
6
+ constructor(name, message) {
7
+ super(message);
8
+ this.name = name;
9
+ }
10
+ };
11
+ function isFrameEntryError(error) {
12
+ return error instanceof PageError && error.name === "FrameEntryError";
13
+ }
14
+ async function pageScript(name, op, scope, selector, options, arg) {
15
+ const api = window[name];
16
+ if (api === void 0) return { kind: "missing" };
17
+ const one = () => api.one(null, scope, selector, options);
18
+ const iframe = async (found) => {
19
+ if (found !== null && found.localName !== "iframe") {
20
+ await api.count(null, [...scope, selector], selector, options);
21
+ }
22
+ return {
23
+ element: found,
24
+ origin: found === null ? { x: 0, y: 0 } : api.origin(found),
25
+ // Whether the iframe is visible in its own document: content in a hidden
26
+ // frame is hidden, whatever its own style says.
27
+ visible: found !== null && api.visible(found)
28
+ };
29
+ };
30
+ try {
31
+ let value;
32
+ switch (op) {
33
+ case "frame":
34
+ value = await iframe(await one());
35
+ break;
36
+ case "frameNow":
37
+ value = await iframe(api.oneNow(null, scope, selector, options));
38
+ break;
39
+ case "element":
40
+ value = { element: await one() };
41
+ break;
42
+ case "probe": {
43
+ const matches = api.allNow(null, scope, selector, options);
44
+ value = { count: matches.length, element: matches.length === 1 ? matches[0] : null };
45
+ break;
46
+ }
47
+ case "visible": {
48
+ const matches = api.allNow(null, scope, selector, options);
49
+ value = {
50
+ count: matches.length,
51
+ visible: matches.length === 1 && api.visible(matches[0])
52
+ };
53
+ break;
54
+ }
55
+ case "count":
56
+ value = await api.count(null, scope, selector, options);
57
+ break;
58
+ case "texts": {
59
+ const matches = await api.all(null, scope, selector, options);
60
+ const index = selector.nth;
61
+ const listed = index === void 0 ? matches : matches.slice(index, index + 1);
62
+ value = listed.map((element) => element.textContent ?? "");
63
+ break;
64
+ }
65
+ case "text":
66
+ value = (await one()).textContent ?? "";
67
+ break;
68
+ case "value":
69
+ value = (await one()).value ?? "";
70
+ break;
71
+ case "attribute":
72
+ value = (await one()).getAttribute(arg ?? "");
73
+ break;
74
+ case "enabled": {
75
+ const element = await one();
76
+ value = {
77
+ disabled: element.disabled === true || element.closest(":disabled") !== null,
78
+ aria: element.getAttribute("aria-disabled")
79
+ };
80
+ break;
81
+ }
82
+ case "checked": {
83
+ const element = await one();
84
+ value = {
85
+ element,
86
+ aria: element.getAttribute("aria-checked"),
87
+ native: Boolean(element.checked)
88
+ };
89
+ break;
90
+ }
91
+ case "selected": {
92
+ const element = await one();
93
+ value = [...element.selectedOptions ?? []].map((option) => option.value);
94
+ break;
95
+ }
96
+ case "select": {
97
+ const element = await one();
98
+ value = {
99
+ element,
100
+ multiple: element.multiple,
101
+ selected: [...element.selectedOptions ?? []].map((option) => option.value),
102
+ // What each option can be chosen by: its value, or the label it shows.
103
+ options: [...element.options ?? []].map((option) => ({
104
+ value: option.value,
105
+ label: option.label.trim()
106
+ }))
107
+ };
108
+ break;
109
+ }
110
+ case "clear": {
111
+ const element = await one();
112
+ const direct = ["range", "date", "datetime-local", "month", "week", "time", "color"];
113
+ element.focus();
114
+ if (typeof element.select === "function") element.select();
115
+ else window.getSelection()?.selectAllChildren(element);
116
+ value = {
117
+ element,
118
+ direct: element.localName === "input" && direct.includes(element.type),
119
+ filled: (element.value ?? element.textContent ?? "") !== ""
120
+ };
121
+ break;
122
+ }
123
+ case "box":
124
+ value = api.box(await one());
125
+ break;
126
+ case "upload": {
127
+ const element = await one();
128
+ const control = element.localName === "label" ? element.control : null;
129
+ const input = control ?? element;
130
+ value = {
131
+ element: input,
132
+ target: api.fileTarget(element),
133
+ filled: (input.files?.length ?? 0) > 0
134
+ };
135
+ break;
136
+ }
137
+ case "focus":
138
+ ;
139
+ (await one()).focus();
140
+ value = null;
141
+ break;
142
+ case "blur":
143
+ ;
144
+ (await one()).blur();
145
+ value = null;
146
+ break;
147
+ }
148
+ return { kind: "ok", value, url: location.href };
149
+ } catch (error) {
150
+ const { name: name2 = "Error", message = String(error) } = error ?? {};
151
+ return { kind: "error", name: name2, message };
152
+ }
153
+ }
154
+
155
+ // src/webdriver.ts
156
+ var classic;
157
+ function classicScript() {
158
+ if (classic === void 0) classic = `return (${pageScript.toString()}).apply(null, arguments)`;
159
+ return classic;
160
+ }
161
+ async function callPage(driver, site, op, scope, selector, timeout, arg = null) {
162
+ checkTestIds(scope, selector);
163
+ const args = [
164
+ PAGE_API_GLOBAL,
165
+ op,
166
+ scope.map(encodeSelector),
167
+ encodeSelector(selector),
168
+ // The page cannot see this process's configure(), so the settings travel.
169
+ { testIdAttribute: getConfig().testIdAttribute, timeout },
170
+ arg
171
+ ];
172
+ const ask = async () => await driver.run(site, args);
173
+ let reply = await ask();
174
+ if (reply.kind === "missing") {
175
+ await driver.inject(site, injectSource());
176
+ reply = await ask();
177
+ }
178
+ if (reply.kind === "missing") {
179
+ throw new Error("harnessed: the resolver could not be injected into the page.");
180
+ }
181
+ if (reply.kind === "error") throw reviveStrictViolation(new PageError(reply.name, reply.message));
182
+ if (site.top) driver.sawUrl?.(reply.url);
183
+ return reply.value;
184
+ }
185
+ function splitAtFrames(scope) {
186
+ const hops = [];
187
+ let current = [];
188
+ for (const link of scope) {
189
+ if (link.frame === true) {
190
+ hops.push({ scope: current, link });
191
+ current = [];
192
+ } else {
193
+ current.push(link);
194
+ }
195
+ }
196
+ return { hops, rest: current };
197
+ }
198
+ var FrameAbsent = class extends Error {
199
+ name = "FrameAbsent";
200
+ };
201
+ async function walk(driver, scope, options, operation) {
202
+ const deadline = Date.now() + options.timeout;
203
+ const remaining = () => Math.max(0, deadline - Date.now());
204
+ const { hops, rest } = splitAtFrames(scope);
205
+ let site = driver.top();
206
+ let entered = false;
207
+ try {
208
+ for (const hop of hops) {
209
+ const op = options.waiting ? "frame" : "frameNow";
210
+ const found = await callPage(driver, site, op, hop.scope, hop.link, remaining());
211
+ if (found.element === null) throw new FrameAbsent();
212
+ const iframe = await driver.element(site, found.element);
213
+ await options.onFrame?.(iframe, { visible: found.visible });
214
+ const origin = { x: site.origin.x + found.origin.x, y: site.origin.y + found.origin.y };
215
+ site = await driver.enterFrame(site, iframe, origin);
216
+ entered = true;
217
+ }
218
+ return await operation(site, rest, remaining);
219
+ } finally {
220
+ if (entered) await driver.leaveFrames();
221
+ }
222
+ }
223
+
224
+ export { FrameAbsent, PageError, callPage, classicScript, isFrameEntryError, pageScript, walk };
225
+ //# sourceMappingURL=webdriver.js.map
226
+ //# sourceMappingURL=webdriver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/webdriver-page-script.ts","../src/webdriver.ts"],"names":["name"],"mappings":";;;;AA0CO,IAAM,SAAA,GAAN,cAAwB,KAAA,CAAM;AAAA,EACnC,WAAA,CAAY,MAAc,OAAA,EAAiB;AACzC,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AAAA,EACd;AACF;AAGO,SAAS,kBAAkB,KAAA,EAAyB;AACzD,EAAA,OAAO,KAAA,YAAiB,SAAA,IAAa,KAAA,CAAM,IAAA,KAAS,iBAAA;AACtD;AAeA,eAAsB,WACpB,IAAA,EACA,EAAA,EACA,KAAA,EACA,QAAA,EACA,SACA,GAAA,EACoB;AACpB,EAAA,MAAM,GAAA,GAAO,OAA0D,IAAI,CAAA;AAE3E,EAAA,IAAI,GAAA,KAAQ,MAAA,EAAW,OAAO,EAAE,MAAM,SAAA,EAAU;AAChD,EAAA,MAAM,MAAM,MAAM,GAAA,CAAI,IAAI,IAAA,EAAM,KAAA,EAAO,UAAU,OAAO,CAAA;AAMxD,EAAA,MAAM,MAAA,GAAS,OAAO,KAAA,KAA0B;AAC9C,IAAA,IAAI,KAAA,KAAU,IAAA,IAAQ,KAAA,CAAM,SAAA,KAAc,QAAA,EAAU;AAClD,MAAA,MAAM,GAAA,CAAI,MAAM,IAAA,EAAM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F","file":"webdriver.js","sourcesContent":["import type { PageApi, PageApiOptions } from './page-api'\nimport type { WireSelector } from './wire'\n\n/**\n * What the in-page script does with the target it resolves. Reads happen in the\n * page, in the same round-trip as the resolution: a WebDriver command costs a\n * few milliseconds each way, and a read that first fetched an element reference\n * and then asked about it would pay that twice.\n */\nexport type PageOp =\n | 'frame'\n | 'frameNow'\n | 'element'\n | 'probe'\n | 'visible'\n | 'count'\n | 'texts'\n | 'text'\n | 'value'\n | 'attribute'\n | 'enabled'\n | 'checked'\n | 'selected'\n | 'select'\n | 'clear'\n | 'focus'\n | 'blur'\n | 'box'\n | 'upload'\n\n/**\n * The script's answer. A page error comes back as a value rather than a thrown\n * script error, so its name and message arrive intact — a thrown one reaches the\n * test wrapped in protocol noise, and under WebDriver Classic is logged as a\n * driver failure as well.\n */\nexport type PageReply =\n | { kind: 'missing' }\n | { kind: 'ok'; value: unknown; url: string }\n | { kind: 'error'; name: string; message: string }\n\n/** Raised in the page by the shared resolver: strictness, index and frame rules. */\nexport class PageError extends Error {\n constructor(name: string, message: string) {\n super(message)\n this.name = name\n }\n}\n\n/** The shared resolver's refusal of a frame it cannot enter. Never \"not there yet\". */\nexport function isFrameEntryError(error: unknown): boolean {\n return error instanceof PageError && error.name === 'FrameEntryError'\n}\n\n/**\n * An element as WebDriver returns it from a script, before the driver's client\n * wraps it (WebdriverIO's `$()`; selenium-webdriver hands back a `WebElement`\n * already).\n */\nexport type ElementRef = { 'element-6066-11e4-a52e-4f735466cecf': string }\n\n/**\n * Runs IN THE PAGE, serialised with `Function.prototype.toString`, so it must\n * close over nothing: every input arrives as an argument. Elements are\n * only ever returned as object properties, each at most once — WebDriver BiDi\n * serialises an element repeated in a result, or one inside an array, as `{}`.\n */\nexport async function pageScript(\n name: string,\n op: PageOp,\n scope: WireSelector[],\n selector: WireSelector,\n options: PageApiOptions,\n arg: string | null,\n): Promise<PageReply> {\n const api = (window as unknown as Record<string, PageApi | undefined>)[name]\n // Navigation replaces the window, and the resolver with it.\n if (api === undefined) return { kind: 'missing' }\n const one = () => api.one(null, scope, selector, options)\n /**\n * A frame link must land on an iframe. When it does not, walking through the\n * same link with the shared resolver raises its FrameEntryError, so the\n * refusal and its wording stay the resolver's rather than a copy kept here.\n */\n const iframe = async (found: Element | null) => {\n if (found !== null && found.localName !== 'iframe') {\n await api.count(null, [...scope, selector], selector, options)\n }\n return {\n element: found,\n origin: found === null ? { x: 0, y: 0 } : api.origin(found),\n // Whether the iframe is visible in its own document: content in a hidden\n // frame is hidden, whatever its own style says.\n visible: found !== null && api.visible(found),\n }\n }\n try {\n let value: unknown\n switch (op) {\n case 'frame':\n value = await iframe(await one())\n break\n case 'frameNow':\n value = await iframe(api.oneNow(null, scope, selector, options))\n break\n case 'element':\n value = { element: await one() }\n break\n case 'probe': {\n // `allNow` applies `nth` and answers at once; the caller decides what\n // more than one match means, with the whole scope chain in hand.\n const matches = api.allNow(null, scope, selector, options)\n value = { count: matches.length, element: matches.length === 1 ? matches[0] : null }\n break\n }\n case 'visible': {\n // `probe`'s answer, with the layout check made in the same round-trip.\n const matches = api.allNow(null, scope, selector, options)\n value = {\n count: matches.length,\n visible: matches.length === 1 && api.visible(matches[0] as Element),\n }\n break\n }\n case 'count':\n value = await api.count(null, scope, selector, options)\n break\n case 'texts': {\n const matches = await api.all(null, scope, selector, options)\n const index = (selector as { nth?: number }).nth\n const listed = index === undefined ? matches : matches.slice(index, index + 1)\n value = listed.map(element => element.textContent ?? '')\n break\n }\n case 'text':\n value = (await one()).textContent ?? ''\n break\n case 'value':\n value = ((await one()) as HTMLInputElement).value ?? ''\n break\n case 'attribute':\n value = (await one()).getAttribute(arg ?? '')\n break\n case 'enabled': {\n const element = (await one()) as HTMLInputElement\n // The property, and the closest `:disabled`, both see an ancestor\n // fieldset's `disabled`; the element's own attribute does not.\n value = {\n disabled: element.disabled === true || element.closest(':disabled') !== null,\n aria: element.getAttribute('aria-disabled'),\n }\n break\n }\n case 'checked': {\n const element = (await one()) as HTMLInputElement\n value = {\n element,\n aria: element.getAttribute('aria-checked'),\n native: Boolean(element.checked),\n }\n break\n }\n case 'selected': {\n const element = (await one()) as HTMLSelectElement\n value = [...(element.selectedOptions ?? [])].map(option => option.value)\n break\n }\n case 'select': {\n const element = (await one()) as HTMLSelectElement\n value = {\n element,\n multiple: element.multiple,\n selected: [...(element.selectedOptions ?? [])].map(option => option.value),\n // What each option can be chosen by: its value, or the label it shows.\n options: [...(element.options ?? [])].map(option => ({\n value: option.value,\n label: option.label.trim(),\n })),\n }\n break\n }\n case 'clear': {\n const element = (await one()) as HTMLInputElement\n // WebDriver sets these input types' values directly: they take no typing.\n const direct = ['range', 'date', 'datetime-local', 'month', 'week', 'time', 'color']\n element.focus()\n // Selected, so one Backspace deletes it all: a real edit, which fires the\n // input events a controlled component listens for.\n if (typeof element.select === 'function') element.select()\n else window.getSelection()?.selectAllChildren(element)\n value = {\n element,\n direct: element.localName === 'input' && direct.includes(element.type),\n filled: (element.value ?? element.textContent ?? '') !== '',\n }\n break\n }\n case 'box':\n // In this document's viewport; the driver adds the frames' origins.\n value = api.box(await one())\n break\n case 'upload': {\n const element = await one()\n const control = element.localName === 'label' ? (element as HTMLLabelElement).control : null\n // The input itself, where a label is the target: WebDriver types paths into the input.\n const input = (control ?? element) as HTMLInputElement\n value = {\n element: input,\n target: api.fileTarget(element),\n filled: (input.files?.length ?? 0) > 0,\n }\n break\n }\n case 'focus':\n ;((await one()) as HTMLElement).focus()\n value = null\n break\n case 'blur':\n ;((await one()) as HTMLElement).blur()\n value = null\n break\n }\n return { kind: 'ok', value, url: location.href }\n } catch (error) {\n const { name = 'Error', message = String(error) } = (error ?? {}) as Partial<Error>\n return { kind: 'error', name, message }\n }\n}\n","import { checkTestIds, getConfig, reviveStrictViolation } from '@harnessed-ts/core'\nimport type { Selector } from '@harnessed-ts/core'\nimport type { Point } from './box'\nimport { injectSource } from './inject'\nimport { PAGE_API_GLOBAL } from './global-name'\nimport { encodeSelector } from './wire'\nimport { PageError, pageScript } from './webdriver-page-script'\nimport type { ElementRef, PageOp, PageReply } from './webdriver-page-script'\n\nexport { isFrameEntryError, PageError, pageScript } from './webdriver-page-script'\nexport type { ElementRef, PageOp, PageReply } from './webdriver-page-script'\nexport type { Point } from './box'\n\n/** What `pageScript` takes, in order: what every `WebDriverLike.run` passes it. */\nexport type PageScriptArgs = Parameters<typeof pageScript>\n\n/**\n * A document a WebDriver client is working in: the top-level one, or a frame's\n * a walk entered. A driver extends it with whatever it needs to address that\n * document (a BiDi browsing context, say).\n */\nexport interface WebDriverSite {\n /** The top-level document, whose URL is the page's URL. */\n readonly top: boolean\n /** Where this document's viewport starts in the top-level one, in CSS pixels. */\n readonly origin: Point\n}\n\n/**\n * What the shared WebDriver code needs from a client — WebdriverIO,\n * selenium-webdriver under Nightwatch — and nothing more: running a script in a\n * document, and crossing into a frame. `S` is the client's site, `E` its element.\n */\nexport interface WebDriverLike<S extends WebDriverSite, E> {\n /** The top-level document. */\n top(): S\n /**\n * Runs `pageScript` in the site's document with these arguments and returns\n * its reply. Under WebDriver Classic, `classicScript()` is the body to send.\n */\n run(site: S, args: PageScriptArgs): Promise<unknown>\n /** Evaluates the resolver's injectable source in the site's document. */\n inject(site: S, source: string): Promise<void>\n /** An element the page script returned, as the client's own element. */\n element(site: S, ref: ElementRef): Promise<E>\n /**\n * Enters `iframe`, an element of `site`'s document, and returns the site for\n * the frame's document, at `origin` in the top-level viewport.\n */\n enterFrame(site: S, iframe: E, origin: Point): Promise<S>\n /**\n * Undoes what entering frames changed in the session — under Classic, switches\n * back to the top-level document. Called once after a walk that entered any.\n */\n leaveFrames(): Promise<void>\n /** Hears the top-level document's URL, which every call there brings back. */\n sawUrl?(url: string): void\n}\n\nlet classic: string | undefined\n\n/**\n * The page script as a classic Execute Script body, which awaits the promise\n * the script returns.\n */\nexport function classicScript(): string {\n if (classic === undefined) classic = `return (${pageScript.toString()}).apply(null, arguments)`\n return classic\n}\n\n/**\n * Runs one operation of the shared resolver in a document, injecting the\n * resolver first if the document does not have it yet.\n *\n * The presence check rides along with the call itself, so a document that\n * already has the resolver pays one round-trip, not two. Injecting happens once\n * per document: navigation replaces the window, and the resolver with it.\n */\nexport async function callPage<S extends WebDriverSite, E>(\n driver: WebDriverLike<S, E>,\n site: S,\n op: PageOp,\n scope: readonly Selector[],\n selector: Selector,\n timeout: number,\n arg: string | null = null,\n): Promise<unknown> {\n checkTestIds(scope, selector)\n const args: PageScriptArgs = [\n PAGE_API_GLOBAL,\n op,\n scope.map(encodeSelector),\n encodeSelector(selector),\n // The page cannot see this process's configure(), so the settings travel.\n { testIdAttribute: getConfig().testIdAttribute, timeout },\n arg,\n ]\n const ask = async () => (await driver.run(site, args)) as PageReply\n let reply = await ask()\n if (reply.kind === 'missing') {\n await driver.inject(site, injectSource())\n reply = await ask()\n }\n if (reply.kind === 'missing') {\n throw new Error('harnessed: the resolver could not be injected into the page.')\n }\n // A strict violation comes back as core's own class, as under every driver.\n if (reply.kind === 'error') throw reviveStrictViolation(new PageError(reply.name, reply.message))\n if (site.top) driver.sawUrl?.(reply.url)\n return reply.value\n}\n\n/** A scope chain cut at a frame link: the links before it, and the link itself. */\ninterface Hop {\n scope: Selector[]\n link: Selector\n}\n\nfunction splitAtFrames(scope: readonly Selector[]): { hops: Hop[]; rest: Selector[] } {\n const hops: Hop[] = []\n let current: Selector[] = []\n for (const link of scope) {\n if (link.frame === true) {\n hops.push({ scope: current, link })\n current = []\n } else {\n current.push(link)\n }\n }\n return { hops, rest: current }\n}\n\n/** A non-waiting walk found a frame link that is not on screen. */\nexport class FrameAbsent extends Error {\n override name = 'FrameAbsent'\n}\n\n/** What the page reports about an iframe a walk is about to enter. */\nexport interface FrameInfo {\n /** Whether the iframe is visible by layout in its own document. */\n readonly visible: boolean\n}\n\nexport interface WalkOptions<E> {\n /** Wait for each frame link, or answer at once by throwing FrameAbsent. */\n waiting: boolean\n /** The budget for every hop and the operation together. */\n timeout: number\n /** Called with each iframe element, from its own document, before entering it. */\n onFrame?: (iframe: E, info: FrameInfo) => Promise<void>\n}\n\n/**\n * Walks the scope chain's frame links, entering each frame, then runs the\n * operation in the innermost document with the links that remain.\n *\n * A WebDriver element belongs to one document: an element inside a frame cannot\n * be acted on from the top-level one, even where the page's own script could\n * reach it. So the shared resolver runs once per document — up to the iframe,\n * then inside it — and WebDriver does the crossing. That is also why\n * cross-origin frames work here, where an in-page resolver cannot enter them.\n *\n * Whatever entering changed in the session is undone afterwards, whatever\n * happened (`leaveFrames`).\n */\nexport async function walk<S extends WebDriverSite, E, T>(\n driver: WebDriverLike<S, E>,\n scope: readonly Selector[],\n options: WalkOptions<E>,\n operation: (site: S, scope: Selector[], remaining: () => number) => Promise<T>,\n): Promise<T> {\n const deadline = Date.now() + options.timeout\n const remaining = () => Math.max(0, deadline - Date.now())\n const { hops, rest } = splitAtFrames(scope)\n let site = driver.top()\n let entered = false\n try {\n for (const hop of hops) {\n const op = options.waiting ? 'frame' : 'frameNow'\n const found = (await callPage(driver, site, op, hop.scope, hop.link, remaining())) as {\n element: ElementRef | null\n origin: Point\n visible: boolean\n }\n if (found.element === null) throw new FrameAbsent()\n const iframe = await driver.element(site, found.element)\n await options.onFrame?.(iframe, { visible: found.visible })\n // The frame's document starts where its content box does, in the document around it.\n const origin = { x: site.origin.x + found.origin.x, y: site.origin.y + found.origin.y }\n site = await driver.enterFrame(site, iframe, origin)\n entered = true\n }\n return await operation(site, rest, remaining)\n } finally {\n if (entered) await driver.leaveFrames()\n }\n}\n"]}
package/dist/wire.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type { Selector } from '@harnessed-ts/core';
2
+ /**
3
+ * A `Selector` as it crosses into a remote page. Everything in one is JSON-safe
4
+ * except a RegExp — `role('button', { name: /save/i })` — which WebDriver,
5
+ * CDP and TestCafe all serialise to `{}`. This encodes them as tagged objects
6
+ * on the way out, and the page API decodes them on the way in.
7
+ */
8
+ export interface WireRegExp {
9
+ $regexp: string;
10
+ flags: string;
11
+ }
12
+ export type WireSelector = Record<string, unknown>;
13
+ export declare function encodeSelector(selector: Selector): WireSelector;
14
+ /** Idempotent: a selector that never left the process decodes to itself. */
15
+ export declare function decodeSelector(selector: Selector | WireSelector): Selector;
16
+ //# sourceMappingURL=wire.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wire.d.ts","sourceRoot":"","sources":["../src/wire.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAElD;;;;;GAKG;AACH,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;CACd;AAED,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AA2ClD,wBAAgB,cAAc,CAAC,QAAQ,EAAE,QAAQ,GAAG,YAAY,CAE/D;AAED,4EAA4E;AAC5E,wBAAgB,cAAc,CAAC,QAAQ,EAAE,QAAQ,GAAG,YAAY,GAAG,QAAQ,CAE1E"}
package/package.json CHANGED
@@ -1,6 +1,82 @@
1
1
  {
2
2
  "name": "@harnessed-ts/resolve",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.7.0",
4
+ "description": "The shared selector resolver behind the Testing Library-family drivers, plus an injectable build for drivers that resolve inside a remote page.",
5
+ "keywords": [
6
+ "testing",
7
+ "harness",
8
+ "testing-library",
9
+ "resolver"
10
+ ],
11
+ "license": "MIT",
12
+ "contributors": [
13
+ {
14
+ "name": "Joe Gaudet",
15
+ "url": "https://github.com/joegaudet"
16
+ },
17
+ {
18
+ "name": "Jay Seo",
19
+ "url": "https://github.com/jayseo5953"
20
+ }
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/joegaudet/harnessed.git",
25
+ "directory": "packages/resolve"
26
+ },
27
+ "homepage": "https://github.com/joegaudet/harnessed#readme",
28
+ "bugs": "https://github.com/joegaudet/harnessed/issues",
29
+ "type": "module",
30
+ "main": "./dist/index.cjs",
31
+ "module": "./dist/index.js",
32
+ "types": "./dist/index.d.ts",
33
+ "exports": {
34
+ ".": {
35
+ "types": "./dist/index.d.ts",
36
+ "import": "./dist/index.js",
37
+ "require": "./dist/index.cjs"
38
+ },
39
+ "./inject": {
40
+ "types": "./dist/inject.d.ts",
41
+ "import": "./dist/inject.js",
42
+ "require": "./dist/inject.cjs"
43
+ },
44
+ "./webdriver": {
45
+ "types": "./dist/webdriver.d.ts",
46
+ "import": "./dist/webdriver.js",
47
+ "require": "./dist/webdriver.cjs"
48
+ },
49
+ "./inject.global.js": "./dist/inject.global.js",
50
+ "./package.json": "./package.json"
51
+ },
52
+ "files": [
53
+ "dist",
54
+ "CHANGELOG.md",
55
+ "README.md",
56
+ "LICENSE"
57
+ ],
58
+ "engines": {
59
+ "node": ">=22.12.0"
60
+ },
61
+ "dependencies": {
62
+ "@harnessed-ts/core": "^0.7.0"
63
+ },
64
+ "peerDependencies": {
65
+ "@testing-library/dom": ">=10"
66
+ },
67
+ "publishConfig": {
68
+ "access": "public"
69
+ },
70
+ "devDependencies": {
71
+ "@testing-library/dom": "^10.4.2",
72
+ "@types/jsdom": "^30.0.0",
73
+ "@types/node": "^26.6.5",
74
+ "jsdom": "^30.1.1",
75
+ "vitest": "^5.0.3"
76
+ },
77
+ "scripts": {
78
+ "build": "tsup",
79
+ "clean": "rm -rf dist *.tsbuildinfo",
80
+ "test": "vitest run"
81
+ }
6
82
  }