@e2e-dev/electron 0.0.0-stage → 0.0.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 +202 -0
- package/NOTICE +5 -0
- package/README.md +91 -2
- package/dist/.build.json +1 -0
- package/dist/actionability.d.ts +41 -0
- package/dist/actionability.js +121 -0
- package/dist/actions.d.ts +52 -0
- package/dist/actions.js +418 -0
- package/dist/app-process.d.ts +114 -0
- package/dist/app-process.js +610 -0
- package/dist/budget.d.ts +33 -0
- package/dist/budget.js +64 -0
- package/dist/capture.d.ts +21 -0
- package/dist/capture.js +137 -0
- package/dist/cdp/connection.d.ts +68 -0
- package/dist/cdp/connection.js +232 -0
- package/dist/cdp/input.d.ts +67 -0
- package/dist/cdp/input.js +195 -0
- package/dist/cdp/keys.d.ts +21 -0
- package/dist/cdp/keys.js +114 -0
- package/dist/console-text.d.ts +4 -0
- package/dist/console-text.js +27 -0
- package/dist/devtools.d.ts +36 -0
- package/dist/devtools.js +111 -0
- package/dist/dialogs.d.ts +38 -0
- package/dist/dialogs.js +81 -0
- package/dist/electron.d.ts +9 -0
- package/dist/electron.js +174 -0
- package/dist/engine-entry.d.ts +13 -0
- package/dist/engine-entry.js +9 -0
- package/dist/engine.d.ts +27 -0
- package/dist/engine.js +98 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +54 -0
- package/dist/evaluate.d.ts +40 -0
- package/dist/evaluate.js +115 -0
- package/dist/executable.d.ts +29 -0
- package/dist/executable.js +249 -0
- package/dist/ffmpeg.d.ts +12 -0
- package/dist/ffmpeg.js +100 -0
- package/dist/fields.d.ts +93 -0
- package/dist/fields.js +254 -0
- package/dist/file-bound.d.ts +31 -0
- package/dist/file-bound.js +104 -0
- package/dist/fixtures.d.ts +168 -0
- package/dist/fixtures.js +6 -0
- package/dist/in-page/masks.d.ts +25 -0
- package/dist/in-page/masks.js +140 -0
- package/dist/in-page/node-ops.d.ts +96 -0
- package/dist/in-page/node-ops.js +406 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +18 -0
- package/dist/launcher.d.ts +45 -0
- package/dist/launcher.js +167 -0
- package/dist/locate.d.ts +46 -0
- package/dist/locate.js +103 -0
- package/dist/main-process.d.ts +25 -0
- package/dist/main-process.js +60 -0
- package/dist/options.d.ts +65 -0
- package/dist/options.js +120 -0
- package/dist/page.d.ts +100 -0
- package/dist/page.js +229 -0
- package/dist/runner-test.d.ts +15 -0
- package/dist/runner-test.js +45 -0
- package/dist/snapshot.d.ts +143 -0
- package/dist/snapshot.js +260 -0
- package/dist/surface.d.ts +158 -0
- package/dist/surface.js +602 -0
- package/dist/tree.d.ts +51 -0
- package/dist/tree.js +406 -0
- package/dist/video.d.ts +42 -0
- package/dist/video.js +253 -0
- package/dist/windows.d.ts +81 -0
- package/dist/windows.js +200 -0
- package/package.json +72 -3
package/dist/actions.js
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Actions: one function per kind, each waiting for its node as the kind
|
|
3
|
+
* needs, then sending trusted input through the operation's budget. A node
|
|
4
|
+
* action finds the point to act at and makes the gesture a point action
|
|
5
|
+
* makes there.
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { EngineError, parseKey, sleep, TestError, pollCondition, withTimeout, } from '@e2e-dev/core/engine';
|
|
10
|
+
import { actionablePoint, contentBox, waitReady, whole } from './actionability.js';
|
|
11
|
+
import { hasKey } from './cdp/input.js';
|
|
12
|
+
import { GONE, lostFocus, notActionable, unsupported } from './errors.js';
|
|
13
|
+
import { nextFrame, viewportSize } from './in-page/node-ops.js';
|
|
14
|
+
/** How long a long press holds by default. */
|
|
15
|
+
const LONG_PRESS_MS = 800;
|
|
16
|
+
/** How long before its operation ends a held press is released: time for the release to reach the app. */
|
|
17
|
+
const RELEASE_MARGIN_MS = 100;
|
|
18
|
+
/** How long a paced drag (`swipeTo`) takes by default. */
|
|
19
|
+
const SWIPE_TO_MS = 300;
|
|
20
|
+
/** The time between two moves of a paced drag: one frame at 60 Hz. */
|
|
21
|
+
const FRAME_MS = 16;
|
|
22
|
+
/** The share of a box one wheel scroll covers, by momentum, as the web engine scrolls. */
|
|
23
|
+
const WHEEL_SHARE = { none: 0.5, slow: 0.75, fast: 1.5 };
|
|
24
|
+
/** Performs a locator action on a node of the shown window. */
|
|
25
|
+
export function performOnNode(context, window, backendNodeId, action, budget) {
|
|
26
|
+
const step = { context, devtools: context.devtools, window, backendNodeId, operation: budget.operation, deadline: budget.waits, budget };
|
|
27
|
+
switch (action.kind) {
|
|
28
|
+
case 'tap':
|
|
29
|
+
case 'doubleTap':
|
|
30
|
+
case 'secondaryTap':
|
|
31
|
+
case 'multiTap':
|
|
32
|
+
return tap(step, action);
|
|
33
|
+
case 'hover':
|
|
34
|
+
return actionablePoint(step, false).then((point) => hover(budget, window, point));
|
|
35
|
+
case 'longPress':
|
|
36
|
+
return actionablePoint(step, true).then((point) => longPress(budget, window, point, action.durationMs));
|
|
37
|
+
case 'focus':
|
|
38
|
+
return focus(step);
|
|
39
|
+
case 'blur':
|
|
40
|
+
return waitReady(step, 'checkConnected').then(() => budget.send(() => step.devtools.callOn(window.page, backendNodeId, 'blur')));
|
|
41
|
+
case 'scrollIntoView':
|
|
42
|
+
return scrollIntoView(step);
|
|
43
|
+
case 'fill':
|
|
44
|
+
case 'clear':
|
|
45
|
+
return fill(step, action.kind === 'fill' ? action.value : '');
|
|
46
|
+
case 'check':
|
|
47
|
+
case 'uncheck':
|
|
48
|
+
return setChecked(step, action.kind === 'check');
|
|
49
|
+
case 'press':
|
|
50
|
+
return press(step, action);
|
|
51
|
+
case 'selectOption':
|
|
52
|
+
return selectOption(step, action);
|
|
53
|
+
case 'setInputFiles':
|
|
54
|
+
return setInputFiles(step, action);
|
|
55
|
+
case 'setInputFilePayloads':
|
|
56
|
+
return setInputFilePayloads(step, action);
|
|
57
|
+
case 'dragTo':
|
|
58
|
+
return dragTo(step, action);
|
|
59
|
+
case 'swipe':
|
|
60
|
+
return swipeNode(step, action);
|
|
61
|
+
case 'scrollTo':
|
|
62
|
+
// Not among the engine's declared actions, so the harness refuses it first.
|
|
63
|
+
throw unsupported('scrollTo is not supported; swipe the container, or scroll a node into view');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** A pointer action at a viewport point of the shown window. */
|
|
67
|
+
export async function performAtPoint(context, window, point, action, budget) {
|
|
68
|
+
const at = whole(point);
|
|
69
|
+
switch (action.kind) {
|
|
70
|
+
case 'tap':
|
|
71
|
+
return click(budget, window, at, {});
|
|
72
|
+
case 'doubleTap':
|
|
73
|
+
return click(budget, window, at, { clickCount: 2 });
|
|
74
|
+
case 'secondaryTap':
|
|
75
|
+
return click(budget, window, at, { button: 'right' });
|
|
76
|
+
case 'hover':
|
|
77
|
+
return hover(budget, window, at);
|
|
78
|
+
case 'longPress':
|
|
79
|
+
return longPress(budget, window, at, action.durationMs);
|
|
80
|
+
case 'dragTo':
|
|
81
|
+
return drag(budget, window, at, () => Promise.resolve(whole(action.target)));
|
|
82
|
+
case 'swipeTo':
|
|
83
|
+
return drag(budget, window, at, () => Promise.resolve(whole(action.target)), action.durationMs ?? SWIPE_TO_MS);
|
|
84
|
+
case 'swipe':
|
|
85
|
+
return swipe(budget, window, at, await window.page.evaluate(viewportSize), action);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** A swipe on the window itself, from its centre. */
|
|
89
|
+
export async function swipeWindow(context, window, action, budget) {
|
|
90
|
+
const size = await window.page.evaluate(viewportSize);
|
|
91
|
+
await swipe(budget, window, whole({ x: size.width / 2, y: size.height / 2 }), size, action);
|
|
92
|
+
}
|
|
93
|
+
/** Types into the focused element of the shown window, one character at a time: no key goes out once the caller stopped waiting. */
|
|
94
|
+
export async function typeText(context, window, text, replace, budget) {
|
|
95
|
+
const { keyboard } = window.page;
|
|
96
|
+
if (!(await context.devtools.focusedTakesText(window.page)))
|
|
97
|
+
throw notActionable('nothing that takes text has keyboard focus; tap the field first');
|
|
98
|
+
if (replace) {
|
|
99
|
+
await withModifiers(budget, window, ['ControlOrMeta'], () => keyboard.press('A'));
|
|
100
|
+
await budget.send(() => keyboard.press('Delete'));
|
|
101
|
+
}
|
|
102
|
+
await typeKeys(window, text, 0, budget);
|
|
103
|
+
}
|
|
104
|
+
/** Types text into whatever holds focus, key by key, `delayMs` apart: each character's key, or the character inserted when the layout has none. */
|
|
105
|
+
export async function typeKeys(window, text, delayMs, budget) {
|
|
106
|
+
let first = true;
|
|
107
|
+
for (const character of text) {
|
|
108
|
+
if (!first && delayMs > 0)
|
|
109
|
+
await sleep(delayMs, budget.operation.signal);
|
|
110
|
+
first = false;
|
|
111
|
+
await budget.send(() => window.page.keyboard.type(character));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** Presses a key in the shown window, wherever focus is. */
|
|
115
|
+
export async function pressKey(window, key, budget) {
|
|
116
|
+
const chord = keyChord(key);
|
|
117
|
+
await withModifiers(budget, window, chord.modifiers, () => window.page.keyboard.press(chord.key));
|
|
118
|
+
}
|
|
119
|
+
// --- node actions ---
|
|
120
|
+
async function tap(step, action) {
|
|
121
|
+
const point = await actionablePoint(step, true);
|
|
122
|
+
await click(step.budget, step.window, point, {
|
|
123
|
+
clickCount: action.kind === 'doubleTap' ? 2 : action.kind === 'multiTap' ? action.count : 1,
|
|
124
|
+
button: action.kind === 'secondaryTap' ? 'right' : 'left',
|
|
125
|
+
modifiers: action.modifiers ?? [],
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
async function focus(step) {
|
|
129
|
+
// A node that is gone fails as stale before anything is sent, so the harness locates it again.
|
|
130
|
+
await waitReady(step, 'checkConnected');
|
|
131
|
+
// pressSequentially focuses, then types into whatever holds focus: the node must be the one that does. An element that
|
|
132
|
+
// cannot take focus leaves it where it was, as on the web; the typing that follows says nothing takes text.
|
|
133
|
+
if ((await step.budget.send(() => step.devtools.callOn(step.window.page, step.backendNodeId, 'focus'))) === 'moved')
|
|
134
|
+
throw lostFocus();
|
|
135
|
+
}
|
|
136
|
+
/** Scrolls the element into view, waiting until it has a box to scroll to: an element that is not rendered cannot be. */
|
|
137
|
+
async function scrollIntoView(step) {
|
|
138
|
+
const { session } = step.window.page;
|
|
139
|
+
await pollCondition({
|
|
140
|
+
deadline: step.deadline,
|
|
141
|
+
signal: step.operation.signal,
|
|
142
|
+
negated: false,
|
|
143
|
+
evaluate: async () => {
|
|
144
|
+
await session.send('DOM.scrollIntoViewIfNeeded', { backendNodeId: step.backendNodeId }).catch(() => step.devtools.callOn(step.window.page, step.backendNodeId, 'scrollIntoView'));
|
|
145
|
+
return (await contentBox(step)) !== undefined;
|
|
146
|
+
},
|
|
147
|
+
onTimeout: () => notActionable('the element cannot be scrolled into view: it has no box on screen (hidden, or empty)'),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
async function fill(step, value) {
|
|
151
|
+
const { devtools, window, backendNodeId, budget } = step;
|
|
152
|
+
await waitReady(step, 'checkEditable');
|
|
153
|
+
// A date, time, color, or range input takes no typed text: its value is set, as a browser's own picker does.
|
|
154
|
+
const set = await budget.send(() => devtools.callOn(window.page, backendNodeId, 'setValue', value));
|
|
155
|
+
if (set !== null) {
|
|
156
|
+
if (!set.ok)
|
|
157
|
+
throw notActionable(set.reason);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
// The app's focus handler can hand focus on; text goes only to the node that still holds it.
|
|
161
|
+
if (!(await budget.send(() => devtools.callOn(window.page, backendNodeId, 'selectContents'))))
|
|
162
|
+
throw lostFocus();
|
|
163
|
+
// Text replaces the selection as typing would, with input events and no key events, as fill promises; an empty value
|
|
164
|
+
// is a Delete key, so an editor that keeps its own model sees beforeinput.
|
|
165
|
+
if (value === '')
|
|
166
|
+
await budget.send(() => window.page.keyboard.press('Delete'));
|
|
167
|
+
else
|
|
168
|
+
await budget.send(() => window.page.keyboard.insertText(value));
|
|
169
|
+
}
|
|
170
|
+
async function setChecked(step, want) {
|
|
171
|
+
const { devtools, window, backendNodeId } = step;
|
|
172
|
+
const kind = want ? 'check' : 'uncheck';
|
|
173
|
+
const now = await devtools.callOn(window.page, backendNodeId, 'readChecked');
|
|
174
|
+
if (now === null)
|
|
175
|
+
throw notActionable(`the element has no checked state to ${kind}: it is no checkbox, radio, or switch`);
|
|
176
|
+
if (now === want)
|
|
177
|
+
return;
|
|
178
|
+
// A click on a checked native radio leaves it checked: only another radio of its group clears it. A custom radio
|
|
179
|
+
// clears if its app says so, which the read after the click finds out.
|
|
180
|
+
if (!want && (await devtools.callOn(window.page, backendNodeId, 'isNativeRadio')))
|
|
181
|
+
throw notActionable('uncheck cannot clear a radio button; select another radio in its group');
|
|
182
|
+
await click(step.budget, window, await actionablePoint(step, true), {});
|
|
183
|
+
// A custom widget can set its state in a later task: the state is polled until the deadline, as the checks before the click are.
|
|
184
|
+
let read = false;
|
|
185
|
+
await pollCondition({
|
|
186
|
+
deadline: step.deadline,
|
|
187
|
+
signal: step.operation.signal,
|
|
188
|
+
negated: false,
|
|
189
|
+
evaluate: async () => {
|
|
190
|
+
let state;
|
|
191
|
+
try {
|
|
192
|
+
state = await devtools.callOn(window.page, backendNodeId, 'readChecked');
|
|
193
|
+
}
|
|
194
|
+
catch (cause) {
|
|
195
|
+
// The app answered the click by replacing the control (a "Saved" in its place): the click did its work. One
|
|
196
|
+
// that goes away after it was read unchanged did not take it.
|
|
197
|
+
if (!read && isGone(cause))
|
|
198
|
+
return true;
|
|
199
|
+
throw cause;
|
|
200
|
+
}
|
|
201
|
+
read = true;
|
|
202
|
+
return state === want;
|
|
203
|
+
},
|
|
204
|
+
onTimeout: () => notActionable(`clicking the element did not ${kind} it`),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
async function press(step, action) {
|
|
208
|
+
// A key the keyboard cannot send is refused before focus moves.
|
|
209
|
+
const chord = keyChord(action.key);
|
|
210
|
+
await waitReady(step, 'checkConnected');
|
|
211
|
+
// The key goes to what holds focus after the node is focused, as on the web: a node that cannot take focus (a plain div) leaves it where it was.
|
|
212
|
+
await step.budget.send(() => step.devtools.callOn(step.window.page, step.backendNodeId, 'focus'));
|
|
213
|
+
await withModifiers(step.budget, step.window, chord.modifiers, () => step.window.page.keyboard.press(chord.key));
|
|
214
|
+
}
|
|
215
|
+
async function selectOption(step, action) {
|
|
216
|
+
const option = typeof action.value === 'string' ? { label: action.value } : action.value;
|
|
217
|
+
// An app can fill its options in after a request: the wait lasts until the option is there and enabled.
|
|
218
|
+
await waitReady(step, 'checkSelect', option);
|
|
219
|
+
const outcome = await step.budget.send(() => step.devtools.callOn(step.window.page, step.backendNodeId, 'selectOption', option));
|
|
220
|
+
if (!outcome.ok)
|
|
221
|
+
throw notActionable(outcome.reason);
|
|
222
|
+
}
|
|
223
|
+
async function setInputFiles(step, action) {
|
|
224
|
+
const files = action.paths.map((file) => path.resolve(step.context.projectRoot, file));
|
|
225
|
+
// The DevTools call takes a path that names nothing and attaches nothing, reporting success.
|
|
226
|
+
const missing = files.find((file) => !existsSync(file));
|
|
227
|
+
if (missing !== undefined)
|
|
228
|
+
throw new TestError('INVALID_ARGUMENT', `setInputFiles: no file at ${missing}`);
|
|
229
|
+
if (!(await step.devtools.callOn(step.window.page, step.backendNodeId, 'isFileInput')))
|
|
230
|
+
return await chooseFiles(step, files);
|
|
231
|
+
await waitReady(step, 'checkFileInput');
|
|
232
|
+
const takes = await step.devtools.callOn(step.window.page, step.backendNodeId, 'takesFiles', files.length);
|
|
233
|
+
if (!takes.ok)
|
|
234
|
+
throw new TestError('INVALID_ARGUMENT', `setInputFiles: ${takes.reason}`);
|
|
235
|
+
// The DevTools call leaves the input as it was for an empty list; `[]` clears it.
|
|
236
|
+
if (files.length === 0) {
|
|
237
|
+
await step.budget.send(() => step.devtools.callOn(step.window.page, step.backendNodeId, 'clearFiles'));
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
await step.budget.send(() => step.window.page.session.send('DOM.setFileInputFiles', { files, backendNodeId: step.backendNodeId }));
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* In-memory files. The DevTools protocol sets a file input from paths only,
|
|
244
|
+
* so each file is written to the attempt's upload directory, which lasts as
|
|
245
|
+
* long as the app may read it: a page reads a chosen file when it sends it,
|
|
246
|
+
* not when it is chosen.
|
|
247
|
+
*/
|
|
248
|
+
async function setInputFilePayloads(step, action) {
|
|
249
|
+
const directory = mkdtempSync(path.join(step.context.uploads(), 'files-'));
|
|
250
|
+
const paths = action.files.map((file, index) => {
|
|
251
|
+
// A directory per file: two files may share a name, and a name is the page's to read, never a path to follow.
|
|
252
|
+
const own = path.join(directory, String(index));
|
|
253
|
+
mkdirSync(own);
|
|
254
|
+
const written = path.join(own, path.basename(file.name) || 'file');
|
|
255
|
+
writeFileSync(written, file.buffer);
|
|
256
|
+
return written;
|
|
257
|
+
});
|
|
258
|
+
await setInputFiles(step, { kind: 'setInputFiles', paths });
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Files for a control that is no file input itself: a button whose click
|
|
262
|
+
* opens a file chooser. The chooser is held back from the screen, the control
|
|
263
|
+
* clicked, and the files given to the input the chooser belongs to.
|
|
264
|
+
*/
|
|
265
|
+
async function chooseFiles(step, files) {
|
|
266
|
+
const { session } = step.window.page;
|
|
267
|
+
let opened = () => undefined;
|
|
268
|
+
const chooser = new Promise((resolve) => {
|
|
269
|
+
opened = resolve;
|
|
270
|
+
});
|
|
271
|
+
const stopListening = session.on('Page.fileChooserOpened', (event) => opened(event));
|
|
272
|
+
await session.send('Page.setInterceptFileChooserDialog', { enabled: true });
|
|
273
|
+
try {
|
|
274
|
+
await click(step.budget, step.window, await actionablePoint(step, true), {});
|
|
275
|
+
const { backendNodeId, mode } = await withTimeout(chooser, Math.max(1, step.deadline.remaining()), () => notActionable('the element is not a file input, and clicking it opened no file chooser'));
|
|
276
|
+
if (backendNodeId === undefined)
|
|
277
|
+
throw notActionable('the file chooser the element opened belongs to no input in the window');
|
|
278
|
+
if (files.length > 1 && mode !== 'selectMultiple')
|
|
279
|
+
throw new TestError('INVALID_ARGUMENT', `setInputFiles: the file chooser takes one file, not ${files.length}`);
|
|
280
|
+
// The DevTools call leaves the input as it was for an empty list, as on the input itself.
|
|
281
|
+
if (files.length === 0)
|
|
282
|
+
await step.budget.send(() => step.devtools.callOn(step.window.page, backendNodeId, 'clearFiles'));
|
|
283
|
+
else
|
|
284
|
+
await step.budget.send(() => session.send('DOM.setFileInputFiles', { files: [...files], backendNodeId }));
|
|
285
|
+
}
|
|
286
|
+
finally {
|
|
287
|
+
stopListening();
|
|
288
|
+
await session.send('Page.setInterceptFileChooserDialog', { enabled: false }).catch(() => undefined);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/** Whether a failure says the node is no longer in the document. */
|
|
292
|
+
function isGone(cause) {
|
|
293
|
+
return (cause instanceof EngineError && cause.code === 'NODE_STALE') || GONE.test(cause instanceof Error ? cause.message : String(cause));
|
|
294
|
+
}
|
|
295
|
+
async function dragTo(step, action) {
|
|
296
|
+
const target = step.context.windows.bind(action.target);
|
|
297
|
+
// The source is pressed where it is now; only then does the target scroll into view, under the held pointer. It is
|
|
298
|
+
// waited for as the source was: in view, and the topmost element at the point the drop lands on.
|
|
299
|
+
await drag(step.budget, step.window, await actionablePoint(step, true), () => actionablePoint({ ...step, backendNodeId: target.backendNodeId }, false).catch((cause) => {
|
|
300
|
+
if (cause instanceof EngineError && cause.code === 'NOT_ACTIONABLE') {
|
|
301
|
+
throw new EngineError('ACTION_MAY_HAVE_COMMITTED', `the drop target ${cause.message.replace(/^the element /, '')}, and the source was already pressed`, { retryable: false, cause });
|
|
302
|
+
}
|
|
303
|
+
throw cause;
|
|
304
|
+
}));
|
|
305
|
+
}
|
|
306
|
+
async function swipeNode(step, action) {
|
|
307
|
+
const point = await actionablePoint(step, false);
|
|
308
|
+
// The layout can change after the wait for actionability read the box.
|
|
309
|
+
const box = await contentBox(step);
|
|
310
|
+
if (box === undefined)
|
|
311
|
+
throw notActionable('the element has no box on screen to swipe');
|
|
312
|
+
await swipe(step.budget, step.window, point, box, action);
|
|
313
|
+
}
|
|
314
|
+
// --- pointer gestures at a point, which node and point actions share ---
|
|
315
|
+
async function click(budget, window, at, options) {
|
|
316
|
+
const { clickCount = 1, button = 'left', modifiers = [] } = options;
|
|
317
|
+
await withModifiers(budget, window, modifiers, () => window.page.mouse.click(at.x, at.y, button, clickCount));
|
|
318
|
+
}
|
|
319
|
+
async function hover(budget, window, at) {
|
|
320
|
+
await budget.send(() => window.page.mouse.move(at.x, at.y));
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Presses at a point and holds for `durationMs`, never past the operation's
|
|
324
|
+
* end, and releases whatever happens: a release that waited for a press
|
|
325
|
+
* longer than its operation would land on whatever control the next action
|
|
326
|
+
* leaves the pointer over.
|
|
327
|
+
*/
|
|
328
|
+
async function longPress(budget, window, at, durationMs = LONG_PRESS_MS) {
|
|
329
|
+
const { mouse } = window.page;
|
|
330
|
+
await budget.send(() => mouse.move(at.x, at.y));
|
|
331
|
+
await budget.send(() => mouse.down());
|
|
332
|
+
// The release lands before the operation's bound fires, so a press its budget cuts short is up when the step fails.
|
|
333
|
+
const held = Math.min(durationMs, Math.max(0, budget.deadline.remaining() - RELEASE_MARGIN_MS));
|
|
334
|
+
try {
|
|
335
|
+
await sleep(held, budget.operation.signal);
|
|
336
|
+
}
|
|
337
|
+
finally {
|
|
338
|
+
await mouse.up();
|
|
339
|
+
}
|
|
340
|
+
budget.check();
|
|
341
|
+
if (held < durationMs)
|
|
342
|
+
throw new EngineError('OPERATION_TIMEOUT', `the press was released after ${Math.round(held)}ms of ${durationMs}ms: the step's timeout ran out`, { retryable: false });
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Presses at `from` and drags to the point `target` answers once the pointer
|
|
346
|
+
* is down, through a move in between so drag handlers see motion before the
|
|
347
|
+
* drop; paced over `durationMs` when given.
|
|
348
|
+
*/
|
|
349
|
+
async function drag(budget, window, from, target, durationMs) {
|
|
350
|
+
const { mouse } = window.page;
|
|
351
|
+
await budget.send(() => mouse.move(from.x, from.y));
|
|
352
|
+
await budget.send(() => mouse.down());
|
|
353
|
+
try {
|
|
354
|
+
const to = await target();
|
|
355
|
+
const steps = durationMs === undefined ? 2 : Math.max(2, Math.round(durationMs / FRAME_MS));
|
|
356
|
+
for (let step = 1; step <= steps; step += 1) {
|
|
357
|
+
await budget.send(() => mouse.move(Math.round(from.x + ((to.x - from.x) * step) / steps), Math.round(from.y + ((to.y - from.y) * step) / steps)));
|
|
358
|
+
if (durationMs !== undefined)
|
|
359
|
+
await sleep(durationMs / steps, budget.operation.signal);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
finally {
|
|
363
|
+
await mouse.up();
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
/** A wheel scroll with the pointer at a point, its distance a share of `box` by momentum, then a frame for the app to render it. */
|
|
367
|
+
async function swipe(budget, window, at, box, action) {
|
|
368
|
+
const { direction } = action;
|
|
369
|
+
const extent = direction === 'up' || direction === 'down' ? box.height : box.width;
|
|
370
|
+
const distance = Math.max(1, Math.round(extent * WHEEL_SHARE[action.momentum ?? 'none']));
|
|
371
|
+
const [deltaX, deltaY] = direction === 'down' ? [0, distance] : direction === 'up' ? [0, -distance] : direction === 'right' ? [distance, 0] : [-distance, 0];
|
|
372
|
+
await budget.send(() => window.page.mouse.move(at.x, at.y));
|
|
373
|
+
await budget.send(() => window.page.mouse.wheel(deltaX, deltaY));
|
|
374
|
+
await window.page.evaluate(nextFrame);
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Sends `input` with `modifiers` held. Each press passes the budget's check
|
|
378
|
+
* on its own: a modifier the app was slow to take is not followed by the key
|
|
379
|
+
* once the caller stopped waiting. Every modifier that went down comes up,
|
|
380
|
+
* whatever happens.
|
|
381
|
+
*/
|
|
382
|
+
export async function withModifiers(budget, window, modifiers, input) {
|
|
383
|
+
const { keyboard } = window.page;
|
|
384
|
+
const held = [];
|
|
385
|
+
try {
|
|
386
|
+
for (const modifier of modifiers) {
|
|
387
|
+
// One the test already holds (`electron.keyboard.down('Shift')`) stays the test's to release.
|
|
388
|
+
if (keyboard.holds(modifier))
|
|
389
|
+
continue;
|
|
390
|
+
await budget.send(() => {
|
|
391
|
+
held.push(modifier);
|
|
392
|
+
return keyboard.down(modifier);
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
await budget.send(input);
|
|
396
|
+
}
|
|
397
|
+
finally {
|
|
398
|
+
for (const modifier of held.toReversed())
|
|
399
|
+
await keyboard.up(modifier);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* A key of the contract grammar as the keyboard presses it: the key and the
|
|
404
|
+
* modifiers held with it. The keyboard has the named keys and the characters
|
|
405
|
+
* of a US layout; any other character (`é`, `ß`) is
|
|
406
|
+
* `UNSUPPORTED_CAPABILITY`, as the contract asks of a key a surface cannot
|
|
407
|
+
* deliver.
|
|
408
|
+
*/
|
|
409
|
+
export function keyChord(key) {
|
|
410
|
+
const parsed = parseKey(key);
|
|
411
|
+
if (parsed === undefined)
|
|
412
|
+
throw unsupported(`${JSON.stringify(key)} is not a key of the contract grammar`);
|
|
413
|
+
const name = parsed.key.kind === 'char' ? parsed.key.char : parsed.key.name;
|
|
414
|
+
if (!hasKey(name)) {
|
|
415
|
+
throw unsupported(`the keyboard cannot press ${JSON.stringify(name)}: only named keys and the characters of a US keyboard have a key; type other text with fill or pressSequentially`);
|
|
416
|
+
}
|
|
417
|
+
return { key: name, modifiers: parsed.modifiers };
|
|
418
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The running app: one launch of the Electron binary, the page targets it
|
|
3
|
+
* opens attached and registered as windows, its main process reached over
|
|
4
|
+
* the inspector when it has one, and a quit that ends it within a budget.
|
|
5
|
+
* Launches and quits run one at a time.
|
|
6
|
+
*/
|
|
7
|
+
import { type EngineAppInfo, type EngineAttemptContext, type OperationContext } from '@e2e-dev/core/engine';
|
|
8
|
+
import { Budget } from './budget.ts';
|
|
9
|
+
import type { DialogRouter } from './dialogs.ts';
|
|
10
|
+
import type { ElectronMainModule } from './fixtures.ts';
|
|
11
|
+
import { type ResolvedElectronOptions } from './options.ts';
|
|
12
|
+
import { PageTarget } from './page.ts';
|
|
13
|
+
import type { Windows } from './windows.ts';
|
|
14
|
+
/** How long the app gets to close itself when its data is thrown away anyway: a `beforeunload` handler would otherwise hold every test end for the full budget. */
|
|
15
|
+
export declare const DISCARD_CLOSE_MS = 1000;
|
|
16
|
+
/** How long it gets when `app.restart()` keeps its data: time to flush it. */
|
|
17
|
+
export declare const RESTART_CLOSE_MS = 5000;
|
|
18
|
+
/** What a launch reads from the run and the attempt. */
|
|
19
|
+
export interface LaunchSetup {
|
|
20
|
+
readonly executable: string;
|
|
21
|
+
readonly projectRoot: string;
|
|
22
|
+
readonly app: EngineAppInfo;
|
|
23
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
24
|
+
readonly userDataDir: string;
|
|
25
|
+
readonly attempt: EngineAttemptContext;
|
|
26
|
+
/** What `electron.launch()` added for this attempt, over the target's arguments and the engine's `env`. */
|
|
27
|
+
readonly extra?: LaunchExtra;
|
|
28
|
+
}
|
|
29
|
+
/** The arguments and variables one attempt's launches add to the target's own. */
|
|
30
|
+
export interface LaunchExtra {
|
|
31
|
+
readonly args: readonly string[];
|
|
32
|
+
readonly env: Readonly<Record<string, string>>;
|
|
33
|
+
/** The directory the process starts in, over the engine's `cwd`. */
|
|
34
|
+
readonly cwd?: string;
|
|
35
|
+
}
|
|
36
|
+
/** A download the app started: the name the server or the link gave it, where it is saved, and Electron's state for it. */
|
|
37
|
+
export interface AppDownload {
|
|
38
|
+
readonly filename: string;
|
|
39
|
+
readonly path: string;
|
|
40
|
+
readonly state: 'progressing' | 'completed' | 'cancelled' | 'interrupted';
|
|
41
|
+
}
|
|
42
|
+
export declare class AppProcess {
|
|
43
|
+
private readonly options;
|
|
44
|
+
private readonly windows;
|
|
45
|
+
private readonly dialogs;
|
|
46
|
+
private app;
|
|
47
|
+
private exit;
|
|
48
|
+
private directory;
|
|
49
|
+
private queue;
|
|
50
|
+
constructor(options: ResolvedElectronOptions, windows: Windows, dialogs: DialogRouter);
|
|
51
|
+
/** Whether an app is running. */
|
|
52
|
+
get running(): boolean;
|
|
53
|
+
/** The directory the running app's own pages are in, once it launched; see `appDirectory`. */
|
|
54
|
+
get appDirectory(): string | undefined;
|
|
55
|
+
/** Why nothing can be read or run: the app quit on its own during the test, or was never opened. */
|
|
56
|
+
notRunning(): Error;
|
|
57
|
+
/**
|
|
58
|
+
* Runs a launch or a quit after the ones before it: `app.open()` and
|
|
59
|
+
* `app.restart()` that overlap would each start a main process, and only
|
|
60
|
+
* one is tracked.
|
|
61
|
+
*/
|
|
62
|
+
serialized<T>(work: () => Promise<T>): Promise<T>;
|
|
63
|
+
/** Quits the running app, if any: a graceful quit within `graceMs` and the budget, then its process tree killed. */
|
|
64
|
+
quit(budget: {
|
|
65
|
+
readonly timeoutMs: number;
|
|
66
|
+
}, graceMs: number): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Launches the app on the attempt's user-data directory and waits for its
|
|
69
|
+
* first window to load, all within one budget: `budget` when the operation
|
|
70
|
+
* did other work before the launch, such as quitting the app.
|
|
71
|
+
*/
|
|
72
|
+
launch(setup: LaunchSetup, operation: OperationContext, budget?: Budget): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* What a launch does once the app has a window that loaded: its main
|
|
75
|
+
* process connected, its downloads placed, its versions reported. Resolves
|
|
76
|
+
* to the app path the main process reports, or undefined when it cannot be asked.
|
|
77
|
+
*/
|
|
78
|
+
private setUp;
|
|
79
|
+
/** Starts the binary. Nothing is awaited: a binary that cannot start shows as a process that ended. */
|
|
80
|
+
private start;
|
|
81
|
+
/**
|
|
82
|
+
* Follows one launched app: its page targets attached and registered as
|
|
83
|
+
* windows once they answer, its logs sent to the attempt that launched it,
|
|
84
|
+
* and its own quit noticed. Its logs stop once another launch replaced it,
|
|
85
|
+
* so an app slow to die never writes into a later attempt; what it logs
|
|
86
|
+
* while it quits still arrives.
|
|
87
|
+
*/
|
|
88
|
+
private watch;
|
|
89
|
+
/**
|
|
90
|
+
* Starts one page's protocol domains and sends its log to the attempt,
|
|
91
|
+
* answers its dialogs, and keeps its crashed and navigated states.
|
|
92
|
+
* Resolves once the page answers, which a page with a renderer does.
|
|
93
|
+
*/
|
|
94
|
+
private follow;
|
|
95
|
+
/** The running app's main process, or why a function cannot run there. */
|
|
96
|
+
private main;
|
|
97
|
+
/** Runs a function in the app's main process; a failure is the function's own unless the app quit under it. */
|
|
98
|
+
evaluate<R, A>(fn: (electron: ElectronMainModule, arg: A) => R | Promise<R>, arg: A): Promise<R>;
|
|
99
|
+
/** Every download the app started since it launched, in order, each as it stands now. */
|
|
100
|
+
downloads(): Promise<readonly AppDownload[]>;
|
|
101
|
+
/** The OS window title of a page's `BrowserWindow`, read in the main process; undefined for a view, or when the app does not answer. */
|
|
102
|
+
windowTitle(page: PageTarget): Promise<string | undefined>;
|
|
103
|
+
/** Closes one window, past any `beforeunload` guard, and resolves once it is gone. */
|
|
104
|
+
closeWindow(page: PageTarget): Promise<void>;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The app's environment: the few variables of the run's that a process needs
|
|
108
|
+
* to start and draw (`INHERITED_ENV`, and the locale's `LC_*`), with each of
|
|
109
|
+
* `layers` over them in turn: the engine's `env` option, then a test's own. The rest of the run's environment stays
|
|
110
|
+
* with the runner: it holds provider keys and tokens, and an app can log or
|
|
111
|
+
* upload what it is given. Never `ELECTRON_RUN_AS_NODE`, which would start
|
|
112
|
+
* the binary as plain Node.js, nor `NODE_OPTIONS`.
|
|
113
|
+
*/
|
|
114
|
+
export declare function launchEnv(runEnv: Readonly<Record<string, string | undefined>>, layers: readonly Readonly<Record<string, string>>[], platform?: NodeJS.Platform): Record<string, string>;
|