Overview
CdpCookieProvider implements the CookieProvider interface using the Chrome DevTools Protocol to get, set, and delete browser cookies directly, bypassing the proxy-based cookie handling used in non-native automation mode.
Description
CdpCookieProvider extends CookieProviderBase and fulfils the CookieProvider contract. It obtains the active CDP client from the current browser connection and issues Storage.getCookies, Network.setCookies, Network.deleteCookies, and Network.clearBrowserCookies calls as needed.
Key behaviours:
- getCookies retrieves all cookies via
Storage.getCookies, then filters them using matchCollection against the provided ExternalCookies[] filter criteria. When URLs are supplied, their hostname and pathname are extracted and used as additional domain/path constraints.
- setCookies accepts cookie strings (parsed via
set-cookie-parser) or CookieOptions objects. Each cookie is converted to a CookieParam with sensible defaults (the URL's hostname/pathname when domain/path are missing, and MAX_TIMESTAMP for expiry when unset).
- deleteCookies with no arguments calls
Network.clearBrowserCookies to wipe all cookies. When given specific cookie filters, it first retrieves existing cookies, filters by URL and cookie criteria, then deletes each match individually.
- getCookieHeader builds a semicolon-separated cookie header string for a given URL, suitable for injecting into outbound requests.
- initialize clears all browser cookies at the start of a test run to ensure a clean state.
Usage
Use CdpCookieProvider when TestCafe runs in native automation mode and tests call the t.getCookies(), t.setCookies(), or t.deleteCookies() API methods. The provider is instantiated by the test-run cookie subsystem when native automation is active.
Code Reference
Source Location
Signature
export class CdpCookieProvider extends CookieProviderBase implements CookieProvider {
private async _getCdpClient (): Promise<remoteChrome.ProtocolApi>;
async initialize (): Promise<void>;
async getCookies (
externalCookies: ExternalCookies[],
urls?: string[],
): Promise<ExternalCookies[]>;
async setCookies (
cookies: string | string[] | CookieOptions[],
url: string,
): Promise<void>;
async deleteCookies (
cookies?: CookieOptions[],
urls?: string[],
): Promise<void>;
async getCookieHeader (url: string): Promise<string | null>;
}
Import
import { CdpCookieProvider } from '../native-automation/cookie-provider';
I/O Contract
getCookies
| Parameter |
Type |
Description
|
externalCookies |
ExternalCookies[] |
Filter criteria (name, domain, path, etc.); empty array returns all cookies
|
urls |
string[] (optional) |
URLs whose hostname/path constrain the cookie lookup
|
| Return Type |
Description
|
Promise<ExternalCookies[]> |
Matching cookies converted from CDP Network.Cookie format
|
setCookies
| Parameter |
Type |
Description
|
cookies |
string | string[] | CookieOptions[] |
Cookies to set; strings are parsed via set-cookie-parser
|
url |
string |
The URL context providing default hostname and pathname
|
| Return Type |
Description
|
Promise<void> |
Cookies are set in the browser via Network.setCookies
|
deleteCookies
| Parameter |
Type |
Description
|
cookies |
CookieOptions[] (optional) |
Specific cookies to delete; if empty, all cookies are cleared
|
urls |
string[] (optional) |
URLs to constrain which cookies are eligible for deletion
|
| Return Type |
Description
|
Promise<void> |
Matching cookies are removed from the browser
|
| Parameter |
Type |
Description
|
url |
string |
URL for which to build the cookie header
|
| Return Type |
Description
|
Promise<string | null> |
Semicolon-separated name=value pairs, e.g. "sid=abc;token=xyz"
|
Usage Examples
import { CdpCookieProvider } from '../native-automation/cookie-provider';
// The provider is constructed with a test run context
const cookieProvider = new CdpCookieProvider(testRun);
// Initialize (clears all existing cookies)
await cookieProvider.initialize();
// Set cookies from a CookieOptions object
await cookieProvider.setCookies(
[{ name: 'session', value: 'abc123', domain: 'example.com', path: '/' }],
'https://example.com/',
);
// Set cookies from Set-Cookie header strings
await cookieProvider.setCookies(
'theme=dark; Path=/; Secure',
'https://example.com/',
);
// Get all cookies matching a filter
const cookies = await cookieProvider.getCookies(
[{ name: 'session' }],
['https://example.com/'],
);
// Build a cookie header for an outgoing request
const header = await cookieProvider.getCookieHeader('https://example.com/api/data');
// Delete specific cookies
await cookieProvider.deleteCookies(
[{ name: 'session' }],
['https://example.com/'],
);
// Delete all cookies
await cookieProvider.deleteCookies();
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.