@e2e-dev/electron 0.1.1 → 0.1.2-nightly-20261205030542

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/dist/capture.js CHANGED
@@ -1,10 +1,11 @@
1
1
  /**
2
- * Pixels: screenshots with every password field and nested document painted
3
- * over, withheld when the window holds one no mask reaches.
2
+ * Pixels: screenshots of the viewport or the whole page with every password
3
+ * field and nested document painted over, withheld when the window holds one
4
+ * no mask reaches.
4
5
  */
5
6
  import { mkdirSync, writeFileSync } from 'node:fs';
6
7
  import path from 'node:path';
7
- import { ConfigurationError, readPngSize } from '@e2e-dev/core/engine';
8
+ import { ConfigurationError, readPngSize, TestError } from '@e2e-dev/core/engine';
8
9
  import { pixelRatio, placeMasks, removeMasks } from './in-page/masks.js';
9
10
  import { viewportSize } from './in-page/node-ops.js';
10
11
  import { inDocument } from './locate.js';
@@ -32,14 +33,14 @@ function outOfReach(count) {
32
33
  * capture runs, inside a closed shadow root or beside the masks already
33
34
  * placed, so the document is read again before the pixels go out.
34
35
  */
35
- function maskedCapture(devtools, window, unmaskableBefore, scale) {
36
+ function maskedCapture(devtools, window, unmaskableBefore, scale, region) {
36
37
  // One capture of a page at a time: the masks are the page's, and a second capture would take the first one's away.
37
- const turn = (captures.get(window.page) ?? Promise.resolve()).then(() => captureUnderMasks(devtools, window, unmaskableBefore, scale));
38
+ const turn = (captures.get(window.page) ?? Promise.resolve()).then(() => captureUnderMasks(devtools, window, unmaskableBefore, scale, region));
38
39
  captures.set(window.page, turn.catch(() => undefined));
39
40
  return turn;
40
41
  }
41
42
  const captures = new WeakMap();
42
- async function captureUnderMasks(devtools, window, unmaskableBefore, scale) {
43
+ async function captureUnderMasks(devtools, window, unmaskableBefore, scale, region) {
43
44
  const { page } = window;
44
45
  if (unmaskableBefore === undefined)
45
46
  return { masked: (await page.evaluate(removeMasks, MASKED_SELECTOR)).count, withheld: UNREAD };
@@ -47,10 +48,10 @@ async function captureUnderMasks(devtools, window, unmaskableBefore, scale) {
47
48
  return { masked: (await page.evaluate(removeMasks, MASKED_SELECTOR)).count, withheld: outOfReach(unmaskableBefore) };
48
49
  const placed = await page.evaluate(placeMasks, MASKED_SELECTOR);
49
50
  const { count: masked } = placed;
50
- let image;
51
+ let shot;
51
52
  let after;
52
53
  try {
53
- image = await captureViewport(window, scale);
54
+ shot = region === undefined ? await captureViewport(window, scale) : await capturePage(window, scale, region);
54
55
  }
55
56
  finally {
56
57
  // The masks never outlive the capture, whatever became of it.
@@ -73,7 +74,7 @@ async function captureUnderMasks(devtools, window, unmaskableBefore, scale) {
73
74
  if (after.boxes !== placed.boxes) {
74
75
  return { masked, withheld: 'a password field or nested document moved while the screenshot was taken, out from under its mask, so the screenshot is withheld' };
75
76
  }
76
- return { masked, image };
77
+ return { masked, ...shot };
77
78
  }
78
79
  /** The window's viewport as a PNG: one image pixel per CSS pixel, or per device pixel. */
79
80
  async function captureViewport(window, scale) {
@@ -87,36 +88,83 @@ async function captureViewport(window, scale) {
87
88
  clip = { x: scrolled.pageX, y: scrolled.pageY, width, height, scale: 1 / ratio };
88
89
  }
89
90
  const { data } = await page.captured(() => page.session.send('Page.captureScreenshot', { format: 'png', ...(clip === undefined ? {} : { clip }) }));
90
- return Buffer.from(data, 'base64');
91
+ return { image: Buffer.from(data, 'base64') };
91
92
  }
92
- /** Adds the pixels to an observation of a read window, or leaves them out when they are withheld. */
93
- export async function withPixels(devtools, raw, snapshot) {
93
+ /**
94
+ * The window's whole page as a PNG, cut to the region's clip: Chromium paints
95
+ * past the viewport without scrolling or resizing the window. A fixed
96
+ * element, the masks among them, lands where it shows at the current scroll,
97
+ * the same place in the page as the element it covers.
98
+ */
99
+ async function capturePage(window, scale, region) {
100
+ const { page } = window;
101
+ const [{ cssContentSize: content, cssVisualViewport: scrolled }, ratio] = await Promise.all([page.session.send('Page.getLayoutMetrics'), page.evaluate(pixelRatio)]);
102
+ const whole = { x: 0, y: 0, width: Math.ceil(content.width), height: Math.ceil(content.height) };
103
+ let kept = whole;
104
+ if (region.clip !== undefined) {
105
+ const inside = overlap(region.clip, whole);
106
+ const { x, y, width, height } = region.clip;
107
+ if (inside === undefined)
108
+ throw new TestError('INVALID_ARGUMENT', `screenshot clip { x: ${x}, y: ${y}, width: ${width}, height: ${height} } is outside the ${whole.width}x${whole.height} page`);
109
+ kept = inside;
110
+ }
111
+ // Past this many image pixels on a side, Chromium repeats the start of the page instead of painting the rest.
112
+ const output = { width: kept.width * (scale === 'css' ? 1 : ratio), height: kept.height * (scale === 'css' ? 1 : ratio) };
113
+ if (Math.max(output.width, output.height) > MAX_CAPTURE_PIXELS) {
114
+ throw new ConfigurationError('UNSUPPORTED_CAPABILITY', `a ${Math.ceil(output.width)}x${Math.ceil(output.height)} capture is past the ${MAX_CAPTURE_PIXELS} pixels on a side Electron paints in one screenshot; capture a clip of the page`);
115
+ }
116
+ // The clip is read in the page's zoomed pixels, and `devicePixelRatio` counts the zoom too.
117
+ const zoom = scrolled.zoom ?? 1;
118
+ const clip = { x: kept.x * zoom, y: kept.y * zoom, width: kept.width * zoom, height: kept.height * zoom, scale: scale === 'css' ? 1 / ratio : 1 };
119
+ const { data } = await page.captured(() => page.session.send('Page.captureScreenshot', { format: 'png', clip, captureBeyondViewport: true }));
120
+ return { image: Buffer.from(data, 'base64'), page: { width: kept.width, scroll: { x: scrolled.pageX, y: scrolled.pageY } } };
121
+ }
122
+ /** The most pixels on a side one `Page.captureScreenshot` paints; Chromium's limit for a single compositor frame. */
123
+ const MAX_CAPTURE_PIXELS = 16_384;
124
+ /** The part of `box` inside `bounds`; undefined when none of it is. */
125
+ function overlap(box, bounds) {
126
+ const x = Math.max(box.x, bounds.x);
127
+ const y = Math.max(box.y, bounds.y);
128
+ const width = Math.min(box.x + box.width, bounds.x + bounds.width) - x;
129
+ const height = Math.min(box.y + box.height, bounds.y + bounds.height) - y;
130
+ return width > 0 && height > 0 ? { x, y, width, height } : undefined;
131
+ }
132
+ /** Adds the pixels of the viewport, or with `fullPage` of the whole page, to an observation of a read window, or leaves them out when they are withheld. */
133
+ export async function withPixels(devtools, raw, snapshot, { fullPage = false } = {}) {
94
134
  // The pixels are of the document the snapshot describes, or the observation is taken again.
95
- const capture = await inDocument(raw.window, raw.generation, () => maskedCapture(devtools, raw.window, raw.dom.unmaskableCount(), 'css'));
135
+ const capture = await inDocument(raw.window, raw.generation, () => maskedCapture(devtools, raw.window, raw.dom.unmaskableCount(), 'css', fullPage ? { fullPage: true } : undefined));
96
136
  if ('withheld' in capture)
97
137
  return { ...snapshot, maskedRegionCount: capture.masked };
98
138
  const data = new Uint8Array(capture.image);
99
- const { width: css } = await raw.window.page.evaluate(viewportSize);
100
139
  const size = readPngSize(data);
101
140
  if (size === undefined)
102
141
  throw new Error('the window capture is not a PNG');
103
- const pixels = { data, mediaType: 'image/png', width: size.width, height: size.height, scale: css > 0 ? size.width / css : 1 };
142
+ const css = capture.page?.width ?? (await raw.window.page.evaluate(viewportSize)).width;
143
+ const pixels = {
144
+ data,
145
+ mediaType: 'image/png',
146
+ width: size.width,
147
+ height: size.height,
148
+ scale: css > 0 ? size.width / css : 1,
149
+ ...(capture.page === undefined ? {} : { scroll: capture.page.scroll }),
150
+ };
104
151
  return { ...snapshot, pixels, maskedRegionCount: capture.masked };
105
152
  }
106
153
  /**
107
- * `app.screenshot(label)`: the window with its masks painted over, written
108
- * under the attempt's artifacts as `screenshots/<n>-<label>.png`. Refused
109
- * with `POLICY_DENIED` when the capture is withheld, when `refused` says why
110
- * the window is not one to capture, or when the window loaded another
111
- * document while the capture waited its turn: the pixels are of the document
112
- * `refused` approved, or none are kept.
154
+ * `app.screenshot(label)`: the window's viewport, or the whole page the
155
+ * region asks for, with its masks painted over, written under the attempt's
156
+ * artifacts as `screenshots/<n>-<label>.png`. Refused with `POLICY_DENIED`
157
+ * when the capture is withheld, when `refused` says why the window is not
158
+ * one to capture, or when the window loaded another document while the
159
+ * capture waited its turn: the pixels are of the document `refused`
160
+ * approved, or none are kept.
113
161
  */
114
- export async function screenshotFile(devtools, window, artifactsDir, name, refused) {
162
+ export async function screenshotFile(devtools, window, artifactsDir, name, refused, region) {
115
163
  const before = refused();
116
164
  if (before !== undefined)
117
165
  throw new ConfigurationError('POLICY_DENIED', before);
118
166
  const generation = window.page.generation;
119
- const capture = await maskedCapture(devtools, window, await unmaskable(devtools, window), 'device');
167
+ const capture = await maskedCapture(devtools, window, await unmaskable(devtools, window), 'device', region);
120
168
  if ('withheld' in capture)
121
169
  throw new ConfigurationError('POLICY_DENIED', capture.withheld);
122
170
  if (window.page.generation !== generation)
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Trusted input over `Input.*`: a keyboard and a mouse for one page, each
3
- * tracking what is held so modifiers apply to the keys and clicks after
4
- * them.
2
+ * Trusted input over `Input.*`: a keyboard and a mouse for one page, sharing
3
+ * what `electron.keyboard.down` and `electron.mouse.down` hold across the
4
+ * app's windows while keeping a gesture's own chords and presses on its page.
5
5
  */
6
6
  import type { Session } from './connection.ts';
7
7
  type Modifier = 'Alt' | 'Control' | 'Meta' | 'Shift';
@@ -13,22 +13,55 @@ export interface InputTarget {
13
13
  }
14
14
  /** Whether the keyboard has a key by this name: a key code (`Enter`, `Space`) or a character of the US layout. */
15
15
  export declare function hasKey(key: string): boolean;
16
+ /** Keys and mouse buttons `electron.keyboard.down` and `electron.mouse.down` hold across an app's windows. */
17
+ export declare class HeldInput {
18
+ #private;
19
+ readonly modifiers: Map<Modifier, Set<InputTarget>>;
20
+ readonly keys: Map<string, Set<InputTarget>>;
21
+ readonly buttons: Map<MouseButton, Set<InputTarget>>;
22
+ /** The button of the newest press, or `none` after a release. */
23
+ get lastButton(): MouseButton | 'none';
24
+ /** Sets the last button and returns the write's number, which a failed press compares to undo only its own write. */
25
+ setLastButton(button: MouseButton | 'none'): number;
26
+ /** Whether no write came after the one numbered `write`. */
27
+ isLastWrite(write: number): boolean;
28
+ }
16
29
  export declare class Keyboard {
17
30
  private readonly target;
18
- private readonly heldModifiers;
19
- private readonly heldKeys;
20
- constructor(target: InputTarget);
21
- /** The modifiers held now. */
31
+ private readonly shared;
32
+ private readonly localModifiers;
33
+ private readonly localKeys;
34
+ private readonly pinnedModifiers;
35
+ constructor(target: InputTarget, shared: HeldInput);
36
+ /** The modifiers held now, across the app and by an in-flight gesture on this page. */
22
37
  modifiers(): ReadonlySet<Modifier>;
23
38
  /** Whether a modifier is held now, by any name it is pressed by (`ControlOrMeta` too). */
24
39
  holds(modifier: string): boolean;
25
- /** Forgets what is held: the renderer that held it is gone. */
40
+ /** Keeps an already-held modifier active on this page for the duration of a chord, even if another window closes. */
41
+ pinModifier(modifier: string): void;
42
+ /** Releases the chord pin placed by `pinModifier`. */
43
+ unpinModifier(modifier: string): void;
44
+ /** Forgets the keys held on this page once it closes or its renderer crashes. */
26
45
  reset(): void;
27
- /** Presses a key down and leaves it held; a held modifier applies to the keys and mouse events after it. */
28
- down(key: string): Promise<void>;
29
- /** Releases a key `down` pressed. */
30
- up(key: string): Promise<void>;
31
- /** Presses a key down and releases it, under whatever modifiers are held. */
46
+ /** Presses a key down and leaves it held; `local` keeps a gesture's own press on this page alone. */
47
+ down(key: string, options?: {
48
+ readonly local?: boolean;
49
+ }): Promise<void>;
50
+ /**
51
+ * Releases a key `down` pressed. A shared release commits before its event
52
+ * is sent, as one keyboard's would: a send that fails on a closing window
53
+ * still releases the key everywhere, as a send that lands does.
54
+ */
55
+ up(key: string, options?: {
56
+ readonly local?: boolean;
57
+ }): Promise<void>;
58
+ /**
59
+ * Forgets a shared hold on `code`. A modifier stays held while another key
60
+ * of it is (`ShiftRight` after `ShiftLeft` goes up), except when released
61
+ * by its plain name: `up('Shift')` releases both sides, as on `main`.
62
+ */
63
+ private release;
64
+ /** Presses a key down and releases it on this page, releasing a shared hold on that key once the press answers. */
32
65
  press(key: string): Promise<void>;
33
66
  /** Types one character: its key when the layout has one, else the character inserted as text. */
34
67
  type(character: string): Promise<void>;
@@ -47,18 +80,28 @@ export declare class Keyboard {
47
80
  export declare class Mouse {
48
81
  private readonly target;
49
82
  private readonly keyboard;
83
+ private readonly shared;
50
84
  private x;
51
85
  private y;
52
- private lastButton;
53
- private readonly buttons;
54
- constructor(target: InputTarget, keyboard: Keyboard);
55
- /** Forgets the buttons held: the renderer that held them is gone. */
86
+ private localLastButton;
87
+ private readonly localButtons;
88
+ constructor(target: InputTarget, keyboard: Keyboard, shared: HeldInput);
89
+ /** Forgets the buttons held on this page once it closes or its renderer crashes. */
56
90
  reset(): void;
91
+ private heldButtons;
57
92
  move(x: number, y: number): Promise<void>;
58
- /** Presses a button where the pointer is. */
59
- down(button?: MouseButton, clickCount?: number): Promise<void>;
60
- /** Releases a button where the pointer is. */
61
- up(button?: MouseButton, clickCount?: number): Promise<void>;
93
+ /** Presses a button where the pointer is; `local` keeps a gesture's own press on this page alone. */
94
+ down(button?: MouseButton, clickCount?: number, options?: {
95
+ readonly local?: boolean;
96
+ }): Promise<void>;
97
+ /**
98
+ * Releases a button where the pointer is. The release commits before its
99
+ * event is sent, and a gesture's own (`local`) release also releases the
100
+ * button the test holds, as a tap does: one mouse serves every window.
101
+ */
102
+ up(button?: MouseButton, clickCount?: number, options?: {
103
+ readonly local?: boolean;
104
+ }): Promise<void>;
62
105
  /** Moves to a point and clicks `clickCount` times, the events sent back to back as one gesture. */
63
106
  click(x: number, y: number, button?: MouseButton, clickCount?: number): Promise<void>;
64
107
  /** A wheel event where the pointer is. */