feat(electron): assert on the window with expect(electron)

OK
1d ago
+554-15

What

expect(electron) asserts on the active window and retries, with the matchers expect(browser) has on the web, and electron.waitForURL waits for the window's URL.

await expect(electron).toHaveURL(/\/settings$/);
await expect(electron).toHaveTitle('Settings');
await expect(electron).toHaveClass(screen.getByRole('row', 'Draft'), /selected/);
await expect(electron).not.toHaveTitle('Sign in');

await electron.waitForURL(/\/workspace\/[^/]+\/debug$/);

The Playwright migration guide gains a tip for porting: run with --action-timeout 5000 so a wrong locator fails fast, and rerun a failure with --last-failed, which keeps its results.

Why

Porting Kong/insomnia's Playwright Electron suite, every page.waitForURL (15 uses), toHaveClass, and expect(page).toHaveTitle had to become an expect.poll around window.url() or a locator read. The web engine has matchers for these; Electron had none.

How it works

  1. The poll. WindowConditions.poll runs a read against the assertion timeout and fails with the last thing it saw (packages/electron/src/expectation.ts:46). Each read fails on a dialog nothing answered, checked before and after it, so one that opens under the wait ends it. For the URL and the title, a read that fails because the document went away, or the window is closed while the app still runs, is one more poll (passing, expectation.ts:76). A read that hangs is left to core's poll to judge. An app that is not running fails at once.
  2. URL. A string must be an absolute URL, parsed once with no base and compared with core's urlMatches; a RegExp tests the URL (expectation.ts:81). A path, a glob, or '' is INVALID_ARGUMENT: an Electron target has no base URL, and the window's own URL moves with the wait. toHaveURL takes ignoreCase, as web's does.
  3. Title. surface.currentTitle reads the document's title inside the poll's deadline and falls back to the OS window's only when the document has none (packages/electron/src/surface.ts:541). The existing windowTitle falls back when the read is slow, which at a poll's last read would compare another title. A read that answers after another window became active is discarded, and the active window polled. A window whose renderer crashed is waited on as a screen read waits on it: up to two seconds for the app to reload it, then APP_NOT_OPEN, also when the crash comes while the read runs.
  4. Class. The same code as web's matcher: the locator's own count() and getAttribute('class'), the class list with its white space collapsed, and no match for an element with no class attribute (expectation.ts:124, readClass at :150). The locator does its own retries.
  5. Docs. The guide's "Wait and assert on the window" section, and entries for waitForURL and the window matchers in docs/reference/electron.mdx.

What differs from web, and why:

  • A URL string must be absolute. Web resolves a relative one against the target's app.url, which an Electron target does not have, and refuses it with APP_URL_REQUIRED when there is none.
  • waitForURL takes no glob. Web's glob matcher lives in @e2e-dev/web; the docs say to use a RegExp for a pattern.

Not in this PR, because web has no counterpart: toHaveCSS and waitForFunction. The docs point to expect.poll over window.evaluate() for both, as web's guide points to browser.evaluate.

Issue

None.

Verified

Ran it locally: yes

Surface: the built CLI on apps/electron-testbed.

packages/electron  unit          Tests  114 passed (114)
packages/electron  integration   Tests  63 passed (63)
apps/electron-testbed            Tests  98 passed (98)
pnpm check                       exit 0

The eleven new testbed tests in apps/electron-testbed/tests/fixture.e2e.ts cover:

  • URL as an absolute string, a RegExp, a RegExp of another realm, and ignoreCase; waitForURL to an absolute URL across a page load.
  • A relative path, a path in a subdirectory, and '' refused with INVALID_ARGUMENT.
  • A title that never comes reporting what it saw; .not.toHaveTitle failing at the deadline; a string matching the whole title; the OS window's title for a document with none.
  • A dialog nothing answered failing the matcher.
  • A wait ending at once with APP_NOT_OPEN when the app quits under it.
  • A wait going on while the window closes and the app opens another, which fails if the retry rule is removed.
  • A dialog that opens after a wait starts ending it.
  • A slow title read that answers after another window became active not passing the matcher.
  • A renderer crash ending a title wait that is running and one that starts after it, and the title read again once the app reloads the window.
  • A dialog that opens while the title is read failing the matcher, though the title matches.
  • A class set 200 ms after the assertion starts; a string matching the whole class list; an element with no class attribute; a locator that matches nothing and one that matches several.

pnpm bench:ab: not run, it has no Electron benchmark. No video: the matchers add no on-screen behavior.