Skip to content

[Feature] Cookie Removal Api #10143

Closed
Closed

Description

@shirshak55

It would be nice to clear the cookie using its name or domain etc. It has a lot of uses like logging out and logging in to certain websites.

Currently, we have only one way to clear the whole cookie. Yes, there are workaround like getting all cookies filtering them, and writing the cookies back using add cookies. So I am making this proposal to add cookie removal API which clears based on name or URL etc.

await context.clearCookies(cookie => cookie.name.startsWith("HELLO")) would remove all cookies which has name that starts with HELLO?

Activity

  1. manochernazari commented on Feb 28, 2022

    @manochernazari

    Any update on this, I really need to be able to delete specific cookie during automated test.

  2. shirshak55 commented on Feb 28, 2022

    @shirshak55
    Author

    manochernazari

    There is an workaround. Firstly, store the cookies in variable, clear the cookies and insert the cookies by filtering out the one which you don't need.

  3. manochernazari commented on Mar 3, 2022

    @manochernazari

    Shirshak (@shirshak55) many thanks for your sugguestion. I got passed the issue just like you sugguested.

  4. Martin-Hogge commented on Aug 10, 2022

    @Martin-Hogge

    This feature would be really appreciated. I was using the workaround (clear/filter/add) but there is a small period of time where there is no cookies in the storage, and I got once an issue where a request has been sent during this small period of time. The request was missing a cookie and because of that the test failed.

    I've worked around it by updating my test to try to avoid any requests sending while I'm filtering cookies, but I can't guarantee this won't happen again. This is why I would love being able to clear a specific cookie without the need to first clear them all.

  5. poziminski commented on Oct 14, 2022

    @poziminski

    Here's a helper script for clearing cookie by name in Playwright.

    const clearCookieByName = async (context, cookieClearedName) => {
          const filteredCookies = await context
              .cookies()
              .filter((cookie) => cookie.name !== cookieClearedName)
    
          context.clearCookies()
          context.addCookies(filteredCookies)
    }
    
    const context = await browser.newContext()
    
    await clearCookieByName(context, 'COOKIE_TO_REMOVE')
    
  6. pavelfeldman commented on Jun 30, 2023

    @pavelfeldman
    Member

    Why was this issue closed?

    Thank you for your involvement. This issue was closed due to limited engagement (upvotes/activity), lack of recent activity, and insufficient actionability. To maintain a manageable database, we prioritize issues based on these factors.

    If you disagree with this closure, please open a new issue and reference this one. More support or clarity on its necessity may prompt a review. Your understanding and cooperation are appreciated.

  7. IanVS commented on May 21, 2024

    @IanVS

    It looks like this has been addressed in Playwright 1.43: https://playwright.dev/docs/release-notes#new-apis-1. context.clearCookies() now supports an optional filter:

    await context.clearCookies();
    await context.clearCookies({ name: 'session-id' });
    await context.clearCookies({ domain: 'my-origin.com' });
    await context.clearCookies({ domain: /.*my-origin\.com/ });
    await context.clearCookies({ path: '/api/v1' });
    await context.clearCookies({ name: 'session-id', domain: 'my-origin.com' });

    https://playwright.dev/docs/api/class-browsercontext#browser-context-clear-cookies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions