Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(playwright): Clear cookie by name #4693

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it's worth mentioning since Playwright 1.43 so that this won't surprise anyone when they are using playwright < 1.43 and this doesn't work for them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's edge case, and I hope everyone will use latest PW

```

@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
Loading