Skip to content

Commit 985b229

Browse files
ngrafNorbert Graf
and
Norbert Graf
authored
feat(playwright): Clear cookie by name (#4693)
* 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]>
1 parent cf299a2 commit 985b229

File tree

9 files changed

+39
-11
lines changed

9 files changed

+39
-11
lines changed

docs/helpers/Nightmare.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ if none provided clears all cookies.
148148

149149
```js
150150
I.clearCookie();
151-
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
151+
I.clearCookie('test');
152152
```
153153

154154
#### Parameters

docs/helpers/Playwright.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ if none provided clears all cookies.
546546

547547
```js
548548
I.clearCookie();
549-
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
549+
I.clearCookie('test');
550550
```
551551

552552
#### Parameters

docs/helpers/Protractor.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ if none provided clears all cookies.
267267

268268
```js
269269
I.clearCookie();
270-
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
270+
I.clearCookie('test');
271271
```
272272

273273
#### Parameters

docs/helpers/Puppeteer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ if none provided clears all cookies.
398398

399399
```js
400400
I.clearCookie();
401-
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
401+
I.clearCookie('test');
402402
```
403403

404404
#### Parameters

docs/helpers/TestCafe.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ if none provided clears all cookies.
198198

199199
```js
200200
I.clearCookie();
201-
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
201+
I.clearCookie('test');
202202
```
203203

204204
#### Parameters

docs/helpers/WebDriver.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ if none provided clears all cookies.
604604

605605
```js
606606
I.clearCookie();
607-
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
607+
I.clearCookie('test');
608608
```
609609

610610
#### Parameters

docs/webapi/clearCookie.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ if none provided clears all cookies.
33

44
```js
55
I.clearCookie();
6-
I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
6+
I.clearCookie('test');
77
```
88

99
@param {?string} [cookie=null] (optional, `null` by default) cookie name

lib/helper/Playwright.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2023,10 +2023,11 @@ class Playwright extends Helper {
20232023
/**
20242024
* {{> clearCookie }}
20252025
*/
2026-
async clearCookie() {
2027-
// Playwright currently doesn't support to delete a certain cookie
2028-
// https://github.com/microsoft/playwright/blob/main/docs/src/api/class-browsercontext.md#async-method-browsercontextclearcookies
2026+
async clearCookie(cookieName) {
20292027
if (!this.browserContext) return
2028+
if (cookieName) {
2029+
return this.browserContext.clearCookies({name: cookieName})
2030+
}
20302031
return this.browserContext.clearCookies()
20312032
}
20322033

test/helper/Playwright_test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -1081,9 +1081,36 @@ describe('Playwright', function () {
10811081
I.see('Information');
10821082
});
10831083
});
1084-
});
1084+
1085+
describe('#clearCookie', () => {
1086+
it('should clear all cookies', async () => {
1087+
await I.amOnPage('/')
1088+
1089+
await I.setCookie({ name: 'test', value: 'test', url: siteUrl})
1090+
const cookiesBeforeClearing = await I.grabCookie()
1091+
assert.isAtLeast(cookiesBeforeClearing.length, 1)
1092+
await I.clearCookie()
1093+
const cookiesAfterClearing = await I.grabCookie()
1094+
assert.equal(cookiesAfterClearing.length, 0)
1095+
})
1096+
1097+
it('should clear individual cookie by name', async () => {
1098+
await I.amOnPage('/')
1099+
await I.setCookie({ name: 'test1', value: 'test1', url: siteUrl })
1100+
await I.setCookie({ name: 'test2', value: 'test2', url: siteUrl })
1101+
1102+
const cookiesBeforeRemovingSpecificCookie = await I.grabCookie()
1103+
// info: we use "atLeast" instead of "isEqual" here because other random cookies might be set by the page to test.
1104+
assert.isAtLeast(cookiesBeforeRemovingSpecificCookie.length, 2)
1105+
await I.clearCookie('test1')
1106+
const cookiesAfterRemovingSpecificCookie = await I.grabCookie()
1107+
assert.equal(cookiesAfterRemovingSpecificCookie.length, cookiesBeforeRemovingSpecificCookie.length - 1)
1108+
})
1109+
})
1110+
})
10851111

10861112
let remoteBrowser;
1113+
10871114
async function createRemoteBrowser() {
10881115
if (remoteBrowser) {
10891116
await remoteBrowser.close();

0 commit comments

Comments
 (0)