feat(web): add browser.consoleMessages() and browser.pageErrors() (ENG-955)

Draft
SA
4d ago
+466-35

What

browser.consoleMessages() and browser.pageErrors() on @e2e-dev/web return the console messages, uncaught errors, and unhandled promise rejections in the active tab since it opened.

await app.open('/checkout');
await screen.getByRole('button', 'Place order').tap();
await expect(screen.getByText('Order placed')).toBeVisible();
const errors = (await browser.consoleMessages()).filter((message) => message.type === 'error');
expect(errors).toEqual([]);
expect(await browser.pageErrors()).toEqual([]);

Each message has { type, text }, and each error has { name, message, stack }. A call can fail with APP_NOT_OPEN (no page is open yet), ACTION_FAILED (timed out after actionTimeout), or ENGINE_FAILURE (the browser connection was lost). All three are already in docs/reference/errors.mdx.

Why

To check a page for console errors or uncaught errors, test authors had to add an init script that wrapped console.error and listened for error and unhandledrejection, then read the results back with evaluate.

How it works

  1. Both methods read Playwright's per-page lists for the active tab with { filter: 'all' } (packages/web/src/browser.ts:710). With Playwright's default ('since-navigation'), entries would be dropped at each navigation. Playwright keeps the latest 200 entries per list. The list also has messages the browser writes, such as Chromium's Failed to load resource.

  2. The calls are recorded as browser.consoleMessages and browser.pageErrors steps without a label (packages/web/src/browser.ts:1029). Only the method name is recorded, not its return value. Console output can still appear in the app log, and a failed assertion can include the values it compared.

  3. WebConsoleMessage and WebPageError are exported from the main and engine entries (packages/web/src/browser.ts:127, packages/web/src/engine-entry.ts:38). type is a union written out in the source. It includes Playwright's ConsoleMessage.type() union plus the extra values browsers can send:

    • 'verbose': level for Chromium browser messages, from CDP's Log.entryAdded (coreBundle.js:39722).
    • 'countReset', 'timeLog', and 'timeStamp': Firefox's Juggler maps each console method to a type (consoleLevelToProtocolType in Juggler's content/Runtime.js, in the Firefox 1572 build), and Playwright passes it through (coreBundle.js:47062 and :47115).
    • 'image' and '': WebKit's Console.ConsoleMessage.type, which is optional (coreBundle.js:48507 and :49043, and src/server/webkit/protocol.d.ts in Playwright v1.65.1).

    Chromium's Runtime.consoleAPICalled doesn't add any values. The WebDriver BiDi path (coreBundle.js:43650) can't be reached from web(). If a Playwright bump adds a value, the build fails at type: message.type() (packages/web/src/browser.ts:713). Both return types, the type values, and the lack of a filter option are pinned in packages/web/tests/types/console-messages.ts.

  4. pageErrors() returns each Playwright error's name, message, and stack (packages/web/src/browser.ts:718). Playwright splits the browser's error text at its first colon (splitErrorMessage, coreBundle.js:9628), so a rejected 'Request failed: 500' has the name 'Request failed' and the message '500'. An Error has its class name, such as 'Error' or 'TypeError'.

  5. A persistent browser (connect.reconnectEndpoint, or a provider in attempt scope) reuses its first tab, and Chrome replays that tab's earlier console output and errors to every new CDP client. Both lists are cleared before navigating to about:blank, so entries from the context's init scripts are kept (packages/web/src/attempt-session.ts:328).

  6. After a CDP reconnect, Chrome only replays entries from the current document. Keeping earlier entries would mean carrying them over from the closed connection, so the limit is documented under "Recovering a CDP transport" instead.

  7. The newsletter benchmark test reads browser.consoleMessages() in place of the console hook it injected with browser.evaluate (apps/web-benchmark/tests/newsletter-signup.e2e.ts:14).

Verified

Ran it locally: yes, on Chromium and Firefox 159.

Chromium:

  • packages/web/tests/integration/console-messages.test.ts, pnpm --filter @e2e-dev/web exec vitest run tests/integration/console-messages.test.ts --reporter verbose:
✓ returns each console message with its type and text 140ms
✓ returns thrown errors and unhandled rejections with their name, message, and stack 124ms
✓ splits thrown and rejected non-Error values at the first colon, with an empty stack in Chromium 117ms
✓ includes the messages and errors from the tab's frames and dedicated workers 185ms
✓ keeps the messages and errors from earlier documents in the tab 133ms
✓ keeps a separate list for each tab 150ms
✓ drops entries from before the attempt on a persistent browser while keeping init script entries 460ms
Tests  7 passed (7)
  • The persistent-tab test fails with initial.clearConsoleMessages() deleted (before the attempt comes back), and with the clear calls moved after the about:blank navigation (only one from init script message comes back).
  • Testbed, the built CLI, node node_modules/e2e/dist/cli/bin.js run tests/browser.e2e.ts. The new test logs with console.error, leaves a rejection unhandled, and throws from a timer. All three are read back, and the lists are empty after app.restart():
 ✓ target "web" command at http://127.0.0.1:4271/ ready 117ms
   ✓ |web| tests/browser.e2e.ts > browser fixture > navigation verbs and the URL and title matchers 395ms
   ✓ |web| tests/browser.e2e.ts > browser fixture > reload runs the page again 133ms
   ✓ |web| tests/browser.e2e.ts > browser fixture > an init script runs before the page reads it, from the next load on 119ms
   ✓ |web| tests/browser.e2e.ts > browser fixture > the viewport size is what the page measures 116ms
   ✓ |web| tests/browser.e2e.ts > browser fixture > cookies set from the test reach the page and read back 137ms
   ✓ |web| tests/browser.e2e.ts > browser fixture > console messages and page errors read back and clear on restart 150ms
   ✓ |web| tests/browser.e2e.ts > offline fails the page's requests and navigations until the test goes back online 550ms
   ✓ |web| tests/browser.e2e.ts > emulated media reaches the page's own @media rules, from the first paint on 497ms
      Tests  8 passed (8)
  • Web benchmark, node node_modules/e2e/dist/cli/bin.js run tests/newsletter-signup.e2e.ts after pnpm run build:
 ✓ target "web" command at http://127.0.0.1:4280/ ready 249ms
   ✓ |web| tests/newsletter-signup.e2e.ts > subscribe renders no feedback and logs a TypeError (planted bug) 2.37s
      Tests  1 passed (1)

Firefox, with browser: 'firefox' added to the engine options locally (not committed):

  • console-messages.test.ts: 5 of 7 pass. The persistent-tab test can't run there, since connect is Chromium-only (INVALID_CONFIG). The test for values that aren't an Error fails on its stack: '' expectation, because Firefox reports a stack for them (uncaught exception: 7 and the frames). That still matches the stack doc ("as the browser reports it").
  • Testbed tests/browser.e2e.ts: 7 passed, the new test included.
  • Firefox doesn't add Failed to load resource for a 404. It logs other warnings instead, such as [JavaScript Warning: "This page is in Quirks Mode..."] for a page without a doctype. The consoleMessages entry covers both browsers, with Chromium's message as the example.

Not captured:

  • pnpm bench:ab wasn't run. The cost of clearing both lists when a persistent browser's first tab is reused wasn't measured.
  • Run video: the methods return data and don't change anything on screen.
  • Docs screenshots: the docs site isn't built from this repository, so the changed pages (docs/reference/web.mdx and the Playwright, Cypress, and Selenium migration guides) can't be rendered locally.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md).
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.