feat(desktop)!: align the desktop fixture with browser and electron

Merged
OK
15h ago
+822-516

What

The desktop fixture now has the shape of browser and electron. All of it is breaking, on a package that has no stable release yet.

const settings = await desktop.waitForWindow(() =>
  screen.getByRole('button', 'Settings…').tap(),
);
await expect(screen.getByRole('checkbox', 'Launch at login')).toBeVisible();

const main = await desktop.window({ title: 'My App' });
await main.activate();
await settings.close();
await desktop.keyboard.press('ControlOrMeta+2');
BeforeAfter
desktop.useWindow(window | id | undefined)window.activate() on a handle
desktop.windows() returns { id, title, focused } recordshandles with title(), isActive(), isClosed(), activate(), close()
expect.poll over desktop.windows() to wait for a windowdesktop.waitForWindow(trigger), desktop.window({ title })
a closed chosen window fails every read with INVALID_STATEscreen reads the focused window again
desktop.press(key)desktop.keyboard.press(key), plus desktop.keyboard.type(text)
desktop({ headed: true })desktop({ physicalInput: true })

Why

useWindow was the only window API in the repo that took a plain record or an id string and had a no-argument reset. Web has BrowserTab and Electron has ElectronWindow, both with activate() and close(). desktop.press sat on the fixture where browser has keyboard. The headed option turned on the real pointer and keyboard, which the run's --headed flag does not, so one word meant two things.

How it works

  1. createDesktopFixture hands out one handle per window id (packages/desktop/src/desktop.ts:82). Each read asks surface.windows(), so isActive() and isClosed() are promises: macOS accessibility has no window events to keep a sync answer current.
  2. waitForWindow lists the windows, runs the trigger, then polls for a new one and activates it (desktop.ts:131). It skips a dialog macOS put over the app and a window only the window server lists. window({ title }) polls for a title match and does not activate (desktop.ts:150). Both share waitFor (desktop.ts:104): each list read keeps the step's full budget, so a trigger that used up the wait still gets one real look, and a list the app was too busy to answer is read again.
  3. The surface keeps the one pinned id it had. activeChoice drops it once the app no longer lists that window, unless the screen is locked, which is a retryable NODE_STALE (packages/desktop/src/surface.ts:515). readWindows reads an explicit window id, else that choice, else the focused window (surface.ts:748). This replaces the INVALID_STATE a closed chosen window raised.
  4. closeWindow is a new driver call (crates/e2e-desktop-napi/src/actions/mod.rs:629): it presses AXCloseButton and waits until the window is gone from the app's accessibility windows and from the window server's on-screen list (windows.rs:293). The window server keeps a window through its closing animation, and listWindows reads both lists. A window that stays open, as one asking about unsaved changes does, fails as a press that may have landed. No close button is NOT_ACTIONABLE. An app that quits with its last window, as Calculator does, counts as closed: the process is asked before its accessibility. A window that already closed is a no-op (surface.ts:490).
  5. e2e mcp drafted desktop.useWindow(window) by the window's title through a testLine field on fixture operations (#2355), because a window id does not hold on the next launch. A handle needs none: desktop.window({ title }) is the call a test writes, and the recorder names the handle it returns as it names a new tab. Desktop was the field's only user, so it is removed: FixtureOperation.testLine and FixtureTestLine from @e2e-dev/core/engine, the lookup in packages/core/src/run/fixture-recording.ts, the rewrite in packages/e2e/src/session-tools/code-recorder.ts, its entries in docs/reference/engine.mdx and docs/reference/mcp.mdx, and the unreleased mcp-fixture-test-line changeset. The rest of #2355 stays.
  6. physicalInput replaces headed in options.ts and engine.ts with no behavior change. The testbed suite follows: tests-physical/, e2e.physical.config.ts, pnpm test:physical.
  7. docs/ja/desktop.mdx, docs/zh/desktop.mdx, and docs/pt/desktop.mdx are translated again from the English page, since each still described useWindow and headed and two of them the agent-desktop driver that is gone.

Decisions a reviewer would ask about:

  • screen still follows the focused window until a test activates one. Web and Electron keep the first window active instead. Here that would hide an alert window from the agent, which has no tab switching on desktop.
  • activate() pins the window for reads and does not raise it. Bringing a window forward takes the user's focus, which only physicalInput allows.
  • Window waits live in the fixture. The surface owns which window is active.

Not in this PR: surface.ts (1,586 lines) and surface.test.ts were over 1,000 lines before and are not split here. The fixture tests call the fixture without the step recorder, so its labels and timeouts are covered only by the hand-run testbed.

Verified

Ran it locally: yes, on 9c2f8ef3c6, macOS 27, with the driver built from this branch.

  • Desktop testbed, app in the background, pnpm --filter @e2e-dev/desktop-testbed test: 30 of 30. Three tests are new: handles and switching by title, closing the active window, and keyboard.type.
   ✓ |macos| tests/controls.e2e.ts > controls window > reads the window the trigger opened as the active one, and switches back by title
   ✓ |macos| tests/controls.e2e.ts > controls window > closes the active window, and reads the focused one again
   ✓ |macos| tests/controls.e2e.ts > controls window > types into the focused field through the desktop keyboard
 Test Files  2 passed (2)
      Tests  30 passed (30)
  • Physical input, pnpm test:physical: 5 of 5, three runs. Earlier runs failed 3 of 5 because a macOS Continuity Camera prompt floated over the Controls window; with it dismissed they pass.
  • Calculator, a scratch test: desktop.keyboard.press for 2 + 3 =, desktop.window({ title: /calc/i }), then close() on its only window. The first run failed with "the window stayed open", because Calculator quits with its last window. That is the fix in step 4.
title "Calculator" active true
missing window: TestError: no window has the title "Nope"; open: "Calculator"
closed true windows 0
  • TextEdit with physicalInput, a scratch test: keyboard.type into a document, waitForWindow on ControlOrMeta+n, close() on the new document, then close() on the edited one, which asks to save.
first "Untitled 4"
second "Untitled 5" active true first active false
second closed true first active true
close edited: TestError: close window: the window stayed open after its close button was pressed; it may be asking to save its changes (the input may have reached the app) in window.close closed false
  • e2e mcp on the testbed, one run_code call. The drafted lines find a window the way a test would, with no testLine:
1. await app.open();
2. const waitForWindow1 = await desktop.waitForWindow(() => screen.getByRole("button", "Controls…").tap()); // the function brings none of the variables run_code declared: declare the ones it uses
3. await desktop.keyboard.press('Tab');
4. await waitForWindow1.close();
5. await expect(screen.getByRole('button', 'Sign in')).toBeVisible();

A second call drafted const window2 = await desktop.window({ title: 'Sign In' }); and await window2.activate();.

  • Flakes seen across about a dozen full background runs, neither in code this PR touches: refuses a disabled option of a pop-up timed out twice under a load average of 7 to 9 (1 s budget), and types a username with an uppercase letter failed once and passed the next four runs.
  • No video: the engine records none.
  • Unit: 272 in packages/desktop, 66 in the crate.