feat(desktop)!: align the desktop fixture with browser and electron
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');
| Before | After |
|---|---|
desktop.useWindow(window | id | undefined) | window.activate() on a handle |
desktop.windows() returns { id, title, focused } records | handles with title(), isActive(), isClosed(), activate(), close() |
expect.poll over desktop.windows() to wait for a window | desktop.waitForWindow(trigger), desktop.window({ title }) |
a closed chosen window fails every read with INVALID_STATE | screen 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
createDesktopFixturehands out one handle per window id (packages/desktop/src/desktop.ts:82). Each read askssurface.windows(), soisActive()andisClosed()are promises: macOS accessibility has no window events to keep a sync answer current.waitForWindowlists 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 sharewaitFor(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.- The surface keeps the one pinned id it had.
activeChoicedrops it once the app no longer lists that window, unless the screen is locked, which is a retryableNODE_STALE(packages/desktop/src/surface.ts:515).readWindowsreads an explicit window id, else that choice, else the focused window (surface.ts:748). This replaces theINVALID_STATEa closed chosen window raised. closeWindowis a new driver call (crates/e2e-desktop-napi/src/actions/mod.rs:629): it pressesAXCloseButtonand 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, andlistWindowsreads 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 isNOT_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).e2e mcpdrafteddesktop.useWindow(window)by the window's title through atestLinefield 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.testLineandFixtureTestLinefrom@e2e-dev/core/engine, the lookup inpackages/core/src/run/fixture-recording.ts, the rewrite inpackages/e2e/src/session-tools/code-recorder.ts, its entries indocs/reference/engine.mdxanddocs/reference/mcp.mdx, and the unreleasedmcp-fixture-test-linechangeset. The rest of #2355 stays.physicalInputreplacesheadedinoptions.tsandengine.tswith no behavior change. The testbed suite follows:tests-physical/,e2e.physical.config.ts,pnpm test:physical.docs/ja/desktop.mdx,docs/zh/desktop.mdx, anddocs/pt/desktop.mdxare translated again from the English page, since each still describeduseWindowandheadedand two of them the agent-desktop driver that is gone.
Decisions a reviewer would ask about:
screenstill 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 onlyphysicalInputallows.- 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, andkeyboard.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.pressfor2 + 3 =,desktop.window({ title: /calc/i }), thenclose()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.typeinto a document,waitForWindowonControlOrMeta+n,close()on the new document, thenclose()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 mcpon the testbed, onerun_codecall. The drafted lines find a window the way a test would, with notestLine:
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-uptimed out twice under a load average of 7 to 9 (1 s budget), andtypes a username with an uppercase letterfailed once and passed the next four runs. - No video: the engine records none.
- Unit: 272 in
packages/desktop, 66 in the crate.