feat(web): add browser.consoleMessages() and browser.pageErrors() (ENG-955)
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
-
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'sFailed to load resource. -
The calls are recorded as
browser.consoleMessagesandbrowser.pageErrorssteps 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. -
WebConsoleMessageandWebPageErrorare exported from the main andengineentries (packages/web/src/browser.ts:127,packages/web/src/engine-entry.ts:38).typeis a union written out in the source. It includes Playwright'sConsoleMessage.type()union plus the extra values browsers can send:'verbose': level for Chromium browser messages, from CDP'sLog.entryAdded(coreBundle.js:39722).'countReset','timeLog', and'timeStamp': Firefox's Juggler maps each console method to a type (consoleLevelToProtocolTypein Juggler'scontent/Runtime.js, in the Firefox 1572 build), and Playwright passes it through (coreBundle.js:47062and:47115).'image'and'': WebKit'sConsole.ConsoleMessage.type, which is optional (coreBundle.js:48507and:49043, andsrc/server/webkit/protocol.d.tsin Playwright v1.65.1).
Chromium's
Runtime.consoleAPICalleddoesn't add any values. The WebDriver BiDi path (coreBundle.js:43650) can't be reached fromweb(). If a Playwright bump adds a value, the build fails attype: message.type()(packages/web/src/browser.ts:713). Both return types, thetypevalues, and the lack of a filter option are pinned inpackages/web/tests/types/console-messages.ts. -
pageErrors()returns each Playwright error'sname,message, andstack(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'. AnErrorhas its class name, such as'Error'or'TypeError'. -
A persistent browser (
connect.reconnectEndpoint, or a provider inattemptscope) 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 toabout:blank, so entries from the context's init scripts are kept (packages/web/src/attempt-session.ts:328). -
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.
-
The newsletter benchmark test reads
browser.consoleMessages()in place of the console hook it injected withbrowser.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 attemptcomes back), and with the clear calls moved after theabout:blanknavigation (only onefrom init scriptmessage comes back). - Testbed, the built CLI,
node node_modules/e2e/dist/cli/bin.js run tests/browser.e2e.ts. The new test logs withconsole.error, leaves a rejection unhandled, and throws from a timer. All three are read back, and the lists are empty afterapp.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.tsafterpnpm 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, sinceconnectis Chromium-only (INVALID_CONFIG). The test for values that aren't anErrorfails on itsstack: ''expectation, because Firefox reports a stack for them (uncaught exception: 7and the frames). That still matches thestackdoc ("as the browser reports it").- Testbed
tests/browser.e2e.ts: 7 passed, the new test included. - Firefox doesn't add
Failed to load resourcefor a 404. It logs other warnings instead, such as[JavaScript Warning: "This page is in Quirks Mode..."]for a page without a doctype. TheconsoleMessagesentry covers both browsers, with Chromium's message as the example.
Not captured:
pnpm bench:abwasn'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.mdxand 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/**/*.mdxpage for the behavior, andskills/e2e/if the skill describes it. - Contract change: emitted
.d.tsreviewed,tests/types/sdk-types.tsupdated; wire change edits the schema and both fixtures. - Engine contract change (
e2e/engine): changesets fore2e,@e2e-dev/web, and@e2e-dev/mobile.