feat(electron): assert on the window with expect(electron)
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
- The poll.
WindowConditions.pollruns 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. - URL. A string must be an absolute URL, parsed once with no base and compared with core's
urlMatches; aRegExptests the URL (expectation.ts:81). A path, a glob, or''isINVALID_ARGUMENT: an Electron target has no base URL, and the window's own URL moves with the wait.toHaveURLtakesignoreCase, as web's does. - Title.
surface.currentTitlereads 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 existingwindowTitlefalls 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 ascreenread waits on it: up to two seconds for the app to reload it, thenAPP_NOT_OPEN, also when the crash comes while the read runs. - Class. The same code as web's matcher: the locator's own
count()andgetAttribute('class'), the class list with its white space collapsed, and no match for an element with noclassattribute (expectation.ts:124,readClassat:150). The locator does its own retries. - Docs. The guide's "Wait and assert on the window" section, and entries for
waitForURLand the window matchers indocs/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 withAPP_URL_REQUIREDwhen there is none. waitForURLtakes no glob. Web's glob matcher lives in@e2e-dev/web; the docs say to use aRegExpfor 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, aRegExpof another realm, andignoreCase;waitForURLto an absolute URL across a page load. - A relative path, a path in a subdirectory, and
''refused withINVALID_ARGUMENT. - A title that never comes reporting what it saw;
.not.toHaveTitlefailing 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_OPENwhen 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
classattribute; 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.