feat(web): filter browser.cookies() by URL (ENG-1502)

Merged
OK
4h ago
+230-61

What

browser.cookies(urls?) takes one URL or a list and returns only the cookies a request to them would carry. Without an argument it returns every cookie, as before.

const cookies = await browser.cookies('/api/orders');
const headers = { cookie: cookies.map((c) => `${c.name}=${c.value}`).join('; ') };
expect(await request.get('/api/orders', { headers })).toBeOK();
  • A cookie matches by domain (a host-only cookie only on its exact host, a .example.com cookie on subdomains too), by path (/admin matches /admin/users, not /administrator), and by Secure (https, or http on a loopback host).
  • A relative URL resolves against the base URL, as goto and a setCookies url do.
  • An empty list returns no cookies.
  • INVALID_ARGUMENT for a value that is not a string or a list of strings, POLICY_DENIED for a URL that is not http(s).

Why

The "Signed-in requests" recipe built the cookie header by picking one cookie by name and domain out of every cookie in the browser, other sites' included. It missed path scoping and apps with more than one cookie, and a looser pick could send a sign-in provider's cookie to the app. The recipe is now browser.cookies(url) for the URL the request goes to.

How it works

  1. browser.cookies(urls) resolves each URL through context.app.resolveUrl before the driver call, so a relative URL and the URL rule behave as in setCookies (packages/web/src/browser.ts:843, resolveCookieUrls in packages/web/src/cookies.ts:28). setCookies now shares the http(s) check through resolveCookieUrl (cookies.ts:19); its behavior is unchanged.
  2. The resolved list goes to the driver, context.cookies(urls), which does the matching. playwright-core 1.65.1 matches host-only and domain cookies, paths on a / boundary, and Secure with the loopback exception. Dropped alternative: a matcher of our own in cookies.ts, which would duplicate what the driver already does.
  3. An empty list returns [] without asking the driver, which reads [] as no filter. A list that a test computed and that came out empty must not widen to every site's cookies. This differs from Playwright, where context.cookies([]) returns everything.
  4. The step label is the URL as the test wrote it (cookieUrlsLabel, cookies.ts:40), like goto.
  5. Docs: the cookies reference entry and interface block, the recipe in api-testing.mdx and its example file, the Playwright and Cypress migrate rows, and the skill (plugins/ copy regenerated). Translations are not updated; their old recipe still works.

For #1503 to port: WebDriver BiDi's storage.getCookies has no URL filter, so that driver needs the matching itself, with the same cases as packages/web/tests/integration/cookies-url.test.ts: host-only against domain cookies, the path boundary, Secure with the loopback exception, the union over several URLs. The fixture side (resolveCookieUrls, the empty-list rule, the label) names no driver and carries over as is.

The provider packages (kernel, lightpanda, moli, notte, browser-run, smol, testmu, e2b) connect through @e2e-dev/web and get the same fixture. No other package implements browser.

Issue

ENG-1502

Verified

Ran it locally: yes

The built CLI on the testbed, tests/browser.e2e.ts, in Chromium and in Firefox (a scratch config with web({ browser: 'firefox' })):

✓ |web| tests/browser.e2e.ts > browser fixture > cookies set from the test reach the page and read back 95ms
✓ |web| tests/browser.e2e.ts > browser fixture > cookies(url) returns only the cookies a request to the URL carries 54ms

The failure path, from a scratch test:

× |web| tests/eng1502-fail.e2e.ts > refuses a non-URL
  INVALID_ARGUMENT: browser.cookies takes a URL or an array of URLs, got 42
× |web| tests/eng1502-fail.e2e.ts > refuses about:blank
  POLICY_DENIED: cookie URL must be http(s): about:blank
  • packages/web/tests/integration/cookies-url.test.ts: 36 passed, 12 cases each in Chromium, Firefox, and WebKit. With the URL argument dropped from the driver call, 27 of them fail.
  • A probe with a local server confirmed all three browsers send a Secure cookie to http://localhost and http://127.0.0.1, so the loopback exception matches what a request carries.
  • Emitted browser.d.ts reviewed; the type assertions are in packages/web/tests/types/clear-cookies.ts.
  • pnpm check, pnpm --filter @e2e-dev/web run test (1190 passed), pnpm --filter @e2e-dev/testbed test (619 passed).
  • No pnpm bench:ab: the change adds one optional argument to one fixture method and touches no shared path.
  • No docs screenshot: the docs site does not build locally. pnpm docs:check passes.
  • In Firefox the testbed's existing offline test fails (browser.reload() does not reject offline). This PR does not touch it, and CI runs the testbed in Chromium.

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.