Skip to content

Commit

Permalink
feat(playwright): Clear cookie by name (#4693)
Browse files Browse the repository at this point in the history
* feature: clear individual cookie by name with Playwright (available since Playwright 1.43)

* feature: clear individual cookie by name with Playwright (available since Playwright 1.43)

* feature: clear individual cookie by name with Playwright (available since Playwright 1.43)

* feature: clear individual cookie by name with Playwright (available since Playwright 1.43)

* feature: clear individual cookie by name with Playwright (available since Playwright 1.43)

---------

Co-authored-by: Norbert Graf <[email protected]>
  • Loading branch information
ngraf and Norbert Graf authored Jan 2, 2025
1 parent cf299a2 commit 985b229
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/helpers/Nightmare.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ if none provided clears all cookies.

```js
I.clearCookie();
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
I.clearCookie('test');
```

#### Parameters
Expand Down
2 changes: 1 addition & 1 deletion docs/helpers/Playwright.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ if none provided clears all cookies.

```js
I.clearCookie();
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
I.clearCookie('test');
```

#### Parameters
Expand Down
2 changes: 1 addition & 1 deletion docs/helpers/Protractor.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ if none provided clears all cookies.

```js
I.clearCookie();
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
I.clearCookie('test');
```

#### Parameters
Expand Down
2 changes: 1 addition & 1 deletion docs/helpers/Puppeteer.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ if none provided clears all cookies.

```js
I.clearCookie();
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
I.clearCookie('test');
```

#### Parameters
Expand Down
2 changes: 1 addition & 1 deletion docs/helpers/TestCafe.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ if none provided clears all cookies.

```js
I.clearCookie();
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
I.clearCookie('test');
```

#### Parameters
Expand Down
2 changes: 1 addition & 1 deletion docs/helpers/WebDriver.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ if none provided clears all cookies.

```js
I.clearCookie();
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
I.clearCookie('test');
```

#### Parameters
Expand Down
2 changes: 1 addition & 1 deletion docs/webapi/clearCookie.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ if none provided clears all cookies.

```js
I.clearCookie();
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
I.clearCookie('test');
```

@param {?string} [cookie=null] (optional, `null` by default) cookie name
7 changes: 4 additions & 3 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -2023,10 +2023,11 @@ class Playwright extends Helper {
/**
* {{> clearCookie }}
*/
async clearCookie() {
// Playwright currently doesn't support to delete a certain cookie
// https://github.com/microsoft/playwright/blob/main/docs/src/api/class-browsercontext.md#async-method-browsercontextclearcookies
async clearCookie(cookieName) {
if (!this.browserContext) return
if (cookieName) {
return this.browserContext.clearCookies({name: cookieName})
}
return this.browserContext.clearCookies()
}

Expand Down
29 changes: 28 additions & 1 deletion test/helper/Playwright_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1081,9 +1081,36 @@ describe('Playwright', function () {
I.see('Information');
});
});
});

describe('#clearCookie', () => {
it('should clear all cookies', async () => {
await I.amOnPage('/')

await I.setCookie({ name: 'test', value: 'test', url: siteUrl})
const cookiesBeforeClearing = await I.grabCookie()
assert.isAtLeast(cookiesBeforeClearing.length, 1)
await I.clearCookie()
const cookiesAfterClearing = await I.grabCookie()
assert.equal(cookiesAfterClearing.length, 0)
})

it('should clear individual cookie by name', async () => {
await I.amOnPage('/')
await I.setCookie({ name: 'test1', value: 'test1', url: siteUrl })
await I.setCookie({ name: 'test2', value: 'test2', url: siteUrl })

const cookiesBeforeRemovingSpecificCookie = await I.grabCookie()
// info: we use "atLeast" instead of "isEqual" here because other random cookies might be set by the page to test.
assert.isAtLeast(cookiesBeforeRemovingSpecificCookie.length, 2)
await I.clearCookie('test1')
const cookiesAfterRemovingSpecificCookie = await I.grabCookie()
assert.equal(cookiesAfterRemovingSpecificCookie.length, cookiesBeforeRemovingSpecificCookie.length - 1)
})
})
})

let remoteBrowser;

async function createRemoteBrowser() {
if (remoteBrowser) {
await remoteBrowser.close();
Expand Down

0 comments on commit 985b229

Please sign in to comment.