feat(web): add browser.name and browser.version (ENG-1242)

Merged
OK
4d ago
+356-3

What

The browser fixture gets two read-only properties:

test('exports a PDF', async ({ app, browser }) => {
  test.skip(browser.name !== 'chromium', 'PDF export is Chromium only');
  console.log(browser.version); // 159.0.8154.6
});
  • browser.name: 'chromium' | 'firefox' | 'webkit', the same names web({ browser }) takes. Always 'chromium' for a browser attached over CDP (connect, any provider).
  • browser.version: the version the running browser reports through the driver.

Why

A test could tell it ran on web, but not on which browser or version. Suites moving from Cypress (Cypress.browser, Cypress.isBrowser), Playwright (browserName, browser.version()) and Selenium (getCapabilities()) branch and skip on that. Both values now come from the driver, so they are right for a launched, attached or hosted browser.

How it works

  1. PlaywrightSurface.browserIdentity() returns a reader of the name and version from the attempt's current connection (packages/web/src/surface.ts:913). It reads on every access, so a browser re-attached after a CDP transport drop answers for itself. The reader is tied to the session the fixture was made in: a fixture kept past its attempt throws INVALID_STATE, also once the next attempt has a browser of its own. The members of a serial group share one session, so each of them reads it. The reader also checks the fixture's own signal, so a fixture first made after its session closed (a @e2e-dev/core session makes them on first access) throws too.
  2. The fixture exposes them as getters (packages/web/src/browser.ts:827), typed on Browser (packages/web/src/browser.ts:350). They are properties, not recorded steps, and work before the first navigation.
  3. Docs: the reference entry, a skip example in the browser guide, rows in the Cypress, Playwright and Selenium migration tables, and the skill text.

Decisions:

  • On browser, not a shared fixture. Name and version exist only for a browser. Nothing on main reported identity to a test: platform is web, device has none, and the report's target record holds only the engine package and its version.
  • Same values as the trace. The trace page already prints browser <name> <version> from the same two driver calls (packages/web/src/trace-feed.ts:58), so a test and its trace agree.
  • Sync properties. The connection exists before a fixture is reachable, so test.skip(browser.name === 'firefox', reason) needs no await. platforms cannot do this declaratively because every browser target is web, so the migration rows point at the body form of test.skip.
  • The name is the driver's, not a brand. Chrome, Edge and a non-Chromium CDP server such as Lightpanda all report 'chromium'. Telling brands apart would mean parsing the product string. A test that needs it can read navigator.userAgentData.brands with browser.evaluate.

Left out:

  • OS and arch: process.platform and process.arch already answer for a local browser. For a hosted one CDP has no OS field, and parsing the user agent would be a guess; the docs point at navigator.platform through browser.evaluate.
  • Channel, headless flag, major version: derivable or not needed for the three frameworks' common use.
  • Browser name and version in report.json: a wire change, better as its own PR.

For #1503 to port: browserIdentity() is the only driver touch point. On BiDi the values are browserName and browserVersion from the session.new capabilities, mapped to the same three names so 'chromium' stays the answer for Chrome and Edge. platformName from the same response would let a later change expose the OS of a hosted browser without parsing anything.

Issue

ENG-1242

Verified

Ran it locally: yes

  • Testbed, built CLI, a scratch config with four targets (web(), web({ userAgent: 'Custom/1.0' }), web({ browser: 'firefox' }), web({ browser: 'webkit' })), e2e run tests/browser.e2e.ts. The testbed tests read the name and version before any page opens and skip by name. They do not read the page's user agent, which a userAgent override or a non-Chromium CDP server changes:
 ✓ |chromium| tests/browser.e2e.ts > the browser names its engine and reports a version before any page opens 233ms
 ✓ |chromium| tests/browser.e2e.ts > a test skips on the browsers it does not cover 6ms
 ✓ |custom-ua| tests/browser.e2e.ts > the browser names its engine and reports a version before any page opens 233ms
 ✓ |custom-ua| tests/browser.e2e.ts > a test skips on the browsers it does not cover 7ms
 ✓ |webkit| tests/browser.e2e.ts > the browser names its engine and reports a version before any page opens 335ms
 ↓ |webkit| tests/browser.e2e.ts > a test skips on the browsers it does not cover [covers Chromium only]
 ✓ |firefox| tests/browser.e2e.ts > the browser names its engine and reports a version before any page opens 627ms
 ↓ |firefox| tests/browser.e2e.ts > a test skips on the browsers it does not cover [covers Chromium only]
  • The same two tests on Lightpanda nightly 1.1.0-nightly.12455 (lightpanda() provider): both pass.
  • openHost on the testbed, a browser fixture kept from session A into session B:
A live: 159.0.8154.6
A closed: throws INVALID_STATE
B live, old fixture version: throws INVALID_STATE
B live, own fixture: 159.0.8154.6
  • launch(web()) from @e2e-dev/core, session C closed before C.browser was ever read, then session D opened:
C.browser.url(): rejects CANCELLED
C.browser.version (made late): throws INVALID_STATE
D.browser.version: 159.0.8154.6
  • A serial group whose two members read the identity (now in apps/testbed/tests/browser.e2e.ts), on Chromium, Firefox and WebKit. Before the fix the first member failed with APP_NOT_OPEN and the second was skipped:
 ✓ |chromium| tests/browser.e2e.ts > browser identity in a serial group > the first member reads the identity 1ms
 ✓ |chromium| tests/browser.e2e.ts > browser identity in a serial group > the second member reads the same browser 1ms
  • Attached to the installed Google Chrome 158.0.8127.88 with web({ connect, userAgent: 'Custom/1.0' }), while the bundled Chromium is 159. The test printed the attached browser's version, not the bundled one, and the user agent override did not change it:
IDENTITY chromium 158.0.8127.88
UA Custom/1.0
  • packages/web/tests/integration/browser-info.test.ts checks the version against navigator.userAgentData full version list (Chromium), the user agent (Firefox, WebKit) and the remote's /json/version (CDP attach). Returning a constant from browserIdentity() fails 9 of its 10 tests. cdp-recovery.test.ts keeps one fixture across a reconnect of the persistent context and checks the replacement browser is the one asked.
  • Not run: Moli and the hosted providers. They attach through the same connectCdp path as the Chrome attach above.
  • No benchmark table: the change adds two property reads and touches no action, locator or agent path.
  • No docs screenshots: the docs site does not build locally; pnpm docs:check passes.

In the same testbed run, the existing offline fails the page's requests... test fails on Firefox (browser.reload() resolves offline). This change does not touch that test or reload, and CI runs the testbed on Chromium only. I did not re-run it on main.