@harnessed-ts/webdriverio 0.0.0-stage → 0.6.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/CHANGELOG.md +72 -0
- package/LICENSE +21 -0
- package/README.md +23 -2
- package/dist/driver-id.d.ts +3 -0
- package/dist/driver-id.d.ts.map +1 -0
- package/dist/env.d.ts +12 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/index.cjs +615 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +611 -0
- package/dist/index.js.map +1 -0
- package/dist/keys.d.ts +13 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/location.d.ts +3 -0
- package/dist/location.d.ts.map +1 -0
- package/dist/matchers.cjs +27 -0
- package/dist/matchers.cjs.map +1 -0
- package/dist/matchers.d.ts +27 -0
- package/dist/matchers.d.ts.map +1 -0
- package/dist/matchers.js +25 -0
- package/dist/matchers.js.map +1 -0
- package/dist/page-script.d.ts +43 -0
- package/dist/page-script.d.ts.map +1 -0
- package/dist/realm.d.ts +64 -0
- package/dist/realm.d.ts.map +1 -0
- package/dist/webdriverio-query.d.ts +70 -0
- package/dist/webdriverio-query.d.ts.map +1 -0
- package/package.json +79 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
import { registerDriver, registerNavigation, Query, timeoutFor, checkedFrom, checkFileTarget, strictViolation, enabledFrom, describeScope, checkTestIds, getConfig, reviveStrictViolation, StrictModeViolation } from '@harnessed-ts/core';
|
|
2
|
+
import { filesOnDisk, PAGE_API_GLOBAL, encodeSelector, injectSource } from '@harnessed-ts/resolve/inject';
|
|
3
|
+
import { Key } from 'webdriverio';
|
|
4
|
+
|
|
5
|
+
// src/driver-id.ts
|
|
6
|
+
var WEBDRIVERIO_DRIVER = "webdriverio";
|
|
7
|
+
var PLAYWRIGHT_ONLY = {
|
|
8
|
+
ControlOrMeta: Key.Ctrl,
|
|
9
|
+
Meta: Key.Command,
|
|
10
|
+
Backquote: "`",
|
|
11
|
+
Minus: "-",
|
|
12
|
+
Equal: "=",
|
|
13
|
+
Backslash: "\\",
|
|
14
|
+
BracketLeft: "[",
|
|
15
|
+
BracketRight: "]",
|
|
16
|
+
Quote: "'",
|
|
17
|
+
Comma: ",",
|
|
18
|
+
Period: ".",
|
|
19
|
+
Slash: "/",
|
|
20
|
+
ShiftLeft: Key.Shift,
|
|
21
|
+
ShiftRight: Key.Shift,
|
|
22
|
+
ControlLeft: Key.Control,
|
|
23
|
+
ControlRight: Key.Control,
|
|
24
|
+
AltLeft: Key.Alt,
|
|
25
|
+
AltRight: Key.Alt,
|
|
26
|
+
MetaLeft: Key.Command,
|
|
27
|
+
MetaRight: Key.Command,
|
|
28
|
+
NumpadAdd: Key.Add,
|
|
29
|
+
NumpadSubtract: Key.Subtract,
|
|
30
|
+
NumpadMultiply: Key.Multiply,
|
|
31
|
+
NumpadDivide: Key.Divide,
|
|
32
|
+
NumpadDecimal: Key.Decimal,
|
|
33
|
+
NumpadEnter: Key.Enter
|
|
34
|
+
};
|
|
35
|
+
var WEBDRIVERIO_KEYS = Key;
|
|
36
|
+
function toKeySequence(key) {
|
|
37
|
+
const parts = key.length > 1 ? key.split(/\+(?!$)/) : [key];
|
|
38
|
+
return parts.map((part) => {
|
|
39
|
+
if ([...part].length === 1) return part;
|
|
40
|
+
const mapped = PLAYWRIGHT_ONLY[part];
|
|
41
|
+
if (mapped !== void 0) return mapped;
|
|
42
|
+
const code = /^(?:Key([A-Z])|Digit(\d))$/.exec(part);
|
|
43
|
+
if (code !== null) return (code[1] ?? code[2]).toLowerCase();
|
|
44
|
+
if (Object.hasOwn(WEBDRIVERIO_KEYS, part)) return WEBDRIVERIO_KEYS[part];
|
|
45
|
+
throw new Error(
|
|
46
|
+
`harnessed: press() does not know the key "${part}"${parts.length > 1 ? ` in "${key}"` : ""}. Use a Playwright key name \u2014 Enter, ArrowLeft, Control+a, KeyA, Digit1 \u2014 or a single character.`
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/location.ts
|
|
52
|
+
var seen = /* @__PURE__ */ new WeakMap();
|
|
53
|
+
function rememberUrl(browser, url) {
|
|
54
|
+
seen.set(browser, url);
|
|
55
|
+
}
|
|
56
|
+
function lastUrl(browser) {
|
|
57
|
+
const url = seen.get(browser);
|
|
58
|
+
if (url === void 0) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
"harnessed: the webdriverio driver has not seen a URL yet. Navigate with goto(), or wait with assertPathname(), before reading currentUrl."
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return url;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/page-script.ts
|
|
67
|
+
var PageError = class extends Error {
|
|
68
|
+
constructor(name, message) {
|
|
69
|
+
super(message);
|
|
70
|
+
this.name = name;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
function isFrameEntryError(error) {
|
|
74
|
+
return error instanceof PageError && error.name === "FrameEntryError";
|
|
75
|
+
}
|
|
76
|
+
async function pageScript(name, op, scope, selector, options, arg) {
|
|
77
|
+
const api = window[name];
|
|
78
|
+
if (api === void 0) return { kind: "missing" };
|
|
79
|
+
const one = () => api.one(null, scope, selector, options);
|
|
80
|
+
const iframe = async (found) => {
|
|
81
|
+
if (found !== null && found.localName !== "iframe") {
|
|
82
|
+
await api.count(null, [...scope, selector], selector, options);
|
|
83
|
+
}
|
|
84
|
+
return { element: found, origin: found === null ? { x: 0, y: 0 } : api.origin(found) };
|
|
85
|
+
};
|
|
86
|
+
try {
|
|
87
|
+
let value;
|
|
88
|
+
switch (op) {
|
|
89
|
+
case "frame":
|
|
90
|
+
value = await iframe(await one());
|
|
91
|
+
break;
|
|
92
|
+
case "frameNow":
|
|
93
|
+
value = await iframe(api.oneNow(null, scope, selector, options));
|
|
94
|
+
break;
|
|
95
|
+
case "element":
|
|
96
|
+
value = { element: await one() };
|
|
97
|
+
break;
|
|
98
|
+
case "probe": {
|
|
99
|
+
const matches = api.allNow(null, scope, selector, options);
|
|
100
|
+
value = { count: matches.length, element: matches.length === 1 ? matches[0] : null };
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
case "count":
|
|
104
|
+
value = await api.count(null, scope, selector, options);
|
|
105
|
+
break;
|
|
106
|
+
case "texts": {
|
|
107
|
+
const matches = await api.all(null, scope, selector, options);
|
|
108
|
+
const index = selector.nth;
|
|
109
|
+
const listed = index === void 0 ? matches : matches.slice(index, index + 1);
|
|
110
|
+
value = listed.map((element) => element.textContent ?? "");
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case "text":
|
|
114
|
+
value = (await one()).textContent ?? "";
|
|
115
|
+
break;
|
|
116
|
+
case "value":
|
|
117
|
+
value = (await one()).value ?? "";
|
|
118
|
+
break;
|
|
119
|
+
case "attribute":
|
|
120
|
+
value = (await one()).getAttribute(arg ?? "");
|
|
121
|
+
break;
|
|
122
|
+
case "enabled": {
|
|
123
|
+
const element = await one();
|
|
124
|
+
value = {
|
|
125
|
+
disabled: element.disabled === true || element.closest(":disabled") !== null,
|
|
126
|
+
aria: element.getAttribute("aria-disabled")
|
|
127
|
+
};
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
case "checked": {
|
|
131
|
+
const element = await one();
|
|
132
|
+
value = {
|
|
133
|
+
element,
|
|
134
|
+
aria: element.getAttribute("aria-checked"),
|
|
135
|
+
native: Boolean(element.checked)
|
|
136
|
+
};
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
case "selected": {
|
|
140
|
+
const element = await one();
|
|
141
|
+
value = [...element.selectedOptions ?? []].map((option) => option.value);
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
case "select": {
|
|
145
|
+
const element = await one();
|
|
146
|
+
value = {
|
|
147
|
+
element,
|
|
148
|
+
multiple: element.multiple,
|
|
149
|
+
selected: [...element.selectedOptions ?? []].map((option) => option.value),
|
|
150
|
+
// What each option can be chosen by: its value, or the label it shows.
|
|
151
|
+
options: [...element.options ?? []].map((option) => ({
|
|
152
|
+
value: option.value,
|
|
153
|
+
label: option.label.trim()
|
|
154
|
+
}))
|
|
155
|
+
};
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case "clear": {
|
|
159
|
+
const element = await one();
|
|
160
|
+
const direct = ["range", "date", "datetime-local", "month", "week", "time", "color"];
|
|
161
|
+
element.focus();
|
|
162
|
+
if (typeof element.select === "function") element.select();
|
|
163
|
+
else window.getSelection()?.selectAllChildren(element);
|
|
164
|
+
value = {
|
|
165
|
+
element,
|
|
166
|
+
direct: element.localName === "input" && direct.includes(element.type),
|
|
167
|
+
filled: (element.value ?? element.textContent ?? "") !== ""
|
|
168
|
+
};
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
case "box":
|
|
172
|
+
value = api.box(await one());
|
|
173
|
+
break;
|
|
174
|
+
case "upload": {
|
|
175
|
+
const element = await one();
|
|
176
|
+
const control = element.localName === "label" ? element.control : null;
|
|
177
|
+
const input = control ?? element;
|
|
178
|
+
value = {
|
|
179
|
+
element: input,
|
|
180
|
+
target: api.fileTarget(element),
|
|
181
|
+
filled: (input.files?.length ?? 0) > 0
|
|
182
|
+
};
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case "focus":
|
|
186
|
+
;
|
|
187
|
+
(await one()).focus();
|
|
188
|
+
value = null;
|
|
189
|
+
break;
|
|
190
|
+
case "blur":
|
|
191
|
+
;
|
|
192
|
+
(await one()).blur();
|
|
193
|
+
value = null;
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
return { kind: "ok", value, url: location.href };
|
|
197
|
+
} catch (error) {
|
|
198
|
+
const { name: name2 = "Error", message = String(error) } = error ?? {};
|
|
199
|
+
return { kind: "error", name: name2, message };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function sendKeys(site, keys) {
|
|
203
|
+
return site.context === void 0 ? site.browser.keys(keys) : site.context.keys(keys);
|
|
204
|
+
}
|
|
205
|
+
var classic;
|
|
206
|
+
function classicScript() {
|
|
207
|
+
if (classic === void 0) classic = `return (${pageScript.toString()}).apply(null, arguments)`;
|
|
208
|
+
return classic;
|
|
209
|
+
}
|
|
210
|
+
var preloaded = /* @__PURE__ */ new WeakSet();
|
|
211
|
+
async function inject(site) {
|
|
212
|
+
const { browser } = site;
|
|
213
|
+
const source = injectSource();
|
|
214
|
+
if (browser.isBidi && !preloaded.has(browser)) {
|
|
215
|
+
preloaded.add(browser);
|
|
216
|
+
browser.scriptAddPreloadScript({ functionDeclaration: `() => {
|
|
217
|
+
${source}
|
|
218
|
+
}` }).catch(() => void 0);
|
|
219
|
+
}
|
|
220
|
+
if (site.context === void 0) await browser.executeScript(source, []);
|
|
221
|
+
else await site.context.execute(source);
|
|
222
|
+
}
|
|
223
|
+
async function callPage(site, op, scope, selector, timeout, arg = null) {
|
|
224
|
+
checkTestIds(scope, selector);
|
|
225
|
+
const args = [
|
|
226
|
+
PAGE_API_GLOBAL,
|
|
227
|
+
op,
|
|
228
|
+
scope.map(encodeSelector),
|
|
229
|
+
encodeSelector(selector),
|
|
230
|
+
// The page cannot see this process's configure(), so the settings travel.
|
|
231
|
+
{ testIdAttribute: getConfig().testIdAttribute, timeout },
|
|
232
|
+
arg
|
|
233
|
+
];
|
|
234
|
+
const ask = async () => site.context === void 0 ? await site.browser.executeScript(classicScript(), [...args]) : await site.context.execute(pageScript, ...args);
|
|
235
|
+
let reply = await ask();
|
|
236
|
+
if (reply.kind === "missing") {
|
|
237
|
+
await inject(site);
|
|
238
|
+
reply = await ask();
|
|
239
|
+
}
|
|
240
|
+
if (reply.kind === "missing") {
|
|
241
|
+
throw new Error("harnessed: the resolver could not be injected into the page.");
|
|
242
|
+
}
|
|
243
|
+
if (reply.kind === "error") throw reviveStrictViolation(new PageError(reply.name, reply.message));
|
|
244
|
+
if (site.top) rememberUrl(site.browser, reply.url);
|
|
245
|
+
return reply.value;
|
|
246
|
+
}
|
|
247
|
+
async function wrap(site, ref) {
|
|
248
|
+
return await site.realm.$(ref);
|
|
249
|
+
}
|
|
250
|
+
function splitAtFrames(scope) {
|
|
251
|
+
const hops = [];
|
|
252
|
+
let current = [];
|
|
253
|
+
for (const link of scope) {
|
|
254
|
+
if (link.frame === true) {
|
|
255
|
+
hops.push({ scope: current, link });
|
|
256
|
+
current = [];
|
|
257
|
+
} else {
|
|
258
|
+
current.push(link);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return { hops, rest: current };
|
|
262
|
+
}
|
|
263
|
+
var FrameAbsent = class extends Error {
|
|
264
|
+
name = "FrameAbsent";
|
|
265
|
+
};
|
|
266
|
+
async function topContext(browser) {
|
|
267
|
+
const handle = await browser.getWindowHandle();
|
|
268
|
+
const contexts = await browser.browsingContexts();
|
|
269
|
+
const found = contexts.find((context) => context.contextId === handle) ?? contexts[0];
|
|
270
|
+
if (found === void 0) throw new Error("harnessed: the session has no browsing context.");
|
|
271
|
+
return found;
|
|
272
|
+
}
|
|
273
|
+
async function walk(browser, scope, options, operation) {
|
|
274
|
+
const deadline = Date.now() + options.timeout;
|
|
275
|
+
const remaining = () => Math.max(0, deadline - Date.now());
|
|
276
|
+
const { hops, rest } = splitAtFrames(scope);
|
|
277
|
+
let site = { browser, realm: browser, top: true, origin: { x: 0, y: 0 } };
|
|
278
|
+
let context;
|
|
279
|
+
let switched = false;
|
|
280
|
+
try {
|
|
281
|
+
for (const hop of hops) {
|
|
282
|
+
const op = options.waiting ? "frame" : "frameNow";
|
|
283
|
+
const found = await callPage(site, op, hop.scope, hop.link, remaining());
|
|
284
|
+
if (found.element === null) throw new FrameAbsent();
|
|
285
|
+
const iframe = await wrap(site, found.element);
|
|
286
|
+
await options.onFrame?.(iframe);
|
|
287
|
+
const origin = { x: site.origin.x + found.origin.x, y: site.origin.y + found.origin.y };
|
|
288
|
+
if (browser.isBidi) {
|
|
289
|
+
context = await (context ?? await topContext(browser)).frame(iframe);
|
|
290
|
+
site = { browser, realm: context, context, top: false, origin };
|
|
291
|
+
} else {
|
|
292
|
+
await browser.switchFrame(iframe);
|
|
293
|
+
switched = true;
|
|
294
|
+
site = { browser, realm: browser, top: false, origin };
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return await operation(site, rest, remaining);
|
|
298
|
+
} finally {
|
|
299
|
+
if (switched) await browser.switchFrame(null);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// src/webdriverio-query.ts
|
|
304
|
+
var POLL_MS = 50;
|
|
305
|
+
var DISPLAYED = { opacityProperty: false };
|
|
306
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
307
|
+
function isTransient(error) {
|
|
308
|
+
const name = error?.name ?? "";
|
|
309
|
+
const message = error?.message ?? "";
|
|
310
|
+
return /stale element|element click intercepted|element not interactable/i.test(
|
|
311
|
+
`${name} ${message}`
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
function optionValues(asked, options, scope, selector) {
|
|
315
|
+
return asked.map((want) => {
|
|
316
|
+
const option = options.find((candidate) => candidate.value === want) ?? options.find((candidate) => candidate.label === want.trim());
|
|
317
|
+
if (option === void 0) {
|
|
318
|
+
const choices = options.map((candidate) => `"${candidate.label}" (${candidate.value})`);
|
|
319
|
+
throw new Error(
|
|
320
|
+
`harnessed: selectOption("${want}") on ${describeScope(scope, selector)} matches no option's value or label. Options: ${choices.join(", ")}.`
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
return option.value;
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
function isScopeMiss(error) {
|
|
327
|
+
return error instanceof FrameAbsent || error instanceof StrictModeViolation || error instanceof PageError && !isFrameEntryError(error);
|
|
328
|
+
}
|
|
329
|
+
var WebdriverioQuery = class _WebdriverioQuery extends Query {
|
|
330
|
+
constructor(browser, scope, selector) {
|
|
331
|
+
super(scope, selector);
|
|
332
|
+
this.browser = browser;
|
|
333
|
+
}
|
|
334
|
+
browser;
|
|
335
|
+
clone(selector) {
|
|
336
|
+
return new _WebdriverioQuery(this.browser, this.scope, selector);
|
|
337
|
+
}
|
|
338
|
+
/** Resolves the target, waiting for it, and reads something off it in the same round-trip. */
|
|
339
|
+
read(op, options, arg = null) {
|
|
340
|
+
return walk(
|
|
341
|
+
this.browser,
|
|
342
|
+
this.scope,
|
|
343
|
+
{ waiting: true, timeout: timeoutFor(options?.timeout) },
|
|
344
|
+
async (site, scope, remaining) => await callPage(site, op, scope, this.selector, remaining(), arg)
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Resolves the target and acts on it with WebDriver, in the document it lives
|
|
349
|
+
* in. A transient failure — the node re-rendered away, or was covered — starts
|
|
350
|
+
* over from resolution until the timeout runs out.
|
|
351
|
+
*/
|
|
352
|
+
async act(op, options, run) {
|
|
353
|
+
const timeout = timeoutFor(options?.timeout);
|
|
354
|
+
const deadline = Date.now() + timeout;
|
|
355
|
+
for (; ; ) {
|
|
356
|
+
try {
|
|
357
|
+
return await walk(
|
|
358
|
+
this.browser,
|
|
359
|
+
this.scope,
|
|
360
|
+
{ waiting: true, timeout: Math.max(0, deadline - Date.now()) },
|
|
361
|
+
async (site, scope, remaining) => {
|
|
362
|
+
const found = await callPage(site, op, scope, this.selector, remaining());
|
|
363
|
+
return run(await wrap(site, found.element), found, site);
|
|
364
|
+
}
|
|
365
|
+
);
|
|
366
|
+
} catch (error) {
|
|
367
|
+
if (!isTransient(error) || Date.now() >= deadline) throw error;
|
|
368
|
+
await sleep(POLL_MS);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
// --- interactions -------------------------------------------------------
|
|
373
|
+
async click(options) {
|
|
374
|
+
await this.act("element", options, (element) => element.click());
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Replaces the value, like Playwright's fill: whatever was there is selected
|
|
378
|
+
* and deleted by keyboard, then the new value is typed. WebDriver's own Element
|
|
379
|
+
* Clear empties the field without an input event — a controlled React input
|
|
380
|
+
* then still holds the old value in state while the DOM shows none.
|
|
381
|
+
*/
|
|
382
|
+
async fill(value, options) {
|
|
383
|
+
await this.act("clear", options, async (element, found, site) => {
|
|
384
|
+
if (found.direct === true) return element.setValue(value);
|
|
385
|
+
if (found.filled === true) await sendKeys(site, ["Backspace"]);
|
|
386
|
+
if (value !== "") await element.addValue(value);
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
async clear(options) {
|
|
390
|
+
await this.fill("", options);
|
|
391
|
+
}
|
|
392
|
+
async check(options) {
|
|
393
|
+
await this.setChecked(true, options);
|
|
394
|
+
}
|
|
395
|
+
async uncheck(options) {
|
|
396
|
+
await this.setChecked(false, options);
|
|
397
|
+
}
|
|
398
|
+
async setChecked(wanted, options) {
|
|
399
|
+
await this.act("checked", options, async (element, found) => {
|
|
400
|
+
const checked = checkedFrom(found.aria, found.native);
|
|
401
|
+
if (checked !== wanted) await element.click();
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
async selectOption(value, options) {
|
|
405
|
+
const asked = Array.isArray(value) ? value : [value];
|
|
406
|
+
await this.act("select", options, async (element, found) => {
|
|
407
|
+
const wanted = optionValues(asked, found.options, this.scope, this.selector);
|
|
408
|
+
if (found.multiple !== true) {
|
|
409
|
+
await element.selectByAttribute("value", wanted[0] ?? "");
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
const selected = found.selected;
|
|
413
|
+
const toggles = [
|
|
414
|
+
...selected.filter((option) => !wanted.includes(option)),
|
|
415
|
+
...wanted.filter((option) => !selected.includes(option))
|
|
416
|
+
];
|
|
417
|
+
for (const option of toggles) await element.selectByAttribute("value", option);
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
async hover(options) {
|
|
421
|
+
await this.act("element", options, (element) => element.moveTo());
|
|
422
|
+
}
|
|
423
|
+
async focus(options) {
|
|
424
|
+
await this.read("focus", options);
|
|
425
|
+
}
|
|
426
|
+
async blur(options) {
|
|
427
|
+
await this.read("blur", options);
|
|
428
|
+
}
|
|
429
|
+
async press(key, options) {
|
|
430
|
+
const keys = toKeySequence(key);
|
|
431
|
+
await walk(
|
|
432
|
+
this.browser,
|
|
433
|
+
this.scope,
|
|
434
|
+
{ waiting: true, timeout: timeoutFor(options?.timeout) },
|
|
435
|
+
async (site, scope, remaining) => {
|
|
436
|
+
await callPage(site, "focus", scope, this.selector, remaining());
|
|
437
|
+
await sendKeys(site, keys);
|
|
438
|
+
}
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* WebDriver sets files from paths typed into the input, so each File is
|
|
443
|
+
* written to disk first (see `filesOnDisk`): the name and bytes arrive, and
|
|
444
|
+
* the browser infers the type from the extension. The browser must share a
|
|
445
|
+
* file system with this process — a remote grid needs `browser.uploadFile`.
|
|
446
|
+
*/
|
|
447
|
+
async upload(files, options) {
|
|
448
|
+
const list = Array.isArray(files) ? files : [files];
|
|
449
|
+
await this.act("upload", options, async (element, found) => {
|
|
450
|
+
checkFileTarget(found.target, list.length, this.scope, this.selector);
|
|
451
|
+
const paths = await filesOnDisk(list);
|
|
452
|
+
if (found.filled === true && found.target.multiple) {
|
|
453
|
+
await element.clearValue();
|
|
454
|
+
}
|
|
455
|
+
await element.addValue(paths.join("\n"));
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
// --- observations -------------------------------------------------------
|
|
459
|
+
async text(options) {
|
|
460
|
+
return (await this.read("text", options)).trim();
|
|
461
|
+
}
|
|
462
|
+
async inputValue(options) {
|
|
463
|
+
return this.read("value", options);
|
|
464
|
+
}
|
|
465
|
+
async attribute(name, options) {
|
|
466
|
+
return this.read("attribute", options, name);
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* WebDriver's own visibility check, which looks at layout, applied to the
|
|
470
|
+
* target and to every iframe on the way to it: content in a hidden frame is
|
|
471
|
+
* hidden. Opacity is not part of it — see `DISPLAYED`. Answers at once, like
|
|
472
|
+
* Playwright's — a target that is not on screen is not visible — and is
|
|
473
|
+
* strict, like every single-target read.
|
|
474
|
+
*/
|
|
475
|
+
async isVisible(_options) {
|
|
476
|
+
return this.visibleNow(1);
|
|
477
|
+
}
|
|
478
|
+
async visibleNow(attempt) {
|
|
479
|
+
try {
|
|
480
|
+
return await walk(
|
|
481
|
+
this.browser,
|
|
482
|
+
this.scope,
|
|
483
|
+
{
|
|
484
|
+
waiting: false,
|
|
485
|
+
timeout: 0,
|
|
486
|
+
// A hidden frame hides everything in it, so there is no need to go in.
|
|
487
|
+
onFrame: async (iframe) => {
|
|
488
|
+
if (!await iframe.isDisplayed(DISPLAYED)) throw new FrameAbsent();
|
|
489
|
+
}
|
|
490
|
+
},
|
|
491
|
+
async (site, scope) => {
|
|
492
|
+
const probe = await callPage(site, "probe", scope, this.selector, 0);
|
|
493
|
+
if (probe.count > 1) throw strictViolation(probe.count, this.scope, this.selector);
|
|
494
|
+
if (probe.element === null) return false;
|
|
495
|
+
return (await wrap(site, probe.element)).isDisplayed(DISPLAYED);
|
|
496
|
+
}
|
|
497
|
+
);
|
|
498
|
+
} catch (error) {
|
|
499
|
+
if (error instanceof FrameAbsent) return false;
|
|
500
|
+
if (isTransient(error) && attempt < 3) return this.visibleNow(attempt + 1);
|
|
501
|
+
throw error;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
/** Measured in the target's own document, then offset by every frame crossed to reach it. */
|
|
505
|
+
async boundingBox(options) {
|
|
506
|
+
return walk(
|
|
507
|
+
this.browser,
|
|
508
|
+
this.scope,
|
|
509
|
+
{ waiting: true, timeout: timeoutFor(options?.timeout) },
|
|
510
|
+
async (site, scope, remaining) => {
|
|
511
|
+
const box = await callPage(
|
|
512
|
+
site,
|
|
513
|
+
"box",
|
|
514
|
+
scope,
|
|
515
|
+
this.selector,
|
|
516
|
+
remaining()
|
|
517
|
+
);
|
|
518
|
+
if (box === null) return { x: 0, y: 0, width: 0, height: 0 };
|
|
519
|
+
return { ...box, x: box.x + site.origin.x, y: box.y + site.origin.y };
|
|
520
|
+
}
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
async isEnabled(options) {
|
|
524
|
+
const { disabled, aria } = await this.read(
|
|
525
|
+
"enabled",
|
|
526
|
+
options
|
|
527
|
+
);
|
|
528
|
+
return enabledFrom(disabled, aria);
|
|
529
|
+
}
|
|
530
|
+
async isChecked(options) {
|
|
531
|
+
const { aria, native } = await this.read(
|
|
532
|
+
"checked",
|
|
533
|
+
options
|
|
534
|
+
);
|
|
535
|
+
return checkedFrom(aria, native);
|
|
536
|
+
}
|
|
537
|
+
async selectedOptions(options) {
|
|
538
|
+
return this.read("selected", options);
|
|
539
|
+
}
|
|
540
|
+
// --- waiting ------------------------------------------------------------
|
|
541
|
+
async waitFor(state, options) {
|
|
542
|
+
const timeout = timeoutFor(options?.timeout);
|
|
543
|
+
const deadline = Date.now() + timeout;
|
|
544
|
+
for (; ; ) {
|
|
545
|
+
if (await this.isVisible() === (state === "visible")) return;
|
|
546
|
+
const left = deadline - Date.now();
|
|
547
|
+
if (left <= 0) {
|
|
548
|
+
throw new Error(
|
|
549
|
+
`harnessed: ${describeScope(this.scope, this.selector)} did not become ${state} within ${timeout}ms.`
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
await sleep(Math.min(POLL_MS, left));
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
async count() {
|
|
556
|
+
try {
|
|
557
|
+
return await this.read("count");
|
|
558
|
+
} catch (error) {
|
|
559
|
+
if (isScopeMiss(error)) return 0;
|
|
560
|
+
throw error;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Every match's text in one round-trip, where the shared loop resolves N
|
|
565
|
+
* times. Trimmed to match `text()`, which the shared implementation uses.
|
|
566
|
+
*/
|
|
567
|
+
async texts() {
|
|
568
|
+
try {
|
|
569
|
+
return (await this.read("texts")).map((value) => value.trim());
|
|
570
|
+
} catch (error) {
|
|
571
|
+
if (isScopeMiss(error)) return [];
|
|
572
|
+
throw error;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
registerDriver(WEBDRIVERIO_DRIVER, (env, scope, selector) => {
|
|
577
|
+
return new WebdriverioQuery(env.browser, scope, selector);
|
|
578
|
+
});
|
|
579
|
+
registerNavigation(WEBDRIVERIO_DRIVER, {
|
|
580
|
+
async goto(env, url) {
|
|
581
|
+
const { browser } = env;
|
|
582
|
+
await browser.url(url);
|
|
583
|
+
rememberUrl(browser, await browser.getUrl());
|
|
584
|
+
},
|
|
585
|
+
currentUrl(env) {
|
|
586
|
+
return lastUrl(env.browser);
|
|
587
|
+
},
|
|
588
|
+
async waitForUrl(env, matches, timeout) {
|
|
589
|
+
const { browser } = env;
|
|
590
|
+
const deadline = Date.now() + timeout;
|
|
591
|
+
for (; ; ) {
|
|
592
|
+
const url = await browser.getUrl();
|
|
593
|
+
rememberUrl(browser, url);
|
|
594
|
+
if (matches(new URL(url))) return;
|
|
595
|
+
const left = deadline - Date.now();
|
|
596
|
+
if (left <= 0) {
|
|
597
|
+
throw new Error(`harnessed: the URL did not match within ${timeout}ms; it is ${url}.`);
|
|
598
|
+
}
|
|
599
|
+
await sleep(Math.min(POLL_MS, left));
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
// src/env.ts
|
|
605
|
+
function wdio(browser) {
|
|
606
|
+
return { driver: WEBDRIVERIO_DRIVER, browser };
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
export { WEBDRIVERIO_DRIVER, WebdriverioQuery, wdio };
|
|
610
|
+
//# sourceMappingURL=index.js.map
|
|
611
|
+
//# sourceMappingURL=index.js.map
|