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 4 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
25 changes: 25 additions & 0 deletions test/helper/Playwright_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,31 @@ describe('Playwright', function () {
I.see('Information')
})
})

describe('#clearCookie', () => {
it('should clear all cookies', async () => {
I.amOnPage('/')
I.setCookie({ name: 'test', value: 'test' })
const cookiesBeforeClearing = await I.grabCookie()
assert.equal(cookiesBeforeClearing.length, 1)
I.clearCookie()
const cookiesAfterClearing = await I.grabCookie()
assert.equal(cookiesAfterClearing.length, 0)
})

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

const cookiesBeforeClearing = await I.grabCookie()
assert.equal(cookiesBeforeClearing.length, 2)
I.clearCookie('test1')
const cookiesAfterClearing = await I.grabCookie()
assert.equal(cookiesAfterClearing.length, 1)
assert.equal(cookiesAfterClearing, { name: 'test2', value: 'test2' })
})
})
})

let remoteBrowser
Expand Down
Loading