From c6cbd34b5bf8877eb10f34a393ac23f00f643bd5 Mon Sep 17 00:00:00 2001 From: Norbert Graf Date: Thu, 12 Dec 2024 16:49:49 +0100 Subject: [PATCH 1/5] feature: clear individual cookie by name with Playwright (available since Playwright 1.43) --- docs/helpers/Nightmare.md | 2 +- docs/helpers/Playwright.md | 2 +- docs/helpers/Protractor.md | 2 +- docs/helpers/Puppeteer.md | 2 +- docs/helpers/TestCafe.md | 2 +- docs/helpers/WebDriver.md | 2 +- docs/webapi/clearCookie.mustache | 2 +- lib/helper/Playwright.js | 7 ++++--- 8 files changed, 11 insertions(+), 10 deletions(-) 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() } From feff8940413308f6adfb82101d1a5ac22009f73d Mon Sep 17 00:00:00 2001 From: Norbert Graf Date: Mon, 30 Dec 2024 09:08:37 +0100 Subject: [PATCH 2/5] feature: clear individual cookie by name with Playwright (available since Playwright 1.43) --- test/helper/Playwright_test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/helper/Playwright_test.js b/test/helper/Playwright_test.js index b410244d4..0903e738e 100644 --- a/test/helper/Playwright_test.js +++ b/test/helper/Playwright_test.js @@ -1095,6 +1095,30 @@ 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: 'test1', value: 'test1' }) + }) }) let remoteBrowser From c3fbe3c11372519e57c9f362cf7c0d4bb5392134 Mon Sep 17 00:00:00 2001 From: Norbert Graf Date: Mon, 30 Dec 2024 09:13:58 +0100 Subject: [PATCH 3/5] feature: clear individual cookie by name with Playwright (available since Playwright 1.43) --- test/helper/Playwright_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helper/Playwright_test.js b/test/helper/Playwright_test.js index 0903e738e..3992e9846 100644 --- a/test/helper/Playwright_test.js +++ b/test/helper/Playwright_test.js @@ -1117,7 +1117,7 @@ describe('Playwright', function () { I.clearCookie('test1') const cookiesAfterClearing = await I.grabCookie() assert.equal(cookiesAfterClearing.length, 1) - assert.equal(cookiesAfterClearing, { name: 'test1', value: 'test1' }) + assert.equal(cookiesAfterClearing, { name: 'test2', value: 'test2' }) }) }) From 0a1a8bc5ad834f44bafd236c35ed5acdfd72567c Mon Sep 17 00:00:00 2001 From: Norbert Graf Date: Mon, 30 Dec 2024 18:30:21 +0100 Subject: [PATCH 4/5] feature: clear individual cookie by name with Playwright (available since Playwright 1.43) --- test/helper/Playwright_test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/helper/Playwright_test.js b/test/helper/Playwright_test.js index 3992e9846..fd8117e20 100644 --- a/test/helper/Playwright_test.js +++ b/test/helper/Playwright_test.js @@ -1119,6 +1119,7 @@ describe('Playwright', function () { assert.equal(cookiesAfterClearing.length, 1) assert.equal(cookiesAfterClearing, { name: 'test2', value: 'test2' }) }) + }) }) let remoteBrowser From fd3b9fa3d500b7e986f2cc452d7b6b767e7b34c8 Mon Sep 17 00:00:00 2001 From: Norbert Graf Date: Thu, 2 Jan 2025 09:29:05 +0100 Subject: [PATCH 5/5] feature: clear individual cookie by name with Playwright (available since Playwright 1.43) --- test/helper/Playwright_test.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/helper/Playwright_test.js b/test/helper/Playwright_test.js index fd8117e20..d660c2d9b 100644 --- a/test/helper/Playwright_test.js +++ b/test/helper/Playwright_test.js @@ -1095,29 +1095,29 @@ describe('Playwright', function () { I.see('Information') }) }) - describe('#clearCookie', () => { it('should clear all cookies', async () => { - I.amOnPage('/') - I.setCookie({ name: 'test', value: 'test' }) + await I.amOnPage('/') + + await I.setCookie({ name: 'test', value: 'test', url: siteUrl}) const cookiesBeforeClearing = await I.grabCookie() - assert.equal(cookiesBeforeClearing.length, 1) - I.clearCookie() + 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 () => { - 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' }) + 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) }) }) })