@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/video.js
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A recording of the app: the DevTools screencast of whichever window is
|
|
3
|
+
* shown, its JPEG frames encoded to one VP9 webm by ffmpeg. The file outlives
|
|
4
|
+
* a window: switching windows, or relaunching the app, moves the screencast
|
|
5
|
+
* to the window shown next, and the gap holds the last frame.
|
|
6
|
+
*/
|
|
7
|
+
import { spawn } from 'node:child_process';
|
|
8
|
+
import { existsSync, mkdirSync, rmSync } from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { EngineError } from '@e2e-dev/core/engine';
|
|
11
|
+
import { answerWithin } from './budget.js';
|
|
12
|
+
/** Frames a second of the encoded video. */
|
|
13
|
+
const FPS = 25;
|
|
14
|
+
const JPEG_QUALITY = 90;
|
|
15
|
+
/** How long the last frame holds past the end: a viewer needs a moment on the final screen. */
|
|
16
|
+
const FRAME_GRACE_MS = 1_000;
|
|
17
|
+
/** How long ffmpeg gets to take the queued frames and write the file before it is killed. */
|
|
18
|
+
const CLOSE_TIMEOUT_MS = 10_000;
|
|
19
|
+
/** How much of ffmpeg's error output a failed encode reports. */
|
|
20
|
+
const STDERR_TAIL = 2_000;
|
|
21
|
+
/** How long a window gets to hand over its last screen when the recording stops. */
|
|
22
|
+
const LAST_SCREEN_MS = 2_000;
|
|
23
|
+
export class Recorder {
|
|
24
|
+
ffmpeg;
|
|
25
|
+
relative;
|
|
26
|
+
allowed;
|
|
27
|
+
encoder;
|
|
28
|
+
startedAt;
|
|
29
|
+
source;
|
|
30
|
+
/** Source switches run one at a time, in the order the windows were shown. */
|
|
31
|
+
switching = Promise.resolve();
|
|
32
|
+
stopped = false;
|
|
33
|
+
absolute;
|
|
34
|
+
/**
|
|
35
|
+
* `relative` is the file under the attempt's artifacts. `allowed` says
|
|
36
|
+
* whether a window's pixels may be kept: frames of one that may not are
|
|
37
|
+
* dropped, and the video holds the frame before.
|
|
38
|
+
*/
|
|
39
|
+
constructor(ffmpeg, artifactsDir, relative, allowed) {
|
|
40
|
+
this.ffmpeg = ffmpeg;
|
|
41
|
+
this.relative = relative;
|
|
42
|
+
this.allowed = allowed;
|
|
43
|
+
this.absolute = path.join(artifactsDir, relative);
|
|
44
|
+
}
|
|
45
|
+
/** Records `page` from now on, or nothing when the app shows no window. */
|
|
46
|
+
follow(page) {
|
|
47
|
+
this.switching = this.switching.then(() => this.switchTo(page)).catch(() => undefined);
|
|
48
|
+
}
|
|
49
|
+
async switchTo(page) {
|
|
50
|
+
if (this.stopped || this.source?.page === page)
|
|
51
|
+
return;
|
|
52
|
+
await this.source?.stop();
|
|
53
|
+
this.source = undefined;
|
|
54
|
+
if (page === undefined || page.isClosed())
|
|
55
|
+
return;
|
|
56
|
+
const { session } = page;
|
|
57
|
+
if (this.encoder === undefined) {
|
|
58
|
+
const { cssVisualViewport } = await session.send('Page.getLayoutMetrics');
|
|
59
|
+
// Even sides: the encoder's pixel format halves them.
|
|
60
|
+
const size = { width: Math.max(2, Math.floor(cssVisualViewport.clientWidth / 2) * 2), height: Math.max(2, Math.floor(cssVisualViewport.clientHeight / 2) * 2) };
|
|
61
|
+
mkdirSync(path.dirname(this.absolute), { recursive: true });
|
|
62
|
+
this.startedAt = new Date().toISOString();
|
|
63
|
+
this.encoder = new FrameEncoder(this.ffmpeg, size, this.absolute);
|
|
64
|
+
}
|
|
65
|
+
const encoder = this.encoder;
|
|
66
|
+
const off = session.on('Page.screencastFrame', (frame) => {
|
|
67
|
+
// Acknowledged once the encoder took it, so the window sends no faster than ffmpeg encodes.
|
|
68
|
+
const taken = this.allowed(page) ? encoder.frame(Buffer.from(frame.data, 'base64')) : Promise.resolve();
|
|
69
|
+
void taken.then(() => session.send('Page.screencastFrameAck', { sessionId: frame.sessionId })).catch(() => undefined);
|
|
70
|
+
});
|
|
71
|
+
page.recording = true;
|
|
72
|
+
this.source = {
|
|
73
|
+
page,
|
|
74
|
+
stop: async () => {
|
|
75
|
+
off();
|
|
76
|
+
page.recording = false;
|
|
77
|
+
await session.send('Page.stopScreencast').catch(() => undefined);
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
try {
|
|
81
|
+
await session.send('Page.startScreencast', { format: 'jpeg', quality: JPEG_QUALITY, maxWidth: encoder.size.width, maxHeight: encoder.size.height });
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// A window that closed as it was shown: the next one shown is recorded.
|
|
85
|
+
off();
|
|
86
|
+
page.recording = false;
|
|
87
|
+
this.source = undefined;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
// A window shown again repaints nothing: the video would hold the window before it.
|
|
91
|
+
await this.snap();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Ends the recording and returns its file, or nothing when no window was
|
|
95
|
+
* ever shown.
|
|
96
|
+
*/
|
|
97
|
+
async stop() {
|
|
98
|
+
await this.switching;
|
|
99
|
+
this.stopped = true;
|
|
100
|
+
const { source, encoder, startedAt } = this;
|
|
101
|
+
if (encoder === undefined || startedAt === undefined)
|
|
102
|
+
return [];
|
|
103
|
+
try {
|
|
104
|
+
await this.snap();
|
|
105
|
+
await source?.stop();
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
this.source = undefined;
|
|
109
|
+
await encoder.close().catch((cause) => {
|
|
110
|
+
if (!existsSync(this.absolute)) {
|
|
111
|
+
throw new EngineError('ENGINE_FAILURE', `the video ${this.relative} could not be finalized: ${cause instanceof Error ? cause.message : String(cause)}`, { retryable: false, cause });
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return existsSync(this.absolute) ? [{ path: this.relative, startedAt }] : [];
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Adds the shown window as it stands to the video. A screencast sends a
|
|
119
|
+
* frame only when the window repaints, so a still window is captured here:
|
|
120
|
+
* once the app has launched, and when the recording ends.
|
|
121
|
+
*/
|
|
122
|
+
async snap() {
|
|
123
|
+
const { source, encoder } = this;
|
|
124
|
+
if (source === undefined || encoder === undefined || source.page.isClosed() || !this.allowed(source.page))
|
|
125
|
+
return;
|
|
126
|
+
const screen = await answerWithin(source.page.session.send('Page.captureScreenshot', { format: 'jpeg', quality: JPEG_QUALITY }), LAST_SCREEN_MS);
|
|
127
|
+
if (screen !== undefined)
|
|
128
|
+
await encoder.frame(Buffer.from(screen.data, 'base64'));
|
|
129
|
+
}
|
|
130
|
+
/** Ends a recording nobody collects: the file it wrote is removed. */
|
|
131
|
+
async abandon() {
|
|
132
|
+
await this.switching;
|
|
133
|
+
this.stopped = true;
|
|
134
|
+
await this.source?.stop();
|
|
135
|
+
this.source = undefined;
|
|
136
|
+
await this.encoder?.abort();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Encodes screencast frames to a constant-rate VP9 webm with ffmpeg. The
|
|
141
|
+
* output is a timeline of `FPS` ticks from the encoder's start: a frame
|
|
142
|
+
* takes the tick it arrives in, a tick a frame already took leaves the newer
|
|
143
|
+
* one for the next, and a tick with no new frame repeats the last. A frame
|
|
144
|
+
* of another size (a second window, a dense display) is scaled down to fit
|
|
145
|
+
* and padded. Writes are serialized through ffmpeg's drain cycle.
|
|
146
|
+
*/
|
|
147
|
+
class FrameEncoder {
|
|
148
|
+
size;
|
|
149
|
+
output;
|
|
150
|
+
process;
|
|
151
|
+
last;
|
|
152
|
+
written = 0;
|
|
153
|
+
startedAt = performance.now();
|
|
154
|
+
writing = Promise.resolve();
|
|
155
|
+
stderr = '';
|
|
156
|
+
failure;
|
|
157
|
+
exited;
|
|
158
|
+
constructor(ffmpeg, size, output) {
|
|
159
|
+
this.size = size;
|
|
160
|
+
this.output = output;
|
|
161
|
+
const { width, height } = size;
|
|
162
|
+
const args = [
|
|
163
|
+
'-loglevel', 'error',
|
|
164
|
+
'-f', 'image2pipe', '-framerate', String(FPS), '-c:v', 'mjpeg', '-i', 'pipe:0',
|
|
165
|
+
'-y', '-an',
|
|
166
|
+
'-c:v', 'libvpx-vp9', '-qmin', '0', '-qmax', '50', '-crf', '8', '-deadline', 'realtime', '-cpu-used', '8',
|
|
167
|
+
'-b:v', '1M', '-row-mt', '1',
|
|
168
|
+
'-vf', `scale=w='min(iw,${width})':h='min(ih,${height})':force_original_aspect_ratio=decrease,pad=${width}:${height}:0:0:gray,format=yuv420p`,
|
|
169
|
+
output,
|
|
170
|
+
];
|
|
171
|
+
this.process = spawn(ffmpeg, args, { stdio: ['pipe', 'ignore', 'pipe'] });
|
|
172
|
+
this.process.stderr?.on('data', (chunk) => {
|
|
173
|
+
this.stderr = (this.stderr + chunk.toString()).slice(-STDERR_TAIL);
|
|
174
|
+
});
|
|
175
|
+
this.process.on('error', (cause) => {
|
|
176
|
+
this.failure ??= cause;
|
|
177
|
+
});
|
|
178
|
+
this.process.stdin?.on('error', (cause) => {
|
|
179
|
+
this.failure ??= cause;
|
|
180
|
+
});
|
|
181
|
+
this.exited = new Promise((resolve) => {
|
|
182
|
+
this.process.once('error', () => resolve({ code: null, signal: null }));
|
|
183
|
+
this.process.once('close', (code, signal) => resolve({ code, signal }));
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/** Takes the next frame; resolves once the encoder took every frame up to it. */
|
|
187
|
+
frame(jpeg) {
|
|
188
|
+
const due = this.due(0);
|
|
189
|
+
this.fill(due);
|
|
190
|
+
if (this.written === due)
|
|
191
|
+
this.write(jpeg);
|
|
192
|
+
this.last = jpeg;
|
|
193
|
+
return this.writing;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Holds the last frame until now and a moment past it, then closes the
|
|
197
|
+
* input and resolves once ffmpeg wrote the file. A recording that never
|
|
198
|
+
* got a frame writes none.
|
|
199
|
+
*/
|
|
200
|
+
async close() {
|
|
201
|
+
if (this.last === undefined) {
|
|
202
|
+
await this.abort();
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const timer = setTimeout(() => this.process.kill('SIGKILL'), CLOSE_TIMEOUT_MS);
|
|
206
|
+
this.fill(this.due(FRAME_GRACE_MS));
|
|
207
|
+
await Promise.race([this.writing, this.exited]);
|
|
208
|
+
this.process.stdin?.end();
|
|
209
|
+
const { code, signal } = await this.exited.finally(() => clearTimeout(timer));
|
|
210
|
+
if (this.failure !== undefined)
|
|
211
|
+
throw new Error(`ffmpeg failed: ${this.failure.message}`);
|
|
212
|
+
if (code !== 0)
|
|
213
|
+
throw new Error(`ffmpeg ${signal === null ? `exited with ${String(code)}` : `was killed (${signal})`}${this.stderr === '' ? '' : `: ${this.stderr.trim()}`}`);
|
|
214
|
+
}
|
|
215
|
+
/** Kills the encoder and removes what it wrote. */
|
|
216
|
+
async abort() {
|
|
217
|
+
this.process.kill('SIGKILL');
|
|
218
|
+
await this.exited;
|
|
219
|
+
rmSync(this.output, { force: true });
|
|
220
|
+
}
|
|
221
|
+
/** The tick the timeline is at, `aheadMs` from now. */
|
|
222
|
+
due(aheadMs) {
|
|
223
|
+
return Math.floor(((performance.now() + aheadMs - this.startedAt) / 1000) * FPS);
|
|
224
|
+
}
|
|
225
|
+
/** Repeats the last frame for every tick before `due` with none. */
|
|
226
|
+
fill(due) {
|
|
227
|
+
if (this.last === undefined)
|
|
228
|
+
return;
|
|
229
|
+
while (this.written < due)
|
|
230
|
+
this.write(this.last);
|
|
231
|
+
}
|
|
232
|
+
write(jpeg) {
|
|
233
|
+
this.written += 1;
|
|
234
|
+
this.writing = this.writing.then(() => this.push(jpeg));
|
|
235
|
+
}
|
|
236
|
+
/** Hands one frame to ffmpeg, waiting for its input to drain when full. */
|
|
237
|
+
push(jpeg) {
|
|
238
|
+
const stdin = this.process.stdin;
|
|
239
|
+
if (stdin === null || !stdin.writable || this.failure !== undefined)
|
|
240
|
+
return undefined;
|
|
241
|
+
if (stdin.write(jpeg))
|
|
242
|
+
return undefined;
|
|
243
|
+
return new Promise((resolve) => {
|
|
244
|
+
const done = () => {
|
|
245
|
+
stdin.off('drain', done);
|
|
246
|
+
stdin.off('close', done);
|
|
247
|
+
resolve();
|
|
248
|
+
};
|
|
249
|
+
stdin.once('drain', done);
|
|
250
|
+
stdin.once('close', done);
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The running app's windows: the registry of the pages it opened, which one
|
|
3
|
+
* `screen` and the agent read, and the node refs that name a node in one.
|
|
4
|
+
*/
|
|
5
|
+
import { type NodeRef, type OperationContext } from '@e2e-dev/core/engine';
|
|
6
|
+
import type { WindowMatcher } from './fixtures.ts';
|
|
7
|
+
import type { PageTarget } from './page.ts';
|
|
8
|
+
/** The observation root's id: one surface, one root, whichever window it reads. */
|
|
9
|
+
export declare const ROOT_ID = "window";
|
|
10
|
+
/** One open window of the app: its page and the serial its node ids carry. */
|
|
11
|
+
export interface AppWindow {
|
|
12
|
+
readonly page: PageTarget;
|
|
13
|
+
readonly serial: number;
|
|
14
|
+
}
|
|
15
|
+
/** Reads the OS window's title of a window's page; undefined for a view with no window of its own. */
|
|
16
|
+
export type WindowTitle = (page: PageTarget) => Promise<string | undefined>;
|
|
17
|
+
/**
|
|
18
|
+
* The node id a window's node carries: the window's serial, which of its
|
|
19
|
+
* documents the node was read in, and the node's backend id. `generation` is
|
|
20
|
+
* the page's when the read began, not when the id is made: a navigation may
|
|
21
|
+
* have landed in between.
|
|
22
|
+
*/
|
|
23
|
+
export declare function nodeId(window: AppWindow, generation: number, backendNodeId: number): string;
|
|
24
|
+
export declare class Windows {
|
|
25
|
+
private readonly byPage;
|
|
26
|
+
private shownWindow;
|
|
27
|
+
/** Told each time another window, or none, becomes the shown one. */
|
|
28
|
+
onShown: ((window: AppWindow | undefined) => void) | undefined;
|
|
29
|
+
/** The windows shown before the current one, oldest first: closing the shown window goes back to the latest still open, unless the window that opened it is. */
|
|
30
|
+
private history;
|
|
31
|
+
private nextSerial;
|
|
32
|
+
private readonly crashed;
|
|
33
|
+
private get shown();
|
|
34
|
+
private set shown(value);
|
|
35
|
+
/** Registers a page the app opened, once; undefined when it is already known or closed. Its node ids get a serial of their own. */
|
|
36
|
+
add(page: PageTarget): AppWindow | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Forgets a window that closed. A read can fail on a closed window before
|
|
39
|
+
* its close event arrives; it discards the window itself and reads again.
|
|
40
|
+
*/
|
|
41
|
+
discard(page: PageTarget): void;
|
|
42
|
+
/** Marks a window whose renderer crashed, until it loads a page again. */
|
|
43
|
+
markCrashed(page: PageTarget): void;
|
|
44
|
+
markLoaded(page: PageTarget): void;
|
|
45
|
+
isCrashed(window: AppWindow): boolean;
|
|
46
|
+
/** Forgets every window: the app quit. */
|
|
47
|
+
clear(): void;
|
|
48
|
+
/** Makes `window` the shown one, remembering the one before it. */
|
|
49
|
+
private showWindow;
|
|
50
|
+
/** The window to show once `closed` is gone: the one that opened it, else the one shown before it that is still open, else the newest. */
|
|
51
|
+
private fallback;
|
|
52
|
+
/** Whether a page is registered as a window. */
|
|
53
|
+
has(page: PageTarget): boolean;
|
|
54
|
+
/** Whether a page is the window `screen` and the agent read. */
|
|
55
|
+
isShownPage(page: PageTarget): boolean;
|
|
56
|
+
/** The open windows, in the order they opened. A `WebContentsView` is a page of its own and counts as a window. */
|
|
57
|
+
open(): AppWindow[];
|
|
58
|
+
/**
|
|
59
|
+
* The window `screen` and the agent read: the one chosen, else the first the
|
|
60
|
+
* app opened. Once that one closes, the window that opened it, else the
|
|
61
|
+
* one shown before it that is still open, else the newest.
|
|
62
|
+
*/
|
|
63
|
+
current(): AppWindow | undefined;
|
|
64
|
+
/** The current window, or why there is none. */
|
|
65
|
+
require(): AppWindow;
|
|
66
|
+
/** The window and backend node a ref names, or a stale error when it is gone or from another window. */
|
|
67
|
+
bind(ref: NodeRef): {
|
|
68
|
+
readonly window: AppWindow;
|
|
69
|
+
readonly backendNodeId: number;
|
|
70
|
+
};
|
|
71
|
+
/** Shows the open window at `index`. */
|
|
72
|
+
show(index: number): AppWindow;
|
|
73
|
+
/** The open window at `index`, which closing leaves another open. */
|
|
74
|
+
closable(index: number): AppWindow;
|
|
75
|
+
/** Makes an open window the shown one. */
|
|
76
|
+
activate(window: AppWindow): void;
|
|
77
|
+
/** The open windows with what a matcher reads of each, so a window that closes while the titles are read cannot shift a match onto another. */
|
|
78
|
+
private describe;
|
|
79
|
+
/** The first open window `which` matches, waiting for it to open until a little before the operation's deadline. It is not shown: the caller activates it. */
|
|
80
|
+
find(which: WindowMatcher, windowTitle: WindowTitle, operation: OperationContext): Promise<AppWindow>;
|
|
81
|
+
}
|
package/dist/windows.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The running app's windows: the registry of the pages it opened, which one
|
|
3
|
+
* `screen` and the agent read, and the node refs that name a node in one.
|
|
4
|
+
*/
|
|
5
|
+
import { pollCondition, TestError } from '@e2e-dev/core/engine';
|
|
6
|
+
import { invalidState, notActionable, stale } from './errors.js';
|
|
7
|
+
import { answerWithin, Budget } from './budget.js';
|
|
8
|
+
/** How long a window gets to say its document's title. */
|
|
9
|
+
const TITLE_MS = 1_000;
|
|
10
|
+
/** The observation root's id: one surface, one root, whichever window it reads. */
|
|
11
|
+
export const ROOT_ID = 'window';
|
|
12
|
+
/**
|
|
13
|
+
* The node id a window's node carries: the window's serial, which of its
|
|
14
|
+
* documents the node was read in, and the node's backend id. `generation` is
|
|
15
|
+
* the page's when the read began, not when the id is made: a navigation may
|
|
16
|
+
* have landed in between.
|
|
17
|
+
*/
|
|
18
|
+
export function nodeId(window, generation, backendNodeId) {
|
|
19
|
+
return `${window.serial}.${generation}.${backendNodeId}`;
|
|
20
|
+
}
|
|
21
|
+
export class Windows {
|
|
22
|
+
byPage = new Map();
|
|
23
|
+
shownWindow;
|
|
24
|
+
/** Told each time another window, or none, becomes the shown one. */
|
|
25
|
+
onShown;
|
|
26
|
+
/** The windows shown before the current one, oldest first: closing the shown window goes back to the latest still open, unless the window that opened it is. */
|
|
27
|
+
history = [];
|
|
28
|
+
nextSerial = 1;
|
|
29
|
+
crashed = new Set();
|
|
30
|
+
get shown() {
|
|
31
|
+
return this.shownWindow;
|
|
32
|
+
}
|
|
33
|
+
set shown(window) {
|
|
34
|
+
if (window === this.shownWindow)
|
|
35
|
+
return;
|
|
36
|
+
this.shownWindow = window;
|
|
37
|
+
this.onShown?.(window);
|
|
38
|
+
}
|
|
39
|
+
/** Registers a page the app opened, once; undefined when it is already known or closed. Its node ids get a serial of their own. */
|
|
40
|
+
add(page) {
|
|
41
|
+
if (this.byPage.has(page) || page.isClosed())
|
|
42
|
+
return undefined;
|
|
43
|
+
const window = { page, serial: this.nextSerial++ };
|
|
44
|
+
this.byPage.set(page, window);
|
|
45
|
+
this.shown ??= window;
|
|
46
|
+
page.on('close', () => this.discard(page));
|
|
47
|
+
return window;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Forgets a window that closed. A read can fail on a closed window before
|
|
51
|
+
* its close event arrives; it discards the window itself and reads again.
|
|
52
|
+
*/
|
|
53
|
+
discard(page) {
|
|
54
|
+
const window = this.byPage.get(page);
|
|
55
|
+
this.byPage.delete(page);
|
|
56
|
+
if (window !== undefined && this.shown === window)
|
|
57
|
+
this.shown = this.fallback(window);
|
|
58
|
+
}
|
|
59
|
+
/** Marks a window whose renderer crashed, until it loads a page again. */
|
|
60
|
+
markCrashed(page) {
|
|
61
|
+
this.crashed.add(page);
|
|
62
|
+
}
|
|
63
|
+
markLoaded(page) {
|
|
64
|
+
this.crashed.delete(page);
|
|
65
|
+
}
|
|
66
|
+
isCrashed(window) {
|
|
67
|
+
return this.crashed.has(window.page);
|
|
68
|
+
}
|
|
69
|
+
/** Forgets every window: the app quit. */
|
|
70
|
+
clear() {
|
|
71
|
+
this.crashed.clear();
|
|
72
|
+
this.byPage.clear();
|
|
73
|
+
this.shown = undefined;
|
|
74
|
+
this.history = [];
|
|
75
|
+
}
|
|
76
|
+
/** Makes `window` the shown one, remembering the one before it. */
|
|
77
|
+
showWindow(window) {
|
|
78
|
+
if (this.shown !== undefined && this.shown !== window)
|
|
79
|
+
this.history = [...this.history.filter((each) => each !== this.shown && each !== window), this.shown];
|
|
80
|
+
this.shown = window;
|
|
81
|
+
}
|
|
82
|
+
/** The window to show once `closed` is gone: the one that opened it, else the one shown before it that is still open, else the newest. */
|
|
83
|
+
fallback(closed) {
|
|
84
|
+
const open = this.open();
|
|
85
|
+
const opener = closed?.page.opener === undefined ? undefined : open.find((window) => window.page === closed.page.opener);
|
|
86
|
+
this.history = this.history.filter((window) => open.includes(window) && window !== opener);
|
|
87
|
+
return opener ?? this.history.pop() ?? open.at(-1);
|
|
88
|
+
}
|
|
89
|
+
/** Whether a page is registered as a window. */
|
|
90
|
+
has(page) {
|
|
91
|
+
return this.byPage.has(page);
|
|
92
|
+
}
|
|
93
|
+
/** Whether a page is the window `screen` and the agent read. */
|
|
94
|
+
isShownPage(page) {
|
|
95
|
+
return this.shown?.page === page;
|
|
96
|
+
}
|
|
97
|
+
/** The open windows, in the order they opened. A `WebContentsView` is a page of its own and counts as a window. */
|
|
98
|
+
open() {
|
|
99
|
+
return [...this.byPage.values()].filter(isOpen).toSorted((a, b) => a.serial - b.serial);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The window `screen` and the agent read: the one chosen, else the first the
|
|
103
|
+
* app opened. Once that one closes, the window that opened it, else the
|
|
104
|
+
* one shown before it that is still open, else the newest.
|
|
105
|
+
*/
|
|
106
|
+
current() {
|
|
107
|
+
if (this.shown !== undefined && isOpen(this.shown))
|
|
108
|
+
return this.shown;
|
|
109
|
+
this.shown = this.fallback(this.shown);
|
|
110
|
+
return this.shown;
|
|
111
|
+
}
|
|
112
|
+
/** The current window, or why there is none. */
|
|
113
|
+
require() {
|
|
114
|
+
const window = this.current();
|
|
115
|
+
if (window === undefined)
|
|
116
|
+
throw invalidState('the app has no open window');
|
|
117
|
+
return window;
|
|
118
|
+
}
|
|
119
|
+
/** The window and backend node a ref names, or a stale error when it is gone or from another window. */
|
|
120
|
+
bind(ref) {
|
|
121
|
+
const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(ref.id);
|
|
122
|
+
if (match === null)
|
|
123
|
+
throw stale(`node ${ref.id} is not a node of this app`);
|
|
124
|
+
const window = this.current();
|
|
125
|
+
if (window === undefined || window.serial !== Number(match[1]))
|
|
126
|
+
throw stale(`node ${ref.id} is in a window that is not shown now`);
|
|
127
|
+
if (window.page.generation !== Number(match[2]))
|
|
128
|
+
throw stale(`node ${ref.id} is in a document the window no longer shows`);
|
|
129
|
+
return { window, backendNodeId: Number(match[3]) };
|
|
130
|
+
}
|
|
131
|
+
/** Shows the open window at `index`. */
|
|
132
|
+
show(index) {
|
|
133
|
+
const open = this.open();
|
|
134
|
+
const window = open[index];
|
|
135
|
+
if (window === undefined)
|
|
136
|
+
throw notActionable(`no such window: the app has ${windowCount(open.length)} open`);
|
|
137
|
+
this.showWindow(window);
|
|
138
|
+
return window;
|
|
139
|
+
}
|
|
140
|
+
/** The open window at `index`, which closing leaves another open. */
|
|
141
|
+
closable(index) {
|
|
142
|
+
const open = this.open();
|
|
143
|
+
const window = open[index];
|
|
144
|
+
if (window === undefined)
|
|
145
|
+
throw notActionable(`no such window: the app has ${windowCount(open.length)} open`);
|
|
146
|
+
// The contract keeps the last tab open; for most apps the last window closing quits the app.
|
|
147
|
+
if (open.length === 1)
|
|
148
|
+
throw notActionable('this is the app\'s only window; closing it would end the app');
|
|
149
|
+
return window;
|
|
150
|
+
}
|
|
151
|
+
/** Makes an open window the shown one. */
|
|
152
|
+
activate(window) {
|
|
153
|
+
if (!this.open().includes(window))
|
|
154
|
+
throw new TestError('ACTION_FAILED', 'the window has closed');
|
|
155
|
+
this.showWindow(window);
|
|
156
|
+
}
|
|
157
|
+
/** The open windows with what a matcher reads of each, so a window that closes while the titles are read cannot shift a match onto another. */
|
|
158
|
+
async describe(windowTitle) {
|
|
159
|
+
return await Promise.all(this.open().map(async (window) => {
|
|
160
|
+
// A window whose renderer crashed, or is busy, never answers: it is listed without a title rather than not at all.
|
|
161
|
+
const [title = '', own] = await Promise.all([answerWithin(window.page.title(), TITLE_MS), windowTitle(window.page)]);
|
|
162
|
+
return { window, title, ...(own === undefined || own === title ? {} : { windowTitle: own }), url: window.page.url() };
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
/** The first open window `which` matches, waiting for it to open until a little before the operation's deadline. It is not shown: the caller activates it. */
|
|
166
|
+
async find(which, windowTitle, operation) {
|
|
167
|
+
let described = [];
|
|
168
|
+
let found;
|
|
169
|
+
const budget = new Budget(operation, 'electron.window');
|
|
170
|
+
await pollCondition({
|
|
171
|
+
deadline: budget.waits,
|
|
172
|
+
signal: operation.signal,
|
|
173
|
+
negated: false,
|
|
174
|
+
evaluate: async () => {
|
|
175
|
+
described = await this.describe(windowTitle);
|
|
176
|
+
// A match that closed while the titles were read is no match; the next read looks again.
|
|
177
|
+
found = described.find((each) => matchesWindow(each, which) && this.open().includes(each.window))?.window;
|
|
178
|
+
return found !== undefined;
|
|
179
|
+
},
|
|
180
|
+
onTimeout: () => notActionable(`no window matches ${describeMatcher(which)}; open: ${described.map(({ title }) => JSON.stringify(title)).join(', ') || 'none'}`),
|
|
181
|
+
});
|
|
182
|
+
if (found === undefined)
|
|
183
|
+
throw notActionable(`no window matches ${describeMatcher(which)}`);
|
|
184
|
+
return found;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function windowCount(count) {
|
|
188
|
+
return `${count} window${count === 1 ? '' : 's'}`;
|
|
189
|
+
}
|
|
190
|
+
/** Whether a window is one the app shows its user: open, and not an inspector the app's own DevTools opened. */
|
|
191
|
+
function isOpen(window) {
|
|
192
|
+
return !window.page.isClosed() && !window.page.url().startsWith('devtools://');
|
|
193
|
+
}
|
|
194
|
+
function matchesWindow(window, which) {
|
|
195
|
+
const test = (pattern, value) => pattern === undefined || (typeof pattern === 'string' ? value === pattern : new RegExp(pattern.source, pattern.flags).test(value));
|
|
196
|
+
return (test(which.title, window.title) || (window.windowTitle !== undefined && test(which.title, window.windowTitle))) && test(which.url, window.url);
|
|
197
|
+
}
|
|
198
|
+
function describeMatcher(which) {
|
|
199
|
+
return [which.title === undefined ? '' : `title ${String(which.title)}`, which.url === undefined ? '' : `url ${String(which.url)}`].filter(Boolean).join(' and ') || 'any window';
|
|
200
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,75 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e2e-dev/electron",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Electron engine for e2e agentic end-to-end testing: launches an Electron app and drives its windows through Chromium's accessibility tree",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"e2e",
|
|
7
|
+
"end-to-end",
|
|
8
|
+
"testing",
|
|
9
|
+
"ai-testing",
|
|
10
|
+
"electron",
|
|
11
|
+
"desktop-testing",
|
|
12
|
+
"accessibility"
|
|
13
|
+
],
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/e2e-inc/e2e.git",
|
|
18
|
+
"directory": "packages/electron"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/e2e-inc/e2e/issues"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://e2e.com/docs/electron",
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"registry": "https://registry.npmjs.org/",
|
|
26
|
+
"access": "public",
|
|
27
|
+
"tag": "latest"
|
|
28
|
+
},
|
|
29
|
+
"type": "module",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./engine": {
|
|
36
|
+
"types": "./dist/engine-entry.d.ts",
|
|
37
|
+
"default": "./dist/engine-entry.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"NOTICE"
|
|
43
|
+
],
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@e2e-dev/core": ">=0.25.1 <1",
|
|
46
|
+
"e2e": ">=0.25.1 <1",
|
|
47
|
+
"electron": "*"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"e2e": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@e2e-dev/core": "0.25.0",
|
|
56
|
+
"@napi-rs/image": "1.16.0",
|
|
57
|
+
"@types/node": "26.6.11",
|
|
58
|
+
"devtools-protocol": "0.0.1756183",
|
|
59
|
+
"e2e": "0.25.0",
|
|
60
|
+
"electron": "45.5.0",
|
|
61
|
+
"typescript": "7.0.2",
|
|
62
|
+
"vitest": "5.1.2"
|
|
63
|
+
},
|
|
64
|
+
"engines": {
|
|
65
|
+
"node": "^22.22.3 || >=24.8.0"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc --project tsconfig.build.json && node ../../scripts/stamp-dist.ts",
|
|
69
|
+
"typecheck": "tsc --noEmit",
|
|
70
|
+
"test": "vitest run --project unit",
|
|
71
|
+
"test:watch": "vitest",
|
|
72
|
+
"test:unit": "vitest run tests/unit",
|
|
73
|
+
"test:integration": "vitest run --project integration"
|
|
74
|
+
}
|
|
6
75
|
}
|