feat(web): filter browser.cookies() by URL (ENG-1502)
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.comcookie on subdomains too), by path (/adminmatches/admin/users, not/administrator), and bySecure(https, orhttpon a loopback host). - A relative URL resolves against the base URL, as
gotoand asetCookiesurldo. - An empty list returns no cookies.
INVALID_ARGUMENTfor a value that is not a string or a list of strings,POLICY_DENIEDfor a URL that is nothttp(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
browser.cookies(urls)resolves each URL throughcontext.app.resolveUrlbefore the driver call, so a relative URL and the URL rule behave as insetCookies(packages/web/src/browser.ts:843,resolveCookieUrlsinpackages/web/src/cookies.ts:28).setCookiesnow shares the http(s) check throughresolveCookieUrl(cookies.ts:19); its behavior is unchanged.- 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, andSecurewith the loopback exception. Dropped alternative: a matcher of our own incookies.ts, which would duplicate what the driver already does. - 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, wherecontext.cookies([])returns everything. - The step label is the URL as the test wrote it (
cookieUrlsLabel,cookies.ts:40), likegoto. - Docs: the
cookiesreference entry and interface block, the recipe inapi-testing.mdxand 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
Securecookie tohttp://localhostandhttp://127.0.0.1, so the loopback exception matches what a request carries. - Emitted
browser.d.tsreviewed; the type assertions are inpackages/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:checkpasses. - 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/**/*.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.