@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/surface.js
ADDED
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Electron surface: one app per attempt, launched by `app.open()` with
|
|
3
|
+
* an empty user-data directory of the attempt's own, its windows read
|
|
4
|
+
* through Chromium's accessibility tree over the DevTools protocol and
|
|
5
|
+
* driven with trusted input events. Everything here goes through the engine
|
|
6
|
+
* contract of `@e2e-dev/core/engine` and the DevTools protocol itself: the
|
|
7
|
+
* engine starts the binary and speaks the protocol with no library between.
|
|
8
|
+
*
|
|
9
|
+
* The state has three lifetimes, each in a module of its own: the worker's
|
|
10
|
+
* (options, executable), the attempt's (its user-data directory and
|
|
11
|
+
* artifacts), and the running app's (`AppProcess` and `Windows`).
|
|
12
|
+
*/
|
|
13
|
+
import { copyFileSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs';
|
|
14
|
+
import { tmpdir } from 'node:os';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import { ConfigurationError, Deadline, EngineError, pollCondition, sleep, TestError, validateComputedStyleProperties } from '@e2e-dev/core/engine';
|
|
17
|
+
import { performAtPoint, performOnNode, pressKey, swipeWindow, typeText } from './actions.js';
|
|
18
|
+
import { AppProcess, DISCARD_CLOSE_MS, RESTART_CLOSE_MS } from './app-process.js';
|
|
19
|
+
import { answerWithin, Budget, withBudget } from './budget.js';
|
|
20
|
+
import { screenshotFile, screenshotName, withPixels } from './capture.js';
|
|
21
|
+
import { DevTools } from './devtools.js';
|
|
22
|
+
import { DialogRouter } from './dialogs.js';
|
|
23
|
+
import { closedWindow, invalidState, notActionable } from './errors.js';
|
|
24
|
+
import { checkJsonArgument, EvaluateError, functionSource } from './evaluate.js';
|
|
25
|
+
import { ensureFfmpeg } from './ffmpeg.js';
|
|
26
|
+
import { outsideApp } from './file-bound.js';
|
|
27
|
+
import { appPathProblem, describeExecutable, ensureElectronBinary, resolveExecutable } from './executable.js';
|
|
28
|
+
import { locateIn, projectRead, readWindow } from './locate.js';
|
|
29
|
+
import { checkCwd, checkLaunchArguments } from './options.js';
|
|
30
|
+
import { Recorder } from './video.js';
|
|
31
|
+
import { ROOT_ID, Windows } from './windows.js';
|
|
32
|
+
/** How long a read waits for a crashed window to load again. */
|
|
33
|
+
const CRASH_RELOAD_WAIT_MS = 2_000;
|
|
34
|
+
/** How long the read that hands the trace an action's screen may take. */
|
|
35
|
+
const TRACE_READ_MS = 1_000;
|
|
36
|
+
/** How long a window gets to say its document's title. */
|
|
37
|
+
const TITLE_MS = 1_000;
|
|
38
|
+
const NO_EXTRA = { args: [], env: {} };
|
|
39
|
+
function crashed() {
|
|
40
|
+
return invalidState('the window\'s renderer crashed: reload it from the main process with electron.evaluate, or restart the app');
|
|
41
|
+
}
|
|
42
|
+
export class ElectronSurface {
|
|
43
|
+
options;
|
|
44
|
+
projectRoot = process.cwd();
|
|
45
|
+
appInfo = {};
|
|
46
|
+
runEnv = {};
|
|
47
|
+
executable;
|
|
48
|
+
attempt;
|
|
49
|
+
userDataDir;
|
|
50
|
+
/** What `electron.launch()` added to this attempt's launches. */
|
|
51
|
+
launchExtra = NO_EXTRA;
|
|
52
|
+
uploadsDir;
|
|
53
|
+
artifactCounter = 0;
|
|
54
|
+
recorder;
|
|
55
|
+
/** Recordings started this attempt, for their file names. */
|
|
56
|
+
recordings = 0;
|
|
57
|
+
devtools = new DevTools();
|
|
58
|
+
windowList = new Windows();
|
|
59
|
+
dialogs = new DialogRouter();
|
|
60
|
+
app;
|
|
61
|
+
constructor(options) {
|
|
62
|
+
this.options = options;
|
|
63
|
+
this.app = new AppProcess(options, this.windowList, this.dialogs);
|
|
64
|
+
this.windowList.onShown = (window) => this.recorder?.follow(window?.page);
|
|
65
|
+
}
|
|
66
|
+
// --- run and worker lifecycle ---
|
|
67
|
+
async prepare(info) {
|
|
68
|
+
await ensureElectronBinary(info.projectRoot, this.options.executablePath, info.env, info.log, info.signal);
|
|
69
|
+
}
|
|
70
|
+
doctor(info) {
|
|
71
|
+
const executable = describeExecutable(info.projectRoot, this.options.executablePath, info.env);
|
|
72
|
+
const checks = [{ id: 'electron', status: executable.status, message: executable.message }];
|
|
73
|
+
const problem = info.app.appPath === undefined ? undefined : appPathProblem(info.projectRoot, info.app.appPath);
|
|
74
|
+
if (problem !== undefined)
|
|
75
|
+
checks.push({ id: 'electron-app', status: 'fail', message: problem });
|
|
76
|
+
return Promise.resolve(checks);
|
|
77
|
+
}
|
|
78
|
+
async init(info) {
|
|
79
|
+
this.projectRoot = info.projectRoot;
|
|
80
|
+
this.appInfo = info.app;
|
|
81
|
+
this.runEnv = info.env;
|
|
82
|
+
this.executable = resolveExecutable(info.projectRoot, this.options.executablePath, info.env);
|
|
83
|
+
// Checked once here, so a wrong path is one error rather than every test's launch failing.
|
|
84
|
+
const problem = info.app.appPath === undefined ? undefined : appPathProblem(info.projectRoot, info.app.appPath);
|
|
85
|
+
if (problem !== undefined)
|
|
86
|
+
throw new ConfigurationError('INVALID_CONFIG', problem);
|
|
87
|
+
checkLaunchArguments(info.app.launchArguments);
|
|
88
|
+
checkCwd('electron({ cwd })', info.projectRoot, this.options.cwd, 'INVALID_CONFIG');
|
|
89
|
+
}
|
|
90
|
+
startAttempt(context) {
|
|
91
|
+
this.attempt = context;
|
|
92
|
+
this.artifactCounter = 0;
|
|
93
|
+
this.recordings = 0;
|
|
94
|
+
this.dialogs.reset();
|
|
95
|
+
this.launchExtra = NO_EXTRA;
|
|
96
|
+
// The app's data lives here for the whole attempt: `app.restart()` keeps it, `app.clearState()` empties it.
|
|
97
|
+
this.userDataDir = mkdtempSync(path.join(tmpdir(), 'e2e-electron-'));
|
|
98
|
+
return Promise.resolve();
|
|
99
|
+
}
|
|
100
|
+
async endAttempt(context) {
|
|
101
|
+
const uncollected = this.recorder;
|
|
102
|
+
this.recorder = undefined;
|
|
103
|
+
await uncollected?.abandon().catch(() => undefined);
|
|
104
|
+
await this.app.serialized(() => this.app.quit(context, DISCARD_CLOSE_MS));
|
|
105
|
+
// Retried: Chromium's helper processes can still be closing their files in it.
|
|
106
|
+
if (this.userDataDir !== undefined)
|
|
107
|
+
rmSync(this.userDataDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
108
|
+
this.userDataDir = undefined;
|
|
109
|
+
if (this.uploadsDir !== undefined)
|
|
110
|
+
rmSync(this.uploadsDir, { recursive: true, force: true });
|
|
111
|
+
this.uploadsDir = undefined;
|
|
112
|
+
this.attempt = undefined;
|
|
113
|
+
// settleAttempt reported what the attempt's dialogs left; its handlers go with it.
|
|
114
|
+
this.dialogs.reset();
|
|
115
|
+
}
|
|
116
|
+
/** A dialog the test's last step opened has no next step to fail: the attempt fails with it, as a step would. */
|
|
117
|
+
async settleAttempt(context) {
|
|
118
|
+
await answerWithin(this.dialogs.settled(), context.timeoutMs);
|
|
119
|
+
this.failOnDialog();
|
|
120
|
+
}
|
|
121
|
+
async dispose(context) {
|
|
122
|
+
await this.endAttempt(context);
|
|
123
|
+
}
|
|
124
|
+
// --- the app ---
|
|
125
|
+
/** `app.open()` and `app.restart()`: quits the app if it runs and launches it again on the attempt's app data. */
|
|
126
|
+
async restart(operation) {
|
|
127
|
+
// A dialog the app left unanswered is this step's failure, not one of the next app's.
|
|
128
|
+
this.failOnDialog();
|
|
129
|
+
// Started now: the quit, and a launch or quit ahead in the queue, spend the operation's time too.
|
|
130
|
+
const budget = new Budget(operation, 'app.open');
|
|
131
|
+
return await this.app.serialized(async () => {
|
|
132
|
+
// A call cancelled, or out of time, while it waited for its turn does nothing to the app.
|
|
133
|
+
budget.check();
|
|
134
|
+
await this.app.quit(operation, RESTART_CLOSE_MS);
|
|
135
|
+
await this.launch(operation, budget);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/** `app.clearState()`: quits the app, empties its data, and launches it again. */
|
|
139
|
+
async reset(operation) {
|
|
140
|
+
this.failOnDialog();
|
|
141
|
+
const budget = new Budget(operation, 'app.clearState');
|
|
142
|
+
return await this.app.serialized(async () => {
|
|
143
|
+
budget.check();
|
|
144
|
+
await this.app.quit(operation, DISCARD_CLOSE_MS);
|
|
145
|
+
// A quit that took the rest of the time leaves the app's data as it was.
|
|
146
|
+
budget.check();
|
|
147
|
+
if (this.userDataDir !== undefined) {
|
|
148
|
+
rmSync(this.userDataDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
149
|
+
// As private as mkdtemp made it: the app's data is no other local user's to list.
|
|
150
|
+
mkdirSync(this.userDataDir, { recursive: true, mode: 0o700 });
|
|
151
|
+
}
|
|
152
|
+
await this.launch(operation, budget);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/** `electron.userDataDir`: the attempt's own directory, there before the app is launched so a test can put data in it. */
|
|
156
|
+
dataDirectory() {
|
|
157
|
+
if (this.userDataDir === undefined)
|
|
158
|
+
throw invalidState('electron.userDataDir is read inside a test: no attempt is running');
|
|
159
|
+
return this.userDataDir;
|
|
160
|
+
}
|
|
161
|
+
/** `electron.launch()`: a fresh launch as `app.open()` does, with `extra` added to this and every later launch of the attempt. */
|
|
162
|
+
async launchWith(extra, operation) {
|
|
163
|
+
this.failOnDialog();
|
|
164
|
+
const budget = new Budget(operation, 'electron.launch');
|
|
165
|
+
return await this.app.serialized(async () => {
|
|
166
|
+
budget.check();
|
|
167
|
+
// Set in turn with the launches: one already under way starts with what it was asked for.
|
|
168
|
+
this.launchExtra = extra;
|
|
169
|
+
await this.app.quit(operation, RESTART_CLOSE_MS);
|
|
170
|
+
await this.launch(operation, budget);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/** The project root, which a test's relative paths resolve against. */
|
|
174
|
+
get root() {
|
|
175
|
+
return this.projectRoot;
|
|
176
|
+
}
|
|
177
|
+
async launch(operation, budget) {
|
|
178
|
+
const { attempt, userDataDir, executable } = this;
|
|
179
|
+
if (attempt === undefined || userDataDir === undefined)
|
|
180
|
+
throw invalidState('app.open() runs inside a test: no attempt is running');
|
|
181
|
+
if (executable === undefined)
|
|
182
|
+
throw invalidState('the electron engine was not initialized');
|
|
183
|
+
await this.app.launch({ executable, projectRoot: this.projectRoot, app: this.appInfo, env: this.runEnv, userDataDir, attempt, extra: this.launchExtra }, operation, budget);
|
|
184
|
+
await this.recorder?.snap();
|
|
185
|
+
}
|
|
186
|
+
// --- reading ---
|
|
187
|
+
projection() {
|
|
188
|
+
return { testIdAttribute: this.options.testIdAttribute, open: this.windowList.open() };
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Reads the shown window. A window can close between being picked and being
|
|
192
|
+
* read, before its close event arrives: it is discarded and the window shown
|
|
193
|
+
* in its place is read. A window whose renderer crashed answers nothing: the
|
|
194
|
+
* read waits, until `deadline`, for the app to load it again, then says it
|
|
195
|
+
* crashed. Undefined when no window is open.
|
|
196
|
+
*/
|
|
197
|
+
async readShown(deadline) {
|
|
198
|
+
for (;;) {
|
|
199
|
+
const window = this.windowList.current();
|
|
200
|
+
if (window === undefined)
|
|
201
|
+
return undefined;
|
|
202
|
+
if (this.windowList.isCrashed(window)) {
|
|
203
|
+
const { page } = window;
|
|
204
|
+
let stopListening = () => undefined;
|
|
205
|
+
const loadedOrClosed = new Promise((resolve) => {
|
|
206
|
+
const stops = [page.on('load', resolve), page.on('close', resolve)];
|
|
207
|
+
stopListening = () => stops.forEach((stop) => stop());
|
|
208
|
+
});
|
|
209
|
+
await answerWithin(loadedOrClosed, deadline.remaining());
|
|
210
|
+
stopListening();
|
|
211
|
+
// A crashed window that closed leaves the one shown in its place to read.
|
|
212
|
+
if (page.isClosed())
|
|
213
|
+
continue;
|
|
214
|
+
if (this.windowList.isCrashed(window))
|
|
215
|
+
throw crashed();
|
|
216
|
+
}
|
|
217
|
+
// A read the renderer's crash leaves pending would never settle: the crash ends it, and the next pass waits.
|
|
218
|
+
let stopWaiting = () => undefined;
|
|
219
|
+
const crash = new Promise((resolve) => {
|
|
220
|
+
stopWaiting = window.page.on('crash', () => resolve('crashed'));
|
|
221
|
+
});
|
|
222
|
+
try {
|
|
223
|
+
const raw = await Promise.race([readWindow(this.devtools, window), crash]);
|
|
224
|
+
if (raw !== 'crashed')
|
|
225
|
+
return raw;
|
|
226
|
+
}
|
|
227
|
+
catch (cause) {
|
|
228
|
+
if (!this.closedUnder(window, cause))
|
|
229
|
+
throw cause;
|
|
230
|
+
}
|
|
231
|
+
finally {
|
|
232
|
+
stopWaiting();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Whether work on a window failed because the window closed, which can
|
|
238
|
+
* happen before its close event arrives. It is forgotten here, so the
|
|
239
|
+
* caller goes on with the window shown in its place.
|
|
240
|
+
*/
|
|
241
|
+
closedUnder(window, cause) {
|
|
242
|
+
if (!window.page.isClosed() && !closedWindow(cause))
|
|
243
|
+
return false;
|
|
244
|
+
this.windowList.discard(window.page);
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
async observe(operation, options) {
|
|
248
|
+
return await withBudget(operation, 'observe', async () => {
|
|
249
|
+
// An observation (the agent's, failure evidence) never waits on a crashed window.
|
|
250
|
+
const raw = await this.readShown(new Deadline(0));
|
|
251
|
+
if (raw === undefined)
|
|
252
|
+
return { root: { ref: { id: ROOT_ID, revision: '' }, role: 'document' }, viewport: { width: 0, height: 0 } };
|
|
253
|
+
// An observation goes to a model, a report, or a file: never of a local file the app's own pages are not.
|
|
254
|
+
const left = this.leftApp(raw.window);
|
|
255
|
+
if (left !== undefined)
|
|
256
|
+
throw new ConfigurationError('POLICY_DENIED', left);
|
|
257
|
+
const snapshot = projectRead(raw, 'tree', this.projection());
|
|
258
|
+
return options?.pixels === true ? await withPixels(this.devtools, raw, snapshot) : snapshot;
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
async locate(expression, operation) {
|
|
262
|
+
// An app that is not running has nothing to find, ever: say so rather than let the locator wait out its timeout.
|
|
263
|
+
this.failOnDialog();
|
|
264
|
+
if (!this.app.running)
|
|
265
|
+
throw this.app.notRunning();
|
|
266
|
+
return await withBudget(operation, 'locate', async (budget) => {
|
|
267
|
+
// A crashed window is waited on briefly, for a reload already under way; every read waiting the whole timeout
|
|
268
|
+
// would make each step of a test whose app crashed take it.
|
|
269
|
+
for (;;) {
|
|
270
|
+
const raw = await this.readShown(new Deadline(Math.min(CRASH_RELOAD_WAIT_MS, budget.waits.remaining())));
|
|
271
|
+
if (raw === undefined)
|
|
272
|
+
return [];
|
|
273
|
+
const sink = this.attempt?.screen;
|
|
274
|
+
if (sink !== undefined && this.leftApp(raw.window) === undefined) {
|
|
275
|
+
try {
|
|
276
|
+
sink(projectRead(raw, 'tree', this.projection()));
|
|
277
|
+
}
|
|
278
|
+
catch {
|
|
279
|
+
// The trace is evidence; a sink that throws never fails the step.
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
return await locateIn(this.devtools, raw, expression, this.projection());
|
|
284
|
+
}
|
|
285
|
+
catch (cause) {
|
|
286
|
+
// The window closed between its read and the query (a popup that closes itself): the window shown in its
|
|
287
|
+
// place is the one to find the node in, as a read that met the closed window does.
|
|
288
|
+
if (!this.closedUnder(raw.window, cause))
|
|
289
|
+
throw cause;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Hands the trace the screen an action left, as `locate` hands it the one
|
|
296
|
+
* it matched against: bounded to a second, and never failing the action.
|
|
297
|
+
*/
|
|
298
|
+
async traceScreen() {
|
|
299
|
+
const sink = this.attempt?.screen;
|
|
300
|
+
if (sink === undefined)
|
|
301
|
+
return;
|
|
302
|
+
try {
|
|
303
|
+
const raw = await answerWithin(this.readShown(new Deadline(0)), TRACE_READ_MS);
|
|
304
|
+
if (raw !== undefined && this.leftApp(raw.window) === undefined)
|
|
305
|
+
sink(projectRead(raw, 'tree', this.projection()));
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
// The trace is evidence; a read that fails or a sink that throws never fails the step.
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
// --- acting ---
|
|
312
|
+
actions() {
|
|
313
|
+
return {
|
|
314
|
+
devtools: this.devtools,
|
|
315
|
+
windows: this.windowList,
|
|
316
|
+
projectRoot: this.projectRoot,
|
|
317
|
+
uploads: () => {
|
|
318
|
+
this.uploadsDir ??= mkdtempSync(path.join(tmpdir(), 'e2e-electron-uploads-'));
|
|
319
|
+
return this.uploadsDir;
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
/** Why the window is not one to observe, trace, or capture: it shows a local file outside the app's directory. */
|
|
324
|
+
leftApp(window) {
|
|
325
|
+
return outsideApp(window.page.url(), this.app.appDirectory);
|
|
326
|
+
}
|
|
327
|
+
/** Fails the step that follows a dialog nothing answered, or whose handler failed. */
|
|
328
|
+
failOnDialog() {
|
|
329
|
+
const unhandled = this.dialogs.take();
|
|
330
|
+
if (unhandled !== undefined)
|
|
331
|
+
throw unhandled;
|
|
332
|
+
}
|
|
333
|
+
requireWindow() {
|
|
334
|
+
this.failOnDialog();
|
|
335
|
+
if (!this.app.running)
|
|
336
|
+
throw this.app.notRunning();
|
|
337
|
+
return this.windowList.require();
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Every action goes through here: its input within the operation's budget,
|
|
341
|
+
* then time for the UI to settle when an agent acts (it reads the screen
|
|
342
|
+
* right after; a test's own steps verify with `expect`), then the screen
|
|
343
|
+
* it left for the trace.
|
|
344
|
+
*/
|
|
345
|
+
async act(operation, label, window, input) {
|
|
346
|
+
await withBudget(operation, label, (budget) => window.page.exclusive(() => input(budget), budget.deadline));
|
|
347
|
+
if (operation.origin === 'agent')
|
|
348
|
+
await sleep(Math.min(this.options.settleMs, operation.timeoutMs), operation.signal);
|
|
349
|
+
await this.traceScreen();
|
|
350
|
+
}
|
|
351
|
+
async perform(ref, action, operation) {
|
|
352
|
+
if (ref.id === ROOT_ID) {
|
|
353
|
+
const window = this.requireWindow();
|
|
354
|
+
if (action.kind === 'scrollIntoView')
|
|
355
|
+
return;
|
|
356
|
+
if (action.kind !== 'swipe')
|
|
357
|
+
throw notActionable(`the window takes swipe and scrollIntoView only, not ${action.kind}; act on a node`);
|
|
358
|
+
await this.act(operation, 'swipe', window, (budget) => swipeWindow(this.actions(), window, action, budget));
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
this.failOnDialog();
|
|
362
|
+
const { window, backendNodeId } = this.windowList.bind(ref);
|
|
363
|
+
await this.act(operation, action.kind, window, (budget) => {
|
|
364
|
+
// Bound again once its turn has come: the gesture before it may have loaded another document.
|
|
365
|
+
this.windowList.bind(ref);
|
|
366
|
+
return performOnNode(this.actions(), window, backendNodeId, action, budget);
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
async performAt(point, action, operation) {
|
|
370
|
+
const window = this.requireWindow();
|
|
371
|
+
await this.act(operation, action.kind, window, (budget) => performAtPoint(this.actions(), window, point, action, budget));
|
|
372
|
+
}
|
|
373
|
+
async typeText(text, options, operation) {
|
|
374
|
+
const window = this.requireWindow();
|
|
375
|
+
await this.act(operation, 'keyboard.type', window, (budget) => typeText(this.actions(), window, text, options.replace, budget));
|
|
376
|
+
}
|
|
377
|
+
async pressKey(key, operation) {
|
|
378
|
+
const window = this.requireWindow();
|
|
379
|
+
await this.act(operation, 'keyboard.press', window, (budget) => pressKey(window, key, budget));
|
|
380
|
+
}
|
|
381
|
+
// --- the fixture's keyboard and mouse ---
|
|
382
|
+
/**
|
|
383
|
+
* `electron.keyboard.*` and `electron.mouse.*`: input to the shown window,
|
|
384
|
+
* wherever its focus and pointer are, taking its turn after the gestures
|
|
385
|
+
* before it as every action does.
|
|
386
|
+
*/
|
|
387
|
+
async input(label, send, operation) {
|
|
388
|
+
const window = this.requireWindow();
|
|
389
|
+
await this.act(operation, label, window, (budget) => send(window, budget));
|
|
390
|
+
}
|
|
391
|
+
// --- windows as tabs ---
|
|
392
|
+
async switchTab(index, operation) {
|
|
393
|
+
const window = this.windowList.show(index);
|
|
394
|
+
await withBudget(operation, 'switch window', () => window.page.bringToFront());
|
|
395
|
+
}
|
|
396
|
+
async closeTab(index, operation) {
|
|
397
|
+
const window = this.windowList.closable(index);
|
|
398
|
+
await withBudget(operation, 'close window', (budget) => budget.send(() => this.app.closeWindow(window.page)));
|
|
399
|
+
}
|
|
400
|
+
// --- what the fixture asks ---
|
|
401
|
+
/** The app's open windows, in the order they opened. */
|
|
402
|
+
openWindows() {
|
|
403
|
+
return this.windowList.open();
|
|
404
|
+
}
|
|
405
|
+
/** `electron.activeWindow()`: the window `screen` reads. */
|
|
406
|
+
shownWindow() {
|
|
407
|
+
if (!this.app.running)
|
|
408
|
+
throw this.app.notRunning();
|
|
409
|
+
return this.windowList.require();
|
|
410
|
+
}
|
|
411
|
+
isActiveWindow(window) {
|
|
412
|
+
return this.windowList.isShownPage(window.page);
|
|
413
|
+
}
|
|
414
|
+
/** `window.activate()`: reads and acts in the window from now on. */
|
|
415
|
+
async activateWindow(window, operation) {
|
|
416
|
+
this.failOnDialog();
|
|
417
|
+
this.windowList.activate(window);
|
|
418
|
+
await withBudget(operation, 'activate window', () => window.page.bringToFront().catch(() => undefined));
|
|
419
|
+
}
|
|
420
|
+
/** `window.close()`: any window, the app's last one too. */
|
|
421
|
+
async closeWindow(window, operation) {
|
|
422
|
+
if (window.page.isClosed())
|
|
423
|
+
return;
|
|
424
|
+
await withBudget(operation, 'close window', (budget) => budget.send(() => this.app.closeWindow(window.page)));
|
|
425
|
+
}
|
|
426
|
+
/** `window.evaluate()`: runs test code in the window's page, with the app's own globals. */
|
|
427
|
+
async evaluateInWindow(window, fn, arg, operation) {
|
|
428
|
+
this.failOnDialog();
|
|
429
|
+
if (window.page.isClosed())
|
|
430
|
+
throw new TestError('ACTION_FAILED', 'window.evaluate: the window has closed');
|
|
431
|
+
checkJsonArgument(arg, 'window.evaluate');
|
|
432
|
+
// Its message is the test's, never read for a pattern.
|
|
433
|
+
const run = window.page.evaluateInPage(functionSource(fn, 'window.evaluate'), arg).catch((cause) => {
|
|
434
|
+
throw cause instanceof EvaluateError ? new TestError('EVALUATE_FAILED', `window.evaluate: ${cause.message}`, { cause }) : cause;
|
|
435
|
+
});
|
|
436
|
+
// A cancelled step never awaits it.
|
|
437
|
+
run.catch(() => undefined);
|
|
438
|
+
// Not input: a timeout is the function's, and nothing the engine sent may have committed.
|
|
439
|
+
return (await withBudget(operation, 'window.evaluate', () => run));
|
|
440
|
+
}
|
|
441
|
+
/** `window.reload()`: the window's document loaded again. */
|
|
442
|
+
async reloadWindow(window, operation) {
|
|
443
|
+
this.failOnDialog();
|
|
444
|
+
if (window.page.isClosed())
|
|
445
|
+
throw new TestError('ACTION_FAILED', 'window.reload: the window has closed');
|
|
446
|
+
await withBudget(operation, 'window.reload', (budget) => budget.send(() => window.page.reload()));
|
|
447
|
+
}
|
|
448
|
+
/** `electron.window()`: the first open window `which` matches, waited for until the operation's deadline. */
|
|
449
|
+
async findWindow(which, operation) {
|
|
450
|
+
this.failOnDialog();
|
|
451
|
+
if (!this.app.running)
|
|
452
|
+
throw this.app.notRunning();
|
|
453
|
+
return await this.windowList.find(which, (page) => this.app.windowTitle(page), operation);
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* `electron.waitForWindow()`: runs `trigger`, waits for a window that was
|
|
457
|
+
* not open before it, and makes that window the active one. The trigger is
|
|
458
|
+
* test code: its error is reported as it is.
|
|
459
|
+
*/
|
|
460
|
+
async waitForWindow(trigger, operation) {
|
|
461
|
+
this.failOnDialog();
|
|
462
|
+
if (!this.app.running)
|
|
463
|
+
throw this.app.notRunning();
|
|
464
|
+
const before = new Set(this.windowList.open());
|
|
465
|
+
const started = Date.now();
|
|
466
|
+
await trigger();
|
|
467
|
+
const triggerMs = Date.now() - started;
|
|
468
|
+
let opened;
|
|
469
|
+
await pollCondition({
|
|
470
|
+
deadline: new Deadline(Math.max(1, operation.timeoutMs - triggerMs)),
|
|
471
|
+
signal: operation.signal,
|
|
472
|
+
negated: false,
|
|
473
|
+
evaluate: () => {
|
|
474
|
+
opened = this.windowList.open().find((window) => !before.has(window));
|
|
475
|
+
return Promise.resolve(opened !== undefined);
|
|
476
|
+
},
|
|
477
|
+
onTimeout: () => new TestError('ACTION_FAILED', `no window opened within ${operation.timeoutMs}ms; the trigger resolved after ${triggerMs}ms`),
|
|
478
|
+
});
|
|
479
|
+
if (opened === undefined)
|
|
480
|
+
throw invalidState('the window was lost');
|
|
481
|
+
await this.activateWindow(opened, operation);
|
|
482
|
+
return opened;
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* `window.title()`: the document's title, else the OS window's for a
|
|
486
|
+
* document with none, or one that does not answer as a crashed renderer's
|
|
487
|
+
* does not. Either is a title `electron.window({ title })` matches.
|
|
488
|
+
*/
|
|
489
|
+
async windowTitle(window, operation) {
|
|
490
|
+
const document = await answerWithin(window.page.title(), Math.min(TITLE_MS, operation.timeoutMs));
|
|
491
|
+
if (document !== undefined && document !== '')
|
|
492
|
+
return document;
|
|
493
|
+
return (await this.app.windowTitle(window.page)) ?? '';
|
|
494
|
+
}
|
|
495
|
+
/** `electron.onDialog()`: answers the app's dialogs from now on, until what it returns is called. */
|
|
496
|
+
onDialog(handler) {
|
|
497
|
+
return this.dialogs.add(handler);
|
|
498
|
+
}
|
|
499
|
+
/** Runs a function in the app's main process. */
|
|
500
|
+
async evaluate(fn, arg, operation) {
|
|
501
|
+
this.failOnDialog();
|
|
502
|
+
// Started outside the bound, so a failure of the function's own is reported as it is.
|
|
503
|
+
const run = this.app.evaluate(fn, arg);
|
|
504
|
+
// A cancelled step never awaits it.
|
|
505
|
+
run.catch(() => undefined);
|
|
506
|
+
// Not input: a timeout is the function's, and nothing the engine sent may have committed.
|
|
507
|
+
return await withBudget(operation, 'electron.evaluate', () => run);
|
|
508
|
+
}
|
|
509
|
+
// --- reads of one node ---
|
|
510
|
+
async readStyle(ref, properties, operation) {
|
|
511
|
+
const requested = validateComputedStyleProperties(properties);
|
|
512
|
+
const { window, backendNodeId } = this.windowList.bind(ref);
|
|
513
|
+
return await withBudget(operation, 'read computed style', () => this.devtools.callOn(window.page, backendNodeId, 'readStyle', requested));
|
|
514
|
+
}
|
|
515
|
+
async viewportIntersection(ref, operation) {
|
|
516
|
+
const { window, backendNodeId } = this.windowList.bind(ref);
|
|
517
|
+
return await withBudget(operation, 'measure viewport intersection', () => this.devtools.callOn(window.page, backendNodeId, 'viewportIntersection'));
|
|
518
|
+
}
|
|
519
|
+
// --- history ---
|
|
520
|
+
/** `app.back()`: the shown window's previous history entry, when it has one. */
|
|
521
|
+
async back(operation) {
|
|
522
|
+
const window = this.requireWindow();
|
|
523
|
+
await this.act(operation, 'back', window, async (budget) => {
|
|
524
|
+
const { session } = window.page;
|
|
525
|
+
const { currentIndex, entries } = await session.send('Page.getNavigationHistory');
|
|
526
|
+
const previous = entries[currentIndex - 1];
|
|
527
|
+
if (previous === undefined)
|
|
528
|
+
throw notActionable('the window has no page to go back to');
|
|
529
|
+
await budget.send(() => session.send('Page.navigateToHistoryEntry', { entryId: previous.id }));
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
// --- downloads ---
|
|
533
|
+
/**
|
|
534
|
+
* Runs `trigger` and waits for the next download the app starts to finish,
|
|
535
|
+
* then copies the file into the attempt's artifacts. The trigger is test
|
|
536
|
+
* code: its error is reported as it is.
|
|
537
|
+
*/
|
|
538
|
+
async waitForDownload(trigger, operation) {
|
|
539
|
+
const attempt = this.attempt;
|
|
540
|
+
if (attempt === undefined)
|
|
541
|
+
throw invalidState('a download is waited for inside a test');
|
|
542
|
+
this.failOnDialog();
|
|
543
|
+
const before = (await this.app.downloads()).length;
|
|
544
|
+
await trigger();
|
|
545
|
+
let finished;
|
|
546
|
+
await pollCondition({
|
|
547
|
+
deadline: new Deadline(operation.timeoutMs),
|
|
548
|
+
signal: operation.signal,
|
|
549
|
+
negated: false,
|
|
550
|
+
evaluate: async () => {
|
|
551
|
+
const next = (await this.app.downloads())[before];
|
|
552
|
+
if (next === undefined || next.state === 'progressing')
|
|
553
|
+
return false;
|
|
554
|
+
finished = next;
|
|
555
|
+
return true;
|
|
556
|
+
},
|
|
557
|
+
onTimeout: () => new TestError('ACTION_FAILED', `no download finished within ${operation.timeoutMs}ms of the trigger`),
|
|
558
|
+
});
|
|
559
|
+
if (finished === undefined)
|
|
560
|
+
throw invalidState('the download was lost');
|
|
561
|
+
if (finished.state !== 'completed')
|
|
562
|
+
throw new EngineError('ENGINE_FAILURE', `the download of ${finished.filename} was ${finished.state}`, { retryable: false });
|
|
563
|
+
this.artifactCounter += 1;
|
|
564
|
+
const relative = path.posix.join('downloads', `${String(this.artifactCounter).padStart(3, '0')}-${path.basename(finished.filename).replace(/[^A-Za-z0-9._-]+/g, '-') || 'download'}`);
|
|
565
|
+
const absolute = path.join(attempt.artifactsDir, relative);
|
|
566
|
+
mkdirSync(path.dirname(absolute), { recursive: true });
|
|
567
|
+
copyFileSync(finished.path, absolute);
|
|
568
|
+
return { path: relative, absolutePath: absolute, suggestedFilename: finished.filename };
|
|
569
|
+
}
|
|
570
|
+
// --- pixels ---
|
|
571
|
+
async screenshot(label, operation) {
|
|
572
|
+
const window = this.requireWindow();
|
|
573
|
+
const attempt = this.attempt;
|
|
574
|
+
if (attempt === undefined)
|
|
575
|
+
throw invalidState('a screenshot is taken inside a test');
|
|
576
|
+
return await withBudget(operation, 'screenshot', async () => {
|
|
577
|
+
this.artifactCounter += 1;
|
|
578
|
+
return await screenshotFile(this.devtools, window, attempt.artifactsDir, screenshotName(this.artifactCounter, label), () => this.leftApp(window));
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
// --- video ---
|
|
582
|
+
/** Starts a recording: of the shown window now, and of each one shown after it, through a relaunch too. */
|
|
583
|
+
async startVideo(operation) {
|
|
584
|
+
const attempt = this.attempt;
|
|
585
|
+
if (attempt === undefined)
|
|
586
|
+
throw invalidState('a video is recorded inside a test');
|
|
587
|
+
if (this.recorder !== undefined)
|
|
588
|
+
throw invalidState('a video is already recording');
|
|
589
|
+
const ffmpeg = await withBudget(operation, 'video start', () => ensureFfmpeg(this.runEnv, operation.signal));
|
|
590
|
+
this.recordings += 1;
|
|
591
|
+
const name = this.recordings === 1 ? 'video' : `video-part${this.recordings}`;
|
|
592
|
+
// Never pixels of a local file outside the app's directory, as no screenshot is.
|
|
593
|
+
const recorder = new Recorder(ffmpeg, attempt.artifactsDir, path.posix.join('video', `${name}.webm`), (page) => outsideApp(page.url(), this.app.appDirectory) === undefined);
|
|
594
|
+
this.recorder = recorder;
|
|
595
|
+
recorder.follow(this.windowList.current()?.page);
|
|
596
|
+
}
|
|
597
|
+
async stopVideo() {
|
|
598
|
+
const recorder = this.recorder;
|
|
599
|
+
this.recorder = undefined;
|
|
600
|
+
return (await recorder?.stop()) ?? [];
|
|
601
|
+
}
|
|
602
|
+
}
|
package/dist/tree.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Projects one window's Chromium accessibility tree onto the engine
|
|
3
|
+
* contract's semantic tree. The roles, names, values, and states are what
|
|
4
|
+
* Chromium computed (`Accessibility.getFullAXTree`); the boxes, attributes,
|
|
5
|
+
* and input values come from one DOM snapshot of the same document
|
|
6
|
+
* (`snapshot.ts`), joined on each node's backend DOM node id. `fields.ts`
|
|
7
|
+
* reads one element; this walks the tree and folds its text.
|
|
8
|
+
* Nothing runs in the page, so an app's content security policy, its frozen
|
|
9
|
+
* globals, and its mutation observers never see a read.
|
|
10
|
+
*
|
|
11
|
+
* Pure: the surface reads the two documents, this turns them into nodes.
|
|
12
|
+
*/
|
|
13
|
+
import { type SemanticNode } from '@e2e-dev/core/engine';
|
|
14
|
+
import { type AXNode } from './fields.ts';
|
|
15
|
+
import { type DomSnapshot } from './snapshot.ts';
|
|
16
|
+
/** How the window's layout maps the snapshot's device pixels to the viewport's CSS pixels. */
|
|
17
|
+
export interface WindowGeometry {
|
|
18
|
+
/** Device pixels per CSS pixel. */
|
|
19
|
+
readonly scale: number;
|
|
20
|
+
/** The visual viewport's scroll offset, in CSS pixels. */
|
|
21
|
+
readonly scrollX: number;
|
|
22
|
+
readonly scrollY: number;
|
|
23
|
+
/** The viewport's size, in CSS pixels. */
|
|
24
|
+
readonly width: number;
|
|
25
|
+
readonly height: number;
|
|
26
|
+
}
|
|
27
|
+
/** `tree` is the observation for a model; `node` is what a locator reads. `POLICIES` says how they differ. */
|
|
28
|
+
export type ProjectionMode = 'tree' | 'node';
|
|
29
|
+
export interface ProjectionOptions {
|
|
30
|
+
readonly mode: ProjectionMode;
|
|
31
|
+
readonly testIdAttribute: string;
|
|
32
|
+
/** Mints a node id from a backend DOM node id; ids carry the window, so a ref never binds in another one. */
|
|
33
|
+
readonly idOf: (backendNodeId: number) => string;
|
|
34
|
+
/** The root's id, stable for the surface. */
|
|
35
|
+
readonly rootId: string;
|
|
36
|
+
/** The document's URL, which a relative link resolves against. */
|
|
37
|
+
readonly baseUrl?: string;
|
|
38
|
+
/** The document's title, the root's name. */
|
|
39
|
+
readonly title?: string;
|
|
40
|
+
/** Backend ids of elements a locator's CSS selector matched: kept as nodes even where the tree would leave them out. */
|
|
41
|
+
readonly keep?: ReadonlySet<number>;
|
|
42
|
+
/** Overrides the mode's node cap; past it nodes are left out and the tree marked truncated. */
|
|
43
|
+
readonly maxNodes?: number;
|
|
44
|
+
}
|
|
45
|
+
export interface Projection {
|
|
46
|
+
readonly root: SemanticNode;
|
|
47
|
+
/** Nodes were left out past the node cap. A nested document is never entered; its frame is a node of its own. */
|
|
48
|
+
readonly truncated: boolean;
|
|
49
|
+
}
|
|
50
|
+
/** Projects the window's accessibility tree onto the contract's semantic tree. */
|
|
51
|
+
export declare function projectTree(axNodes: readonly AXNode[], snapshot: DomSnapshot, geometry: WindowGeometry, options: ProjectionOptions): Projection;
|