fix(electron): capture and record a window the app hid on Windows and Linux
What
On Windows and Linux, app.screenshot(), the agent's screenshots, and video work for a window the app showed and then hid with hide() (an app that hides to the tray, for one). The engine starts every window of the app with backgroundThrottling off.
Why
Measured with Electron 45.5.0 on Windows Server 2025, and on Linux under xvfb. After BrowserWindow.hide():
Page.captureScreenshotnever returns, soapp.screenshot()waits out the whole timeout (71 s in the testbed's owncaptures a window the app keeps hidden, which fails when the windows are shown:E2E_ELECTRON_HEADED=1).- A screencast sends 0 frames, so a recording ends with
recording 1 stopped, but the engine wrote no video for it.
hide() stops the window's compositor. The screencast the engine starts around a capture only un-hides the renderer's widget, which is enough for a minimized or never-shown window and on macOS, and is why CI is green: the testbed never shows its windows there. No DevTools call reaches the compositor: fromSurface: false, captureBeyondViewport, focus emulation, Page.setWebLifecycleState, Page.bringToFront, and the app's own webContents.capturePage() all hang the same way.
Electron keeps the compositor of a window whose contents have background throttling off. With webContents.setBackgroundThrottling(false) the capture returns in 20 ms with the right pixels.
How it works
setUp, which already asks the main process to place downloads at launch, also callskeepWindowsDrawing.packages/electron/src/app-process.ts:211keepWindowsDrawingturns background throttling off for everywebContentsthere is, and for each window the app opens later, frombrowser-window-created. Nothing is turned back on.app-process.ts:594
This is the same choice the launch switches already make (--disable-renderer-backgrounding, --disable-backgrounding-occluded-windows): a window the user cannot see still draws during a test. It applies on macOS too, where it changes nothing a capture needs, so there is no platform branch.
An app with no inspector (the EnableNodeCliInspectArguments fuse off) has no main process to ask, so there the limit stays, and the packaged-app section of the docs says so.
Dropped, after review:
- Turning throttling off around each capture and back on after it. It needed counted holds, an ordering rule against the screencast, and a path for a main process that answers late, and a recording held it for the whole test anyway.
web-contents-createdfor windows opened later. The flag readsfalse, but a capture of such a window still hangs: the contents belong to no window yet when the event fires.
Not fixed here: a window the app hides the moment it loaded, before it has drawn, still hangs a capture in most runs on both platforms (the tests keep a window shown for 1 s first, and say so). An app that shows a window and hides it within the same tick is the case; one that never shows it works.
Issue
None filed. Found in a Windows verification pass of @e2e-dev/electron.
Verified
Ran it locally: yes. Windows Server 2025 in an interactive desktop session, Linux in a node:24-bookworm container under xvfb-run, and macOS.
| main | this PR | |
|---|---|---|
Windows, testbed with shown windows (E2E_ELECTRON_HEADED=1) | 74 passed, 1 failed after 71 s (captures a window the app keeps hidden) | 75 passed, that test in 0.7 s |
| Windows, testbed with hidden windows, as CI runs it | 75 passed | 75 passed |
| Windows, the two new integration tests, 12 runs | both fail: capture times out, recording has no video | 12 of 12 pass |
| Linux, the two new integration tests, 12 runs | both fail the same way (3 of 3) | 12 of 12 pass |
| Linux, whole integration suite | 47 passed | |
| macOS, whole integration suite | 48 passed |
"main" for the new tests is this branch with the one keepWindowsDrawing call removed.
without the call, Windows and Linux:
TestTimeoutError: Test timed out in 12000ms.
AssertionError: expected [] to have a length of 1 but got +0
probe, one fresh app per row, window shown then hidden (Windows; Linux gives the same):
cdp screencast then capture cdp: TIMEOUT 4007ms
setBackgroundThrottling(false) AFTER hide, cdp cdp: ok 1636 20ms
webPreferences backgroundThrottling false cdp: ok 1636 23ms
capturePage() (never resolves)
shown screencast frames in 3 s: 31
hidden screencast frames in 3 s: 0
hidden, throttling off screencast frames in 3 s: 31
The Windows integration run has one failure that is on main too and fixed in #2409 (spawnSync ls ENOENT). Unit tests 105 passed, pnpm check passes.
No benchmark table: the change is one main-process call at launch, and the testbed takes 90 s before and after.
Summary by CodeRabbit
- Bug Fixes
- Electron windows continue drawing in the background after being hidden, improving screenshots and video recordings when the window has had time to render before it is hidden.
- Documentation
- Clarified that on Windows and Linux, screenshots of hidden windows may time out and recordings may contain no frames.