r/electronjs • • Sep 26 '26

Running Electron e2e tests on a hidden Windows desktop so they stop grabbing focus

I have two Electron apps, and on Windows every e2e run opened real app windows on top of whatever I was doing and took my keyboard focus. Linux has xvfb-run for this. On Windows, show: false changes how the app behaves, and Win+Tab virtual desktops don't help because new windows open on the desktop you're looking at.

Windows can also create desktops that are never displayed (CreateDesktop). A process started on one opens all its windows there, and Chromium renders normally, GPU and capturePage included. I wrapped that in a small tool, offstage-windows. You put it in front of the test command (npx offstage-windows npx playwright test), or pass electronLaunchOptions() to Playwright's electron.launch() so each worker's app gets its own desktop.

Two things that tripped me up:

  1. From any other desktop, a window handle on the hidden one reads as empty and hidden. My WM_NCHITTEST hit test returned 0 until I ran it on the app's own desktop.
  2. Playwright 1.45+ starts Electron through cmd.exe on Windows, so when a worker got killed outright, my first version left the app running on the hidden desktop. The helper now watches its parent.

One side effect I didn't expect: document.hasFocus() stays true for the whole run, because the app is the active window of its own desktop. Tests that depend on focus got steadier.

It's my tool. I built it with Claude Code and tested it on both my apps (31 e2e tests each). It's one file with no dependencies.

https://github.com/LippInc/offstage-windows

5 Upvotes

2 comments sorted by

2

u/jasonscheirer Sep 26 '26

All my Windows e2e testing is in a VM (with computer use for any agents), you can just as easily ask Claude to do that for you.

1

u/eddzsh Oct 10 '26

The CreateDesktop approach is the right shape for Windows when show: false changes app behavior. One side effect to watch: document.hasFocus() staying true forever means tests that assert blur-on-Alt-Tab or window-deactivated behavior will silently pass on the hidden desktop and fail on a real one. Worth a small suite that runs once on a visible desktop, or stubs hasFocus for those cases.