diff --git a/docs/helpers/Nightmare.md b/docs/helpers/Nightmare.md index ce1deb302..12727861e 100644 --- a/docs/helpers/Nightmare.md +++ b/docs/helpers/Nightmare.md @@ -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 diff --git a/docs/helpers/Playwright.md b/docs/helpers/Playwright.md index 9f7c9da4f..8e12c4054 100644 --- a/docs/helpers/Playwright.md +++ b/docs/helpers/Playwright.md @@ -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 diff --git a/docs/helpers/Protractor.md b/docs/helpers/Protractor.md index f70d2893d..f7847d683 100644 --- a/docs/helpers/Protractor.md +++ b/docs/helpers/Protractor.md @@ -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 diff --git a/docs/helpers/Puppeteer.md b/docs/helpers/Puppeteer.md index ea7a3e161..3a7702dde 100644 --- a/docs/helpers/Puppeteer.md +++ b/docs/helpers/Puppeteer.md @@ -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 diff --git a/docs/helpers/TestCafe.md b/docs/helpers/TestCafe.md index d75af3f59..fe1191408 100644 --- a/docs/helpers/TestCafe.md +++ b/docs/helpers/TestCafe.md @@ -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 diff --git a/docs/helpers/WebDriver.md b/docs/helpers/WebDriver.md index 54a47ca40..596d5b6d0 100644 --- a/docs/helpers/WebDriver.md +++ b/docs/helpers/WebDriver.md @@ -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 diff --git a/docs/webapi/clearCookie.mustache b/docs/webapi/clearCookie.mustache index e7f52b84a..4820c0fa0 100644 --- a/docs/webapi/clearCookie.mustache +++ b/docs/webapi/clearCookie.mustache @@ -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 diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index 37c23dd35..f402e7876 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -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() } diff --git a/test/helper/Playwright_test.js b/test/helper/Playwright_test.js index 0af57e234..a12041a1c 100644 --- a/test/helper/Playwright_test.js +++ b/test/helper/Playwright_test.js @@ -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();