Implementation:Microsoft Playwright CookieStore
| Knowledge Sources | |
|---|---|
| Domains | Server, Cookies |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for managing browser cookies in-memory with domain/path matching and expiration handling provided by the Playwright library.
Description
The module defines two classes: `Cookie` and `CookieStore`. The `Cookie` class wraps a `channels.NetworkCookie` and provides URL matching (domain, path, secure checks per RFC 6265), equality comparison, and expiration checking. The `CookieStore` class maintains a `Map<string, Set<Cookie>>` keyed by cookie name, implementing `addCookies` (with deduplication per RFC 6265 Section 5.3), `cookies(url)` for URL-filtered retrieval, `allCookies()` for listing, and `removeCookies`. Expired cookies are lazily pruned during iteration. The `domainMatches` and `pathMatches` helper functions implement standard cookie matching rules.
Usage
Use CookieStore when Playwright needs to maintain an in-memory cookie jar, such as for route-based cookie management or when browser-level cookie APIs are insufficient.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/server/cookieStore.ts
Signature
export class Cookie {
constructor(data: channels.NetworkCookie);
name(): string;
matches(url: URL): boolean;
equals(other: Cookie): boolean;
networkCookie(): channels.NetworkCookie;
expired(): boolean;
}
export class CookieStore {
addCookies(cookies: channels.NetworkCookie[]): void;
cookies(url: URL): channels.NetworkCookie[];
allCookies(): channels.NetworkCookie[];
}
Import
import { Cookie, CookieStore } from '../server/cookieStore';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| cookies | channels.NetworkCookie[] | Yes | Array of cookie data to add to the store |
| url | URL | Yes (cookies) | URL to filter cookies by domain/path matching |
Outputs
| Name | Type | Description |
|---|---|---|
| cookies | channels.NetworkCookie[] | Array of matching cookies for the given URL |
| allCookies | channels.NetworkCookie[] | All non-expired cookies in the store |
Usage Examples
import { CookieStore } from '../server/cookieStore';
const store = new CookieStore();
store.addCookies([{
name: 'session',
value: 'abc123',
domain: '.example.com',
path: '/',
expires: -1,
httpOnly: false,
secure: false,
sameSite: 'Lax',
}]);
const cookies = store.cookies(new URL('https://example.com/page'));
// Returns the 'session' cookie