chore: move filter params into the clearCookies (#30111)

This commit is contained in:
Pavel Feldman 2024-03-26 08:12:26 -07:00 committed by GitHub
parent 1824ba118e
commit 2de8a6b002
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 301 additions and 340 deletions

View File

@ -459,7 +459,71 @@ Returns the browser instance of the context. If it was launched as a persistent
## async method: BrowserContext.clearCookies
* since: v1.8
Clears context cookies.
Removes cookies from context. Accepts optional filter.
**Usage**
```js
await context.clearCookies();
await context.clearCookies({ name: 'session-id' });
await context.clearCookies({ domain: 'my-origin.com' });
await context.clearCookies({ domain: /.*my-origin\.com/ });
await context.clearCookies({ path: '/api/v1' });
await context.clearCookies({ name: 'session-id', domain: 'my-origin.com' });
```
```java
context.clearCookies();
context.clearCookies(new BrowserContext.ClearCookiesOptions().setName("session-id"));
context.clearCookies(new BrowserContext.ClearCookiesOptions().setDomain("my-origin.com"));
context.clearCookies(new BrowserContext.ClearCookiesOptions().setPath("/api/v1"));
context.clearCookies(new BrowserContext.ClearCookiesOptions()
.setName("session-id")
.setDomain("my-origin.com"));
```
```python async
await context.clear_cookies()
await context.clear_cookies(name="session-id")
await context.clear_cookies(domain="my-origin.com")
await context.clear_cookies(path="/api/v1")
await context.clear_cookies(name="session-id", domain="my-origin.com")
```
```python sync
context.clear_cookies()
context.clear_cookies(name="session-id")
context.clear_cookies(domain="my-origin.com")
context.clear_cookies(path="/api/v1")
context.clear_cookies(name="session-id", domain="my-origin.com")
```
```csharp
await context.ClearCookiesAsync();
await context.ClearCookiesAsync(new() { Name = "session-id" });
await context.ClearCookiesAsync(new() { Domain = "my-origin.com" });
await context.ClearCookiesAsync(new() { Path = "/api/v1" });
await context.ClearCookiesAsync(new() { Name = "session-id", Domain = "my-origin.com" });
```
### option: BrowserContext.clearCookies.name
* since: v1.43
- `name` <[string]|[RegExp]>
Only removes cookies with the given name.
### option: BrowserContext.clearCookies.domain
* since: v1.43
- `domain` <[string]|[RegExp]>
Only removes cookies with the given domain.
### option: BrowserContext.clearCookies.path
* since: v1.43
- `path` <[string]|[RegExp]>
Only removes cookies with the given path.
## async method: BrowserContext.clearPermissions
* since: v1.8
@ -1013,27 +1077,6 @@ Creates a new page in the browser context.
Returns all open pages in the context.
## async method: BrowserContext.removeCookies
* since: v1.43
Removes cookies from context. At least one of the removal criteria should be provided.
**Usage**
```js
await browserContext.removeCookies({ name: 'session-id' });
await browserContext.removeCookies({ domain: 'my-origin.com' });
await browserContext.removeCookies({ path: '/api/v1' });
await browserContext.removeCookies({ name: 'session-id', domain: 'my-origin.com' });
```
### param: BrowserContext.removeCookies.filter
* since: v1.43
- `filter` <[Object]>
- `name` ?<[string]>
- `domain` ?<[string]>
- `path` ?<[string]>
## property: BrowserContext.request
* since: v1.16
* langs:

View File

@ -265,12 +265,18 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
await this._channel.addCookies({ cookies });
}
async clearCookies(): Promise<void> {
await this._channel.clearCookies();
}
async removeCookies(filter: network.RemoveNetworkCookieParam): Promise<void> {
await this._channel.removeCookies({ filter });
async clearCookies(options: network.ClearNetworkCookieOptions = {}): Promise<void> {
await this._channel.clearCookies({
name: isString(options.name) ? options.name : undefined,
nameRegexSource: isRegExp(options.name) ? options.name.source : undefined,
nameRegexFlags: isRegExp(options.name) ? options.name.flags : undefined,
domain: isString(options.domain) ? options.domain : undefined,
domainRegexSource: isRegExp(options.domain) ? options.domain.source : undefined,
domainRegexFlags: isRegExp(options.domain) ? options.domain.flags : undefined,
path: isString(options.path) ? options.path : undefined,
pathRegexSource: isRegExp(options.path) ? options.path.source : undefined,
pathRegexFlags: isRegExp(options.path) ? options.path.flags : undefined,
});
}
async grantPermissions(permissions: string[], options?: { origin?: string }): Promise<void> {

View File

@ -58,10 +58,10 @@ export type SetNetworkCookieParam = {
sameSite?: 'Strict' | 'Lax' | 'None'
};
export type RemoveNetworkCookieParam = {
name?: string,
domain?: string,
path?: string,
export type ClearNetworkCookieOptions = {
name?: string | RegExp,
domain?: string | RegExp,
path?: string | RegExp,
};
type SerializedFallbackOverrides = {

View File

@ -826,16 +826,18 @@ scheme.BrowserContextAddInitScriptParams = tObject({
source: tString,
});
scheme.BrowserContextAddInitScriptResult = tOptional(tObject({}));
scheme.BrowserContextClearCookiesParams = tOptional(tObject({}));
scheme.BrowserContextClearCookiesResult = tOptional(tObject({}));
scheme.BrowserContextRemoveCookiesParams = tObject({
filter: tObject({
name: tOptional(tString),
domain: tOptional(tString),
path: tOptional(tString),
}),
scheme.BrowserContextClearCookiesParams = tObject({
name: tOptional(tString),
nameRegexSource: tOptional(tString),
nameRegexFlags: tOptional(tString),
domain: tOptional(tString),
domainRegexSource: tOptional(tString),
domainRegexFlags: tOptional(tString),
path: tOptional(tString),
pathRegexSource: tOptional(tString),
pathRegexFlags: tOptional(tString),
});
scheme.BrowserContextRemoveCookiesResult = tOptional(tObject({}));
scheme.BrowserContextClearCookiesResult = tOptional(tObject({}));
scheme.BrowserContextClearPermissionsParams = tOptional(tObject({}));
scheme.BrowserContextClearPermissionsResult = tOptional(tObject({}));
scheme.BrowserContextCloseParams = tObject({

View File

@ -251,7 +251,6 @@ export abstract class BrowserContext extends SdkObject {
abstract pages(): Page[];
abstract newPageDelegate(): Promise<PageDelegate>;
abstract addCookies(cookies: channels.SetNetworkCookie[]): Promise<void>;
abstract clearCookies(): Promise<void>;
abstract setGeolocation(geolocation?: types.Geolocation): Promise<void>;
abstract setExtraHTTPHeaders(headers: types.HeadersArray): Promise<void>;
abstract setUserAgent(userAgent: string | undefined): Promise<void>;
@ -259,6 +258,7 @@ export abstract class BrowserContext extends SdkObject {
abstract cancelDownload(uuid: string): Promise<void>;
abstract clearCache(): Promise<void>;
protected abstract doGetCookies(urls: string[]): Promise<channels.NetworkCookie[]>;
protected abstract doClearCookies(): Promise<void>;
protected abstract doGrantPermissions(origin: string, permissions: string[]): Promise<void>;
protected abstract doClearPermissions(): Promise<void>;
protected abstract doSetHTTPCredentials(httpCredentials?: types.Credentials): Promise<void>;
@ -276,20 +276,27 @@ export abstract class BrowserContext extends SdkObject {
return await this.doGetCookies(urls as string[]);
}
async removeCookies(filter: {name?: string, domain?: string, path?: string}): Promise<void> {
if (!filter.name && !filter.domain && !filter.path)
throw new Error(`Either name, domain or path are required`);
async clearCookies(options: {name?: string | RegExp, domain?: string | RegExp, path?: string | RegExp}): Promise<void> {
const currentCookies = await this.cookies();
await this.doClearCookies();
const cookiesToKeep = currentCookies.filter(cookie => {
return !((!filter.name || filter.name === cookie.name) &&
(!filter.domain || filter.domain === cookie.domain) &&
(!filter.path || filter.path === cookie.path));
const matches = (cookie: channels.NetworkCookie, prop: 'name' | 'domain' | 'path', value: string | RegExp | undefined) => {
if (!value)
return true;
if (value instanceof RegExp) {
value.lastIndex = 0;
return value.test(cookie[prop]);
}
return cookie[prop] === value;
};
const cookiesToReadd = currentCookies.filter(cookie => {
return !matches(cookie, 'name', options.name)
|| !matches(cookie, 'domain', options.domain)
|| !matches(cookie, 'path', options.path);
});
await this.clearCookies();
await this.addCookies(cookiesToKeep);
await this.addCookies(cookiesToReadd);
}
setHTTPCredentials(httpCredentials?: types.Credentials): Promise<void> {
@ -559,7 +566,7 @@ export abstract class BrowserContext extends SdkObject {
}
async _resetCookies() {
await this.clearCookies();
await this.doClearCookies();
if (this._options.storageState?.cookies)
await this.addCookies(this._options.storageState?.cookies);
}

View File

@ -411,7 +411,7 @@ export class CRBrowserContext extends BrowserContext {
await this._browser._session.send('Storage.setCookies', { cookies: network.rewriteCookies(cookies), browserContextId: this._browserContextId });
}
async clearCookies() {
async doClearCookies() {
await this._browser._session.send('Storage.clearCookies', { browserContextId: this._browserContextId });
}

View File

@ -220,12 +220,15 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
await this._context.addCookies(params.cookies);
}
async clearCookies(): Promise<void> {
await this._context.clearCookies();
}
async removeCookies(params: channels.BrowserContextRemoveCookiesParams): Promise<void> {
await this._context.removeCookies(params.filter);
async clearCookies(params: channels.BrowserContextClearCookiesParams): Promise<void> {
const nameRe = params.nameRegexSource !== undefined && params.nameRegexFlags !== undefined ? new RegExp(params.nameRegexSource, params.nameRegexFlags) : undefined;
const domainRe = params.domainRegexSource !== undefined && params.domainRegexFlags !== undefined ? new RegExp(params.domainRegexSource, params.domainRegexFlags) : undefined;
const pathRe = params.pathRegexSource !== undefined && params.pathRegexFlags !== undefined ? new RegExp(params.pathRegexSource, params.pathRegexFlags) : undefined;
await this._context.clearCookies({
name: nameRe || params.name,
domain: domainRe || params.domain,
path: pathRe || params.path,
});
}
async grantPermissions(params: channels.BrowserContextGrantPermissionsParams): Promise<void> {

View File

@ -295,7 +295,7 @@ export class FFBrowserContext extends BrowserContext {
await this._browser.session.send('Browser.setCookies', { browserContextId: this._browserContextId, cookies: cc });
}
async clearCookies() {
async doClearCookies() {
await this._browser.session.send('Browser.clearCookies', { browserContextId: this._browserContextId });
}

View File

@ -272,7 +272,7 @@ export class WKBrowserContext extends BrowserContext {
await this._browser._browserSession.send('Playwright.setCookies', { cookies: cc, browserContextId: this._browserContextId });
}
async clearCookies() {
async doClearCookies() {
await this._browser._browserSession.send('Playwright.deleteAllCookies', { browserContextId: this._browserContextId });
}

View File

@ -8317,9 +8317,37 @@ export interface BrowserContext {
browser(): null|Browser;
/**
* Clears context cookies.
* Removes cookies from context. Accepts optional filter.
*
* **Usage**
*
* ```js
* await context.clearCookies();
* await context.clearCookies({ name: 'session-id' });
* await context.clearCookies({ domain: 'my-origin.com' });
* await context.clearCookies({ domain: /.*my-origin\.com/ });
* await context.clearCookies({ path: '/api/v1' });
* await context.clearCookies({ name: 'session-id', domain: 'my-origin.com' });
* ```
*
* @param options
*/
clearCookies(): Promise<void>;
clearCookies(options?: {
/**
* Only removes cookies with the given domain.
*/
domain?: string|RegExp;
/**
* Only removes cookies with the given name.
*/
name?: string|RegExp;
/**
* Only removes cookies with the given path.
*/
path?: string|RegExp;
}): Promise<void>;
/**
* Clears all permission overrides for the browser context.
@ -8445,28 +8473,6 @@ export interface BrowserContext {
*/
pages(): Array<Page>;
/**
* Removes cookies from context. At least one of the removal criteria should be provided.
*
* **Usage**
*
* ```js
* await browserContext.removeCookies({ name: 'session-id' });
* await browserContext.removeCookies({ domain: 'my-origin.com' });
* await browserContext.removeCookies({ path: '/api/v1' });
* await browserContext.removeCookies({ name: 'session-id', domain: 'my-origin.com' });
* ```
*
* @param filter
*/
removeCookies(filter: {
name?: string;
domain?: string;
path?: string;
}): Promise<void>;
/**
* Routing provides the capability to modify network requests that are made by any page in the browser context. Once
* route is enabled, every request matching the url pattern will stall unless it's continued, fulfilled or aborted.

View File

@ -1426,8 +1426,7 @@ export interface BrowserContextChannel extends BrowserContextEventTarget, EventT
_type_BrowserContext: boolean;
addCookies(params: BrowserContextAddCookiesParams, metadata?: CallMetadata): Promise<BrowserContextAddCookiesResult>;
addInitScript(params: BrowserContextAddInitScriptParams, metadata?: CallMetadata): Promise<BrowserContextAddInitScriptResult>;
clearCookies(params?: BrowserContextClearCookiesParams, metadata?: CallMetadata): Promise<BrowserContextClearCookiesResult>;
removeCookies(params: BrowserContextRemoveCookiesParams, metadata?: CallMetadata): Promise<BrowserContextRemoveCookiesResult>;
clearCookies(params: BrowserContextClearCookiesParams, metadata?: CallMetadata): Promise<BrowserContextClearCookiesResult>;
clearPermissions(params?: BrowserContextClearPermissionsParams, metadata?: CallMetadata): Promise<BrowserContextClearPermissionsResult>;
close(params: BrowserContextCloseParams, metadata?: CallMetadata): Promise<BrowserContextCloseResult>;
cookies(params: BrowserContextCookiesParams, metadata?: CallMetadata): Promise<BrowserContextCookiesResult>;
@ -1521,20 +1520,29 @@ export type BrowserContextAddInitScriptOptions = {
};
export type BrowserContextAddInitScriptResult = void;
export type BrowserContextClearCookiesParams = {};
export type BrowserContextClearCookiesOptions = {};
export type BrowserContextClearCookiesParams = {
name?: string,
nameRegexSource?: string,
nameRegexFlags?: string,
domain?: string,
domainRegexSource?: string,
domainRegexFlags?: string,
path?: string,
pathRegexSource?: string,
pathRegexFlags?: string,
};
export type BrowserContextClearCookiesOptions = {
name?: string,
nameRegexSource?: string,
nameRegexFlags?: string,
domain?: string,
domainRegexSource?: string,
domainRegexFlags?: string,
path?: string,
pathRegexSource?: string,
pathRegexFlags?: string,
};
export type BrowserContextClearCookiesResult = void;
export type BrowserContextRemoveCookiesParams = {
filter: {
name?: string,
domain?: string,
path?: string,
},
};
export type BrowserContextRemoveCookiesOptions = {
};
export type BrowserContextRemoveCookiesResult = void;
export type BrowserContextClearPermissionsParams = {};
export type BrowserContextClearPermissionsOptions = {};
export type BrowserContextClearPermissionsResult = void;

View File

@ -1031,15 +1031,16 @@ BrowserContext:
source: string
clearCookies:
removeCookies:
parameters:
filter:
type: object
properties:
name: string?
domain: string?
path: string?
name: string?
nameRegexSource: string?
nameRegexFlags: string?
domain: string?
domainRegexSource: string?
domainRegexFlags: string?
path: string?
pathRegexSource: string?
pathRegexFlags: string?
clearPermissions:

View File

@ -48,3 +48,119 @@ it('should isolate cookies when clearing', async ({ context, server, browser })
expect((await anotherContext.cookies()).length).toBe(0);
await anotherContext.close();
});
it('should remove cookies by name', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/',
},
{
name: 'cookie2',
value: '2',
domain: new URL(server.PREFIX).hostname,
path: '/',
}
]);
await page.goto(server.PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie2=2');
await context.clearCookies({ name: 'cookie1' });
expect(await page.evaluate('document.cookie')).toBe('cookie2=2');
});
it('should remove cookies by name regex', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/',
},
{
name: 'cookie2',
value: '2',
domain: new URL(server.PREFIX).hostname,
path: '/',
}
]);
await page.goto(server.PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie2=2');
await context.clearCookies({ name: /coo.*1/ });
expect(await page.evaluate('document.cookie')).toBe('cookie2=2');
});
it('should remove cookies by domain', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/',
},
{
name: 'cookie2',
value: '2',
domain: new URL(server.CROSS_PROCESS_PREFIX).hostname,
path: '/',
}
]);
await page.goto(server.PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
await page.goto(server.CROSS_PROCESS_PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie2=2');
await context.clearCookies({ domain: new URL(server.CROSS_PROCESS_PREFIX).hostname });
expect(await page.evaluate('document.cookie')).toBe('');
await page.goto(server.PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
});
it('should remove cookies by path', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/api/v1',
},
{
name: 'cookie2',
value: '2',
domain: new URL(server.PREFIX).hostname,
path: '/api/v2',
},
{
name: 'cookie3',
value: '3',
domain: new URL(server.PREFIX).hostname,
path: '/',
}
]);
await page.goto(server.PREFIX + '/api/v1');
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie3=3');
await context.clearCookies({ path: '/api/v1' });
expect(await page.evaluate('document.cookie')).toBe('cookie3=3');
await page.goto(server.PREFIX + '/api/v2');
expect(await page.evaluate('document.cookie')).toBe('cookie2=2; cookie3=3');
await page.goto(server.PREFIX + '/');
expect(await page.evaluate('document.cookie')).toBe('cookie3=3');
});
it('should remove cookies by name and domain', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/',
},
{
name: 'cookie1',
value: '1',
domain: new URL(server.CROSS_PROCESS_PREFIX).hostname,
path: '/',
}
]);
await page.goto(server.PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
await context.clearCookies({ name: 'cookie1', domain: new URL(server.PREFIX).hostname });
expect(await page.evaluate('document.cookie')).toBe('');
await page.goto(server.CROSS_PROCESS_PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
});

View File

@ -1,231 +0,0 @@
/**
* Copyright 2018 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { contextTest as it, expect } from '../config/browserTest';
it('should remove cookies by name', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/',
},
{
name: 'cookie2',
value: '2',
domain: new URL(server.PREFIX).hostname,
path: '/',
}
]);
await page.goto(server.PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie2=2');
await context.removeCookies({ name: 'cookie1' });
expect(await page.evaluate('document.cookie')).toBe('cookie2=2');
});
it('should remove cookies by domain', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/',
},
{
name: 'cookie2',
value: '2',
domain: new URL(server.CROSS_PROCESS_PREFIX).hostname,
path: '/',
}
]);
await page.goto(server.PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
await page.goto(server.CROSS_PROCESS_PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie2=2');
await context.removeCookies({ domain: new URL(server.CROSS_PROCESS_PREFIX).hostname });
expect(await page.evaluate('document.cookie')).toBe('');
await page.goto(server.PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
});
it('should remove cookies by path', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/api/v1',
},
{
name: 'cookie2',
value: '2',
domain: new URL(server.PREFIX).hostname,
path: '/api/v2',
},
{
name: 'cookie3',
value: '3',
domain: new URL(server.PREFIX).hostname,
path: '/',
}
]);
await page.goto(server.PREFIX + '/api/v1');
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie3=3');
await context.removeCookies({ path: '/api/v1' });
expect(await page.evaluate('document.cookie')).toBe('cookie3=3');
await page.goto(server.PREFIX + '/api/v2');
expect(await page.evaluate('document.cookie')).toBe('cookie2=2; cookie3=3');
await page.goto(server.PREFIX + '/');
expect(await page.evaluate('document.cookie')).toBe('cookie3=3');
});
it('should remove cookies by name and domain', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/',
},
{
name: 'cookie1',
value: '1',
domain: new URL(server.CROSS_PROCESS_PREFIX).hostname,
path: '/',
}
]);
await page.goto(server.PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
await context.removeCookies({ name: 'cookie1', domain: new URL(server.PREFIX).hostname });
expect(await page.evaluate('document.cookie')).toBe('');
await page.goto(server.CROSS_PROCESS_PREFIX);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
});
it('should remove cookies by name and path', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/api/v1',
},
{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/api/v2',
},
{
name: 'cookie3',
value: '3',
domain: new URL(server.PREFIX).hostname,
path: '/',
}
]);
await page.goto(server.PREFIX + '/api/v1');
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie3=3');
await context.removeCookies({ name: 'cookie1', path: '/api/v1' });
expect(await page.evaluate('document.cookie')).toBe('cookie3=3');
await page.goto(server.PREFIX + '/api/v2');
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie3=3');
await page.goto(server.PREFIX + '/');
expect(await page.evaluate('document.cookie')).toBe('cookie3=3');
});
it('should remove cookies by domain and path', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/api/v1',
},
{
name: 'cookie2',
value: '2',
domain: new URL(server.PREFIX).hostname,
path: '/api/v2',
},
{
name: 'cookie3',
value: '3',
domain: new URL(server.CROSS_PROCESS_PREFIX).hostname,
path: '/api/v1',
},
{
name: 'cookie4',
value: '4',
domain: new URL(server.CROSS_PROCESS_PREFIX).hostname,
path: '/api/v2',
}
]);
await page.goto(server.PREFIX + '/api/v1');
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
await context.removeCookies({ domain: new URL(server.PREFIX).hostname, path: '/api/v1' });
expect(await page.evaluate('document.cookie')).toBe('');
await page.goto(server.PREFIX + '/api/v2');
expect(await page.evaluate('document.cookie')).toBe('cookie2=2');
await page.goto(server.CROSS_PROCESS_PREFIX + '/api/v2');
expect(await page.evaluate('document.cookie')).toBe('cookie4=4');
});
it('should remove cookies by name, domain and path', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/api/v1',
},
{
name: 'cookie2',
value: '2',
domain: new URL(server.PREFIX).hostname,
path: '/api/v2',
},
{
name: 'cookie1',
value: '1',
domain: new URL(server.CROSS_PROCESS_PREFIX).hostname,
path: '/api/v1',
},
]);
await page.goto(server.PREFIX + '/api/v1');
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
await context.removeCookies({ name: 'cookie1', domain: new URL(server.PREFIX).hostname, path: '/api/v1' });
expect(await page.evaluate('document.cookie')).toBe('');
await page.goto(server.PREFIX + '/api/v2');
expect(await page.evaluate('document.cookie')).toBe('cookie2=2');
await page.goto(server.CROSS_PROCESS_PREFIX + '/api/v1');
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
});
it('should throw if empty object is passed', async ({ context, page, server }) => {
await context.addCookies([{
name: 'cookie1',
value: '1',
domain: new URL(server.PREFIX).hostname,
path: '/',
},
{
name: 'cookie2',
value: '2',
domain: new URL(server.PREFIX).hostname,
path: '/',
},
]);
await page.goto(server.PREFIX + '/');
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie2=2');
const error = await context.removeCookies({ }).catch(e => e);
expect(error.message).toContain(`Either name, domain or path are required`);
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie2=2');
});