@harnessed-ts/testcafe 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.
- package/CHANGELOG.md +80 -0
- package/LICENSE +21 -0
- package/README.md +30 -2
- package/dist/driver-id.d.ts +3 -0
- package/dist/driver-id.d.ts.map +1 -0
- package/dist/env.d.ts +24 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/index.cjs +772 -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 +768 -0
- package/dist/index.js.map +1 -0
- package/dist/keys.d.ts +10 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/page-functions.d.ts +77 -0
- package/dist/page-functions.d.ts.map +1 -0
- package/dist/session.d.ts +48 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/testcafe-query.d.ts +117 -0
- package/dist/testcafe-query.d.ts.map +1 -0
- package/package.json +67 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var testcafe$1 = require('testcafe');
|
|
4
|
+
var inject = require('@harnessed-ts/resolve/inject');
|
|
5
|
+
var core = require('@harnessed-ts/core');
|
|
6
|
+
|
|
7
|
+
// src/driver-id.ts
|
|
8
|
+
var TESTCAFE_DRIVER = "testcafe";
|
|
9
|
+
var REASONS = {
|
|
10
|
+
E24: "nothing matches it",
|
|
11
|
+
E26: "it is not visible",
|
|
12
|
+
E27: "it is not an element",
|
|
13
|
+
E31: "it is not editable",
|
|
14
|
+
E36: "TestCafe has no such key",
|
|
15
|
+
E39: "the iframe's content did not load",
|
|
16
|
+
E40: "it is not an <iframe>",
|
|
17
|
+
E42: "the iframe's content did not load",
|
|
18
|
+
E43: "the iframe it is in no longer exists",
|
|
19
|
+
E44: "the iframe it is in is no longer visible",
|
|
20
|
+
E49: "the page unloaded while TestCafe was reading it",
|
|
21
|
+
E84: "it timed out",
|
|
22
|
+
E101: "another element covers it"
|
|
23
|
+
};
|
|
24
|
+
function messageOf(error) {
|
|
25
|
+
if (error instanceof Error) return error.message;
|
|
26
|
+
const { errMsg } = error ?? {};
|
|
27
|
+
return typeof errMsg === "string" ? errMsg : "";
|
|
28
|
+
}
|
|
29
|
+
function toError(error, context) {
|
|
30
|
+
if (error instanceof Error) return error;
|
|
31
|
+
const { code, errMsg, errStack, reason } = error ?? {};
|
|
32
|
+
if (typeof errMsg === "string") {
|
|
33
|
+
return core.reviveStrictViolation(
|
|
34
|
+
new Error(errMsg.replace(/^[A-Za-z]*Error: /, ""), { cause: error })
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
const what = context === void 0 ? "TestCafe failed" : `TestCafe could not ${context}`;
|
|
38
|
+
const known = code === void 0 ? void 0 : REASONS[code];
|
|
39
|
+
const detail = typeof reason === "string" && reason.trim() !== "" ? `: ${reason.trim()}` : "";
|
|
40
|
+
const why = known === void 0 ? "" : `: ${known}${detail}`;
|
|
41
|
+
const which = code === void 0 ? "" : ` (TestCafe error ${code})`;
|
|
42
|
+
const page = typeof errStack === "string" ? `: the page raised ${errStack.split("\n")[0]}` : "";
|
|
43
|
+
return new Error(`harnessed: ${what}${why}${which}${page}.`, { cause: error });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/page-functions.ts
|
|
47
|
+
var NOT_INJECTED = "harnessed:not-injected";
|
|
48
|
+
function pageCall(name, source, op, scope, selector, options, arg, lookup) {
|
|
49
|
+
let api = null;
|
|
50
|
+
let home = window;
|
|
51
|
+
let current = window;
|
|
52
|
+
for (; ; ) {
|
|
53
|
+
try {
|
|
54
|
+
const found = current[name];
|
|
55
|
+
if (found !== void 0) {
|
|
56
|
+
api = found;
|
|
57
|
+
home = current;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
if (current === current.parent) break;
|
|
63
|
+
current = current.parent;
|
|
64
|
+
}
|
|
65
|
+
if (api === null) {
|
|
66
|
+
if (source === null) throw new Error("harnessed:not-injected");
|
|
67
|
+
new Function(source)();
|
|
68
|
+
api = window[name];
|
|
69
|
+
home = window;
|
|
70
|
+
}
|
|
71
|
+
const resolver = api;
|
|
72
|
+
const href = home.location.href;
|
|
73
|
+
const done = (value) => ({ value, href });
|
|
74
|
+
const isVisible = (element) => {
|
|
75
|
+
let node = element;
|
|
76
|
+
while (node !== null) {
|
|
77
|
+
if (!resolver.visible(node)) return false;
|
|
78
|
+
const view = node.ownerDocument.defaultView;
|
|
79
|
+
node = view === null || view === home ? null : view.frameElement;
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
};
|
|
83
|
+
const present = () => {
|
|
84
|
+
try {
|
|
85
|
+
return resolver.oneNow(null, scope, selector, options);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
const message = error?.message;
|
|
88
|
+
if (typeof message === "string" && /^harnessed: index \d+ is out of range/.test(message)) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const target = () => lookup === "now" ? Promise.resolve(resolver.oneNow(null, scope, selector, options)) : resolver.one(null, scope, selector, options);
|
|
95
|
+
const withTarget = (read) => target().then((node) => node === null ? { value: null, href, absent: true } : done(read(node)));
|
|
96
|
+
const list = () => {
|
|
97
|
+
if (scope.length > 0) {
|
|
98
|
+
const last = Object.assign({}, scope[scope.length - 1], { frame: false });
|
|
99
|
+
let link;
|
|
100
|
+
try {
|
|
101
|
+
link = resolver.oneNow(null, scope.slice(0, -1), last, options);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (error?.name === "FrameEntryError") throw error;
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
if (link === null) return null;
|
|
107
|
+
}
|
|
108
|
+
return resolver.allNow(null, scope, selector, options);
|
|
109
|
+
};
|
|
110
|
+
const withList = (read) => {
|
|
111
|
+
const matches = list();
|
|
112
|
+
return matches === null ? { value: null, href, absent: true } : done(read(matches));
|
|
113
|
+
};
|
|
114
|
+
switch (op) {
|
|
115
|
+
case "count":
|
|
116
|
+
return withList((matches) => matches.length);
|
|
117
|
+
case "texts":
|
|
118
|
+
return withList((matches) => matches.map((node) => (node.textContent ?? "").trim()));
|
|
119
|
+
case "visible": {
|
|
120
|
+
const node = present();
|
|
121
|
+
return done(node !== null && isVisible(node));
|
|
122
|
+
}
|
|
123
|
+
case "hidden": {
|
|
124
|
+
const node = present();
|
|
125
|
+
return done(node === null || !isVisible(node));
|
|
126
|
+
}
|
|
127
|
+
case "text":
|
|
128
|
+
return withTarget((node) => (node.textContent ?? "").trim());
|
|
129
|
+
case "value":
|
|
130
|
+
return withTarget((node) => node.value ?? "");
|
|
131
|
+
case "attribute":
|
|
132
|
+
return withTarget((node) => node.getAttribute(arg));
|
|
133
|
+
case "enabled":
|
|
134
|
+
return withTarget((node) => [
|
|
135
|
+
node.disabled === true || node.closest(":disabled") !== null,
|
|
136
|
+
node.getAttribute("aria-disabled")
|
|
137
|
+
]);
|
|
138
|
+
case "checked":
|
|
139
|
+
return withTarget((node) => [
|
|
140
|
+
node.getAttribute("aria-checked"),
|
|
141
|
+
Boolean(node.checked)
|
|
142
|
+
]);
|
|
143
|
+
case "selected":
|
|
144
|
+
return withTarget(
|
|
145
|
+
(node) => Array.prototype.map.call(
|
|
146
|
+
node.selectedOptions ?? [],
|
|
147
|
+
(option) => option.value
|
|
148
|
+
)
|
|
149
|
+
);
|
|
150
|
+
case "resolve":
|
|
151
|
+
return withTarget(() => null);
|
|
152
|
+
case "fileTarget":
|
|
153
|
+
return withTarget((node) => resolver.fileTarget(node));
|
|
154
|
+
case "box":
|
|
155
|
+
return withTarget((node) => {
|
|
156
|
+
const box = resolver.box(node);
|
|
157
|
+
if (box === null) return { x: 0, y: 0, width: 0, height: 0 };
|
|
158
|
+
let view = node.ownerDocument.defaultView;
|
|
159
|
+
while (view !== null && view !== home) {
|
|
160
|
+
const frame = view.frameElement;
|
|
161
|
+
if (frame === null) break;
|
|
162
|
+
const origin = resolver.origin(frame);
|
|
163
|
+
box.x += origin.x;
|
|
164
|
+
box.y += origin.y;
|
|
165
|
+
view = frame.ownerDocument.defaultView;
|
|
166
|
+
}
|
|
167
|
+
return box;
|
|
168
|
+
});
|
|
169
|
+
case "editable":
|
|
170
|
+
return withTarget((node) => {
|
|
171
|
+
const input = node.localName === "input";
|
|
172
|
+
const control = input || node.localName === "textarea";
|
|
173
|
+
if (!control && !node.isContentEditable) return `<${node.localName}>`;
|
|
174
|
+
if (input) {
|
|
175
|
+
const type = node.type.toLowerCase();
|
|
176
|
+
const fillable = /^(?:|email|number|password|search|tel|text|url|color|date|time|datetime|datetime-local|month|range|week)$/;
|
|
177
|
+
if (!fillable.test(type)) return `<input type="${type}">`;
|
|
178
|
+
}
|
|
179
|
+
if (control && node.matches(":disabled")) return "disabled";
|
|
180
|
+
if (node.getAttribute("aria-disabled") === "true") return "disabled";
|
|
181
|
+
if (control && node.readOnly) return "readonly";
|
|
182
|
+
return "ok";
|
|
183
|
+
});
|
|
184
|
+
case "focus":
|
|
185
|
+
return withTarget((node) => {
|
|
186
|
+
node.focus();
|
|
187
|
+
return null;
|
|
188
|
+
});
|
|
189
|
+
case "blur":
|
|
190
|
+
return withTarget((node) => {
|
|
191
|
+
node.blur();
|
|
192
|
+
return null;
|
|
193
|
+
});
|
|
194
|
+
case "select":
|
|
195
|
+
return withTarget((node) => {
|
|
196
|
+
const { values, target: named } = arg;
|
|
197
|
+
if (node.localName !== "select") {
|
|
198
|
+
throw new Error(
|
|
199
|
+
`harnessed: selectOption() needs a <select>, but ${named} is a <${node.localName}>.`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
const element = node;
|
|
203
|
+
const all = Array.prototype.slice.call(element.options);
|
|
204
|
+
const picked = values.map((value) => {
|
|
205
|
+
const option = all.find((candidate) => candidate.value === value) ?? all.find((candidate) => candidate.label === value);
|
|
206
|
+
if (option === void 0) {
|
|
207
|
+
throw new Error(`harnessed: no <option> of ${named} matches "${value}".`);
|
|
208
|
+
}
|
|
209
|
+
return option;
|
|
210
|
+
});
|
|
211
|
+
if (!element.multiple && picked.length > 1) {
|
|
212
|
+
throw new Error(
|
|
213
|
+
`harnessed: selectOption() was given several values for ${named}, a single-select.`
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
for (const option of all) option.selected = picked.includes(option);
|
|
217
|
+
const view = element.ownerDocument.defaultView ?? window;
|
|
218
|
+
element.dispatchEvent(new view.Event("input", { bubbles: true, composed: true }));
|
|
219
|
+
element.dispatchEvent(new view.Event("change", { bubbles: true }));
|
|
220
|
+
return null;
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function pickTarget() {
|
|
225
|
+
const { name, scope, selector, options, control } = harnessedTarget;
|
|
226
|
+
let api = null;
|
|
227
|
+
let current = window;
|
|
228
|
+
for (; ; ) {
|
|
229
|
+
try {
|
|
230
|
+
const found = current[name];
|
|
231
|
+
if (found !== void 0) api = found;
|
|
232
|
+
} catch {
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
if (current === current.parent) break;
|
|
236
|
+
current = current.parent;
|
|
237
|
+
}
|
|
238
|
+
if (api === null) return [];
|
|
239
|
+
const node = api.oneNow(null, scope, selector, options);
|
|
240
|
+
if (node === null) return [];
|
|
241
|
+
const labelled = control === true && node.localName === "label";
|
|
242
|
+
const target = labelled ? node.control : node;
|
|
243
|
+
return target === null ? [] : [target];
|
|
244
|
+
}
|
|
245
|
+
function currentHref() {
|
|
246
|
+
return window.location.href;
|
|
247
|
+
}
|
|
248
|
+
function browserPlatform() {
|
|
249
|
+
return window.navigator.platform;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/session.ts
|
|
253
|
+
var sessions = /* @__PURE__ */ new WeakMap();
|
|
254
|
+
function assertOwnController(t) {
|
|
255
|
+
if (typeof t.testRun !== "object") {
|
|
256
|
+
throw new Error(
|
|
257
|
+
"harnessed: pass the test function's own t, not the one imported from 'testcafe': test('\u2026', async t => { \u2026 testcafe(t) \u2026 })."
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function sessionFor(t) {
|
|
262
|
+
const existing = sessions.get(t);
|
|
263
|
+
if (existing !== void 0) return existing;
|
|
264
|
+
assertOwnController(t);
|
|
265
|
+
const call = testcafe$1.ClientFunction(pageCall, { boundTestRun: t });
|
|
266
|
+
const href = testcafe$1.ClientFunction(currentHref, { boundTestRun: t });
|
|
267
|
+
const readPlatform = testcafe$1.ClientFunction(browserPlatform, { boundTestRun: t });
|
|
268
|
+
let platform;
|
|
269
|
+
let queue = Promise.resolve();
|
|
270
|
+
let carrySource = true;
|
|
271
|
+
const seen = (url) => {
|
|
272
|
+
if (session.href !== void 0 && url !== session.href) carrySource = true;
|
|
273
|
+
session.href = url;
|
|
274
|
+
};
|
|
275
|
+
const session = {
|
|
276
|
+
href: void 0,
|
|
277
|
+
async read(op, scope, selector, options, arg, lookup) {
|
|
278
|
+
const run = async (source) => await call(inject.PAGE_API_GLOBAL, source, op, scope, selector, options, arg, lookup);
|
|
279
|
+
let result;
|
|
280
|
+
try {
|
|
281
|
+
result = await run(carrySource ? inject.injectSource() : null);
|
|
282
|
+
} catch (error) {
|
|
283
|
+
if (!messageOf(error).includes(NOT_INJECTED)) throw toError(error);
|
|
284
|
+
try {
|
|
285
|
+
result = await run(inject.injectSource());
|
|
286
|
+
} catch (retried) {
|
|
287
|
+
throw toError(retried);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
carrySource = false;
|
|
291
|
+
session.href = result.href;
|
|
292
|
+
return result;
|
|
293
|
+
},
|
|
294
|
+
async locate() {
|
|
295
|
+
let current;
|
|
296
|
+
try {
|
|
297
|
+
current = await href();
|
|
298
|
+
} catch (error) {
|
|
299
|
+
throw toError(error);
|
|
300
|
+
}
|
|
301
|
+
seen(current);
|
|
302
|
+
return current;
|
|
303
|
+
},
|
|
304
|
+
async platform() {
|
|
305
|
+
if (platform === void 0) {
|
|
306
|
+
try {
|
|
307
|
+
platform = await readPlatform();
|
|
308
|
+
} catch (error) {
|
|
309
|
+
throw toError(error);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return platform;
|
|
313
|
+
},
|
|
314
|
+
navigated() {
|
|
315
|
+
carrySource = true;
|
|
316
|
+
},
|
|
317
|
+
exclusive(run) {
|
|
318
|
+
const next = queue.then(run, run);
|
|
319
|
+
queue = next.catch(() => void 0);
|
|
320
|
+
return next;
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
sessions.set(t, session);
|
|
324
|
+
return session;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// src/keys.ts
|
|
328
|
+
var NAMES = {
|
|
329
|
+
" ": "space",
|
|
330
|
+
Space: "space",
|
|
331
|
+
Enter: "enter",
|
|
332
|
+
Tab: "tab",
|
|
333
|
+
Backspace: "backspace",
|
|
334
|
+
Delete: "delete",
|
|
335
|
+
Escape: "esc",
|
|
336
|
+
Insert: "ins",
|
|
337
|
+
Home: "home",
|
|
338
|
+
End: "end",
|
|
339
|
+
PageUp: "pageup",
|
|
340
|
+
PageDown: "pagedown",
|
|
341
|
+
ArrowUp: "up",
|
|
342
|
+
ArrowDown: "down",
|
|
343
|
+
ArrowLeft: "left",
|
|
344
|
+
ArrowRight: "right",
|
|
345
|
+
Control: "ctrl",
|
|
346
|
+
Shift: "shift",
|
|
347
|
+
Alt: "alt",
|
|
348
|
+
Meta: "meta",
|
|
349
|
+
CapsLock: "capslock"
|
|
350
|
+
};
|
|
351
|
+
var SUPPORTED = /* @__PURE__ */ new Set([
|
|
352
|
+
"alt",
|
|
353
|
+
"ctrl",
|
|
354
|
+
"meta",
|
|
355
|
+
"shift",
|
|
356
|
+
"backspace",
|
|
357
|
+
"capslock",
|
|
358
|
+
"delete",
|
|
359
|
+
"down",
|
|
360
|
+
"end",
|
|
361
|
+
"enter",
|
|
362
|
+
"esc",
|
|
363
|
+
"home",
|
|
364
|
+
"ins",
|
|
365
|
+
"left",
|
|
366
|
+
"pagedown",
|
|
367
|
+
"pageup",
|
|
368
|
+
"right",
|
|
369
|
+
"space",
|
|
370
|
+
"tab",
|
|
371
|
+
"up"
|
|
372
|
+
]);
|
|
373
|
+
function parts(key) {
|
|
374
|
+
if (key.length <= 1) return [key];
|
|
375
|
+
const plus = key.endsWith("++");
|
|
376
|
+
const split = (plus ? key.slice(0, -2) : key).split("+");
|
|
377
|
+
return plus ? [...split, "+"] : split;
|
|
378
|
+
}
|
|
379
|
+
function toPart(part, platform) {
|
|
380
|
+
if (part === "ControlOrMeta") {
|
|
381
|
+
return platform !== void 0 && /^(?:Mac|iP)/.test(platform) ? "meta" : "ctrl";
|
|
382
|
+
}
|
|
383
|
+
const code = /^(?:Key([A-Z])|Digit(\d))$/.exec(part);
|
|
384
|
+
if (code !== null) return (code[1] ?? code[2]).toLowerCase();
|
|
385
|
+
const named = NAMES[part];
|
|
386
|
+
if (named !== void 0) return named;
|
|
387
|
+
if (part.length === 1) return part;
|
|
388
|
+
return SUPPORTED.has(part) ? part : void 0;
|
|
389
|
+
}
|
|
390
|
+
function toTestCafeKey(key, platform) {
|
|
391
|
+
return parts(key).map((part) => {
|
|
392
|
+
const mapped = toPart(part, platform);
|
|
393
|
+
if (mapped === void 0) {
|
|
394
|
+
throw new Error(
|
|
395
|
+
`harnessed: press() cannot send "${key}" under TestCafe, which has no "${part}" key.`
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
return mapped;
|
|
399
|
+
}).join("+");
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// src/testcafe-query.ts
|
|
403
|
+
var POLL_MS = 50;
|
|
404
|
+
var LAST_WAIT_MS = POLL_MS;
|
|
405
|
+
var NOT_EDITABLE = {
|
|
406
|
+
disabled: "it is disabled",
|
|
407
|
+
readonly: "it is readonly"
|
|
408
|
+
};
|
|
409
|
+
function unframed(link) {
|
|
410
|
+
return { ...link, frame: void 0 };
|
|
411
|
+
}
|
|
412
|
+
function delay(ms) {
|
|
413
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
414
|
+
}
|
|
415
|
+
var TestCafeQuery = class _TestCafeQuery extends core.Query {
|
|
416
|
+
constructor(t, scope, selector) {
|
|
417
|
+
super(scope, selector);
|
|
418
|
+
this.t = t;
|
|
419
|
+
}
|
|
420
|
+
t;
|
|
421
|
+
clone(selector) {
|
|
422
|
+
return new _TestCafeQuery(this.t, this.scope, selector);
|
|
423
|
+
}
|
|
424
|
+
get session() {
|
|
425
|
+
return sessionFor(this.t);
|
|
426
|
+
}
|
|
427
|
+
get described() {
|
|
428
|
+
return core.describeScope(this.scope, this.selector);
|
|
429
|
+
}
|
|
430
|
+
/** Runtime config travels with every call: the page cannot see `configure()`. */
|
|
431
|
+
pageOptions(timeout) {
|
|
432
|
+
core.checkTestIds(this.scope, this.selector);
|
|
433
|
+
return { testIdAttribute: core.getConfig().testIdAttribute, timeout: core.timeoutFor(timeout) };
|
|
434
|
+
}
|
|
435
|
+
/** One round trip, answered at once. */
|
|
436
|
+
async read(op, options, arg) {
|
|
437
|
+
const result = await this.session.read(
|
|
438
|
+
op,
|
|
439
|
+
this.scope.map(inject.encodeSelector),
|
|
440
|
+
inject.encodeSelector(this.selector),
|
|
441
|
+
this.pageOptions(options?.timeout),
|
|
442
|
+
arg,
|
|
443
|
+
"now"
|
|
444
|
+
);
|
|
445
|
+
return result.value;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* A list operation, polled from Node until its scope is there, as every
|
|
449
|
+
* driver waits for the scope; the list itself answers at once. A scope that
|
|
450
|
+
* never appears holds nothing, so the deadline answers `empty` rather than
|
|
451
|
+
* failing.
|
|
452
|
+
*/
|
|
453
|
+
async readList(op, empty) {
|
|
454
|
+
const timeout = core.timeoutFor();
|
|
455
|
+
const deadline = Date.now() + timeout;
|
|
456
|
+
const scope = this.scope.map(inject.encodeSelector);
|
|
457
|
+
const selector = inject.encodeSelector(this.selector);
|
|
458
|
+
for (; ; ) {
|
|
459
|
+
const result = await this.session.read(
|
|
460
|
+
op,
|
|
461
|
+
scope,
|
|
462
|
+
selector,
|
|
463
|
+
this.pageOptions(timeout),
|
|
464
|
+
void 0,
|
|
465
|
+
"now"
|
|
466
|
+
);
|
|
467
|
+
if (result.absent !== true) return result.value;
|
|
468
|
+
if (Date.now() >= deadline) return empty;
|
|
469
|
+
await delay(POLL_MS);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* A single-target operation, polled from Node until its node is there. The
|
|
474
|
+
* last poll waits briefly in the page, so a target that never appears fails
|
|
475
|
+
* with the resolver's own message; a strict violation fails on the first.
|
|
476
|
+
*/
|
|
477
|
+
async readOne(op, options, arg) {
|
|
478
|
+
const timeout = core.timeoutFor(options?.timeout);
|
|
479
|
+
const deadline = Date.now() + timeout;
|
|
480
|
+
const scope = this.scope.map(inject.encodeSelector);
|
|
481
|
+
const selector = inject.encodeSelector(this.selector);
|
|
482
|
+
for (; ; ) {
|
|
483
|
+
const last = Date.now() >= deadline;
|
|
484
|
+
const result = await this.session.read(
|
|
485
|
+
op,
|
|
486
|
+
scope,
|
|
487
|
+
selector,
|
|
488
|
+
this.pageOptions(last ? LAST_WAIT_MS : timeout),
|
|
489
|
+
arg,
|
|
490
|
+
last ? "wait" : "now"
|
|
491
|
+
);
|
|
492
|
+
if (result.absent !== true) return result.value;
|
|
493
|
+
await delay(POLL_MS);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
/** A TestCafe selector for one link of the chain, resolved by the same resolver. */
|
|
497
|
+
selectorFor(scope, selector, timeout, control = false) {
|
|
498
|
+
const harnessedTarget2 = {
|
|
499
|
+
name: inject.PAGE_API_GLOBAL,
|
|
500
|
+
scope: scope.map(inject.encodeSelector),
|
|
501
|
+
selector: inject.encodeSelector(selector),
|
|
502
|
+
options: this.pageOptions(timeout),
|
|
503
|
+
control
|
|
504
|
+
};
|
|
505
|
+
return testcafe$1.Selector(pickTarget, {
|
|
506
|
+
dependencies: { harnessedTarget: harnessedTarget2 },
|
|
507
|
+
boundTestRun: this.t,
|
|
508
|
+
timeout
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Runs a TestCafe action against this query's node.
|
|
513
|
+
*
|
|
514
|
+
* The resolver answers first, from the page: that is what makes a strict
|
|
515
|
+
* violation, a missing target or a frame marker on a non-iframe fail at once
|
|
516
|
+
* with the shared wording. An error thrown inside a TestCafe selector is
|
|
517
|
+
* instead retried until the selector times out, and a strict violation never
|
|
518
|
+
* becomes a single match by waiting. `prepare` then refuses what TestCafe
|
|
519
|
+
* would get silently wrong, within the same deadline. After that, TestCafe's
|
|
520
|
+
* own selectors only have to wait for the node to be actionable — each frame
|
|
521
|
+
* switch and the action given what is left of that one deadline, so the
|
|
522
|
+
* whole operation fails within the caller's timeout.
|
|
523
|
+
*/
|
|
524
|
+
act(run, options, prepare, control = false) {
|
|
525
|
+
return this.session.exclusive(async () => {
|
|
526
|
+
const timeout = core.timeoutFor(options?.timeout);
|
|
527
|
+
const deadline = Date.now() + timeout;
|
|
528
|
+
await this.readOne("resolve", { timeout });
|
|
529
|
+
if (prepare !== void 0) await prepare(deadline, timeout);
|
|
530
|
+
const remaining = () => Math.max(deadline - Date.now(), 0);
|
|
531
|
+
const frames = this.scope.flatMap((link, index) => link.frame === true ? [index] : []);
|
|
532
|
+
try {
|
|
533
|
+
for (const index of frames) {
|
|
534
|
+
await this.t.switchToIframe(
|
|
535
|
+
this.selectorFor(this.scope.slice(0, index), unframed(this.scope[index]), remaining())
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
await run(this.selectorFor(this.scope, this.selector, remaining(), control), deadline);
|
|
539
|
+
} catch (error) {
|
|
540
|
+
throw toError(error, `act on ${this.described}`);
|
|
541
|
+
} finally {
|
|
542
|
+
if (frames.length > 0) await this.t.switchToMainWindow();
|
|
543
|
+
}
|
|
544
|
+
await this.session.locate();
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Waits until the target can be typed into, and refuses one that never can.
|
|
549
|
+
* TestCafe's typeText would otherwise do nothing to a disabled or readonly
|
|
550
|
+
* control, and type into the first editable descendant of a wrapper.
|
|
551
|
+
*/
|
|
552
|
+
async editable(action, deadline, timeout) {
|
|
553
|
+
for (; ; ) {
|
|
554
|
+
const state = await this.readOne("editable", {
|
|
555
|
+
timeout: Math.max(deadline - Date.now(), 0)
|
|
556
|
+
});
|
|
557
|
+
if (state === "ok") return;
|
|
558
|
+
if (state !== "disabled" && state !== "readonly") {
|
|
559
|
+
throw new Error(
|
|
560
|
+
`harnessed: ${action}() needs an <input>, <textarea> or [contenteditable] element, but ${this.described} is a ${state}.`
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
if (Date.now() >= deadline) {
|
|
564
|
+
throw new Error(
|
|
565
|
+
`harnessed: ${action}() could not edit ${this.described} within ${timeout}ms: ${NOT_EDITABLE[state]}.`
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
await delay(POLL_MS);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
// --- interactions -------------------------------------------------------
|
|
572
|
+
async click(options) {
|
|
573
|
+
await this.act((target) => this.t.click(target), options);
|
|
574
|
+
}
|
|
575
|
+
async fill(value, options) {
|
|
576
|
+
await this.replaceValue("fill", value, options);
|
|
577
|
+
}
|
|
578
|
+
async clear(options) {
|
|
579
|
+
await this.replaceValue("clear", "", options);
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Replaces the value, as Playwright's `fill()` does. `paste` sets the whole
|
|
583
|
+
* value in one input event rather than typing character by character; an
|
|
584
|
+
* empty value — which typeText refuses — selects what is there and deletes
|
|
585
|
+
* it, so `fill('')` and `clear()` are one operation with one set of checks.
|
|
586
|
+
*/
|
|
587
|
+
async replaceValue(action, value, options) {
|
|
588
|
+
await this.act(
|
|
589
|
+
(target) => value === "" ? this.t.selectText(target).pressKey("delete") : this.t.typeText(target, value, { replace: true, paste: true }),
|
|
590
|
+
options,
|
|
591
|
+
(deadline, timeout) => this.editable(action, deadline, timeout)
|
|
592
|
+
);
|
|
593
|
+
}
|
|
594
|
+
async check(options) {
|
|
595
|
+
if (!await this.isChecked(options)) await this.click(options);
|
|
596
|
+
}
|
|
597
|
+
async uncheck(options) {
|
|
598
|
+
if (await this.isChecked(options)) await this.click(options);
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* TestCafe has no select API, and driving a native dropdown by clicking is
|
|
602
|
+
* platform-dependent. The selection is set in the page and `input` and
|
|
603
|
+
* `change` dispatched — what Playwright's selectOption does — replacing any
|
|
604
|
+
* existing selection of a multi-select. A change handler can navigate, so
|
|
605
|
+
* this is an action like any other: it takes its turn, and re-reads the URL.
|
|
606
|
+
*/
|
|
607
|
+
async selectOption(value, options) {
|
|
608
|
+
const arg = {
|
|
609
|
+
values: Array.isArray(value) ? value : [value],
|
|
610
|
+
target: this.described
|
|
611
|
+
};
|
|
612
|
+
await this.session.exclusive(async () => {
|
|
613
|
+
await this.readOne("select", options, arg);
|
|
614
|
+
await this.session.locate();
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* TestCafe sets files only from paths, so each File is written to disk first
|
|
619
|
+
* (see `filesOnDisk`): the name and bytes arrive, and the type is inferred
|
|
620
|
+
* from the extension.
|
|
621
|
+
*/
|
|
622
|
+
async upload(files, options) {
|
|
623
|
+
const list = Array.isArray(files) ? files : [files];
|
|
624
|
+
let paths = [];
|
|
625
|
+
await this.act(
|
|
626
|
+
(target) => this.t.setFilesToUpload(target, paths),
|
|
627
|
+
options,
|
|
628
|
+
async (deadline) => {
|
|
629
|
+
const target = await this.readOne("fileTarget", {
|
|
630
|
+
timeout: Math.max(deadline - Date.now(), 0)
|
|
631
|
+
});
|
|
632
|
+
core.checkFileTarget(target, list.length, this.scope, this.selector);
|
|
633
|
+
paths = await inject.filesOnDisk(list);
|
|
634
|
+
},
|
|
635
|
+
true
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
async hover(options) {
|
|
639
|
+
await this.act((target) => this.t.hover(target), options);
|
|
640
|
+
}
|
|
641
|
+
async focus(options) {
|
|
642
|
+
await this.readOne("focus", options);
|
|
643
|
+
}
|
|
644
|
+
async blur(options) {
|
|
645
|
+
await this.readOne("blur", options);
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Focuses the target first, as Playwright's press does; keys go to the
|
|
649
|
+
* focused node. The key is checked before anything happens, and the focus
|
|
650
|
+
* spends only what is left of the one timeout. `ControlOrMeta` follows the
|
|
651
|
+
* browser's platform, which may not be the test runner's.
|
|
652
|
+
*/
|
|
653
|
+
async press(key, options) {
|
|
654
|
+
const platform = key.includes("ControlOrMeta") ? await this.session.platform() : void 0;
|
|
655
|
+
const keys = toTestCafeKey(key, platform);
|
|
656
|
+
await this.act(async (_target, deadline) => {
|
|
657
|
+
await this.readOne("focus", { timeout: Math.max(deadline - Date.now(), 0) });
|
|
658
|
+
await this.t.pressKey(keys);
|
|
659
|
+
}, options);
|
|
660
|
+
}
|
|
661
|
+
// --- observations -------------------------------------------------------
|
|
662
|
+
async text(options) {
|
|
663
|
+
return this.readOne("text", options);
|
|
664
|
+
}
|
|
665
|
+
async inputValue(options) {
|
|
666
|
+
return this.readOne("value", options);
|
|
667
|
+
}
|
|
668
|
+
async attribute(name, options) {
|
|
669
|
+
return this.readOne("attribute", options, name);
|
|
670
|
+
}
|
|
671
|
+
async boundingBox(options) {
|
|
672
|
+
return this.readOne("box", options);
|
|
673
|
+
}
|
|
674
|
+
/** Answers at once, as Playwright's does: an absent target is not visible. */
|
|
675
|
+
async isVisible(options) {
|
|
676
|
+
return this.read("visible", options);
|
|
677
|
+
}
|
|
678
|
+
async isEnabled(options) {
|
|
679
|
+
const [disabled, ariaDisabled] = await this.readOne(
|
|
680
|
+
"enabled",
|
|
681
|
+
options
|
|
682
|
+
);
|
|
683
|
+
return core.enabledFrom(disabled, ariaDisabled);
|
|
684
|
+
}
|
|
685
|
+
async isChecked(options) {
|
|
686
|
+
const [aria, native] = await this.readOne("checked", options);
|
|
687
|
+
return core.checkedFrom(aria, native);
|
|
688
|
+
}
|
|
689
|
+
async selectedOptions(options) {
|
|
690
|
+
return this.readOne("selected", options);
|
|
691
|
+
}
|
|
692
|
+
// --- waiting ------------------------------------------------------------
|
|
693
|
+
/** Polls from Node rather than waiting inside the page, as every read here does. */
|
|
694
|
+
async waitFor(state, options) {
|
|
695
|
+
const timeout = core.timeoutFor(options?.timeout);
|
|
696
|
+
const deadline = Date.now() + timeout;
|
|
697
|
+
for (; ; ) {
|
|
698
|
+
if (await this.read(state, { timeout })) return;
|
|
699
|
+
if (Date.now() >= deadline) {
|
|
700
|
+
throw new Error(`harnessed: ${this.described} did not become ${state} within ${timeout}ms.`);
|
|
701
|
+
}
|
|
702
|
+
await delay(POLL_MS);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
/** Waits for the scope chain, as every driver does; the count itself answers at once. */
|
|
706
|
+
async count() {
|
|
707
|
+
return this.readList("count", 0);
|
|
708
|
+
}
|
|
709
|
+
/** Every match read in one round trip, once the scope is there; trimmed to match `text()`. */
|
|
710
|
+
async texts() {
|
|
711
|
+
return this.readList("texts", []);
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
core.registerDriver(TESTCAFE_DRIVER, (env, scope, selector) => {
|
|
715
|
+
return new TestCafeQuery(env.t, scope, selector);
|
|
716
|
+
});
|
|
717
|
+
core.registerNavigation(TESTCAFE_DRIVER, {
|
|
718
|
+
async goto(env, url) {
|
|
719
|
+
const { t, baseUrl } = env;
|
|
720
|
+
const session = sessionFor(t);
|
|
721
|
+
const base = baseUrl ?? await session.locate();
|
|
722
|
+
let target;
|
|
723
|
+
try {
|
|
724
|
+
target = new URL(url, base).href;
|
|
725
|
+
} catch {
|
|
726
|
+
throw new Error(
|
|
727
|
+
`harnessed: cannot resolve "${url}" against ${base}. Navigate to the app first, or pass \`baseUrl\` to testcafe(t, { baseUrl }).`
|
|
728
|
+
);
|
|
729
|
+
}
|
|
730
|
+
try {
|
|
731
|
+
await t.navigateTo(target);
|
|
732
|
+
} catch (error) {
|
|
733
|
+
throw toError(error, `navigate to ${target}`);
|
|
734
|
+
} finally {
|
|
735
|
+
session.navigated();
|
|
736
|
+
}
|
|
737
|
+
await session.locate();
|
|
738
|
+
},
|
|
739
|
+
currentUrl(env) {
|
|
740
|
+
const { href } = sessionFor(env.t);
|
|
741
|
+
if (href === void 0) {
|
|
742
|
+
throw new Error(
|
|
743
|
+
"harnessed: the testcafe driver has not read the URL yet. TestCafe reads the page asynchronously, so the URL is learned from goto() and from every harness call \u2014 make one of those first."
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
return href;
|
|
747
|
+
},
|
|
748
|
+
async waitForUrl(env, matches, timeout) {
|
|
749
|
+
const session = sessionFor(env.t);
|
|
750
|
+
const deadline = Date.now() + timeout;
|
|
751
|
+
for (; ; ) {
|
|
752
|
+
const href = await session.locate();
|
|
753
|
+
if (matches(new URL(href))) return;
|
|
754
|
+
if (Date.now() >= deadline) {
|
|
755
|
+
throw new Error(`harnessed: the URL did not match within ${timeout}ms; it is ${href}.`);
|
|
756
|
+
}
|
|
757
|
+
await delay(POLL_MS);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
// src/env.ts
|
|
763
|
+
function testcafe(t, options = {}) {
|
|
764
|
+
assertOwnController(t);
|
|
765
|
+
return options.baseUrl === void 0 ? { driver: TESTCAFE_DRIVER, t } : { driver: TESTCAFE_DRIVER, t, baseUrl: options.baseUrl };
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
exports.TESTCAFE_DRIVER = TESTCAFE_DRIVER;
|
|
769
|
+
exports.TestCafeQuery = TestCafeQuery;
|
|
770
|
+
exports.testcafe = testcafe;
|
|
771
|
+
//# sourceMappingURL=index.cjs.map
|
|
772
|
+
//# sourceMappingURL=index.cjs.map
|