question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cy.clearCookies should clear *ALL* cookies of all domains

See original GitHub issue

After extensive R&D we’ve decided to deviate from the Webdriver spec and enhance clearing cookies to include all domains.

By default, Webdriver will only ever clear cookies based on the current domain context. So if you are visiting http://localhost and you issue HTTP requests to other domains (which set cookies) - those cookies will not be cleared.

We currently respect this spec, and our own cookie implementations also have this same restriction.

However, this is really just a limitation of Webdriver - and not necessarily something Cypress ever has to abide to. We’ve built Cypress with cross browser functionality in mind, and any time we deviate from the Webdriver spec, we are forced to ensure we can create compatibility across all other browsers.

Even though we don’t support cross browsers now, it’s a fundamental part of our strategy and we intend to support them down the road.

What is the proposed change?

  • cy.clearCookies will clear all cookies on all domains, irrespective of the current browsing context
  • Between tests Cypress automatically clears cookies as well, and it will also clear all cookies across all domains.

How will Cypress be able to consistently clear all cookies?

Cypress’s architecture is completely different than that of Webdriver, which puts us in a unique situation to do lots of low level network tricks to pull this off.

While this is subject to change, the way this can be done is by inspecting all of the network traffic that goes through the browser (and therefore Cypress, as we already do), and for all domains, we can manually keep track of all the domains with cookies having been set on them.

At the end of the test, or on a cy.clearCookies command, we can then issue multiple HTTP requests out of the browser (through the Cypress driver) to these various endpoints.

Instead of allowing those requests to pass onto the remote server, Cypress will trap the requests, and automatically respond to the requests by issuing a Set-Cookie header for each cookie that needs to be cleared by setting the Max-Age or Expires directive.

The browser will then run its natural course and clear all of the cookies. This will be a bit slower than programatic API’s, but will only have to be done when the browser does not expose any kind of automation API to achieve the same result programatically.

Related issues

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:91
  • Comments:39 (7 by maintainers)

github_iconTop GitHub Comments

27reactions
flotwigcommented, Feb 6, 2020

Is it undocumented functionality or is not supposed to be used ?

@8BitJonny correct, passing {domain: null} will clear all domains. However, this is undocumented, unsupported, and may change when we improve the cookies API in the future.

25reactions
macphamcommented, Jun 4, 2019

This would be very useful for apps that might use a hosted login page from another domain.

Read more comments on GitHub >

github_iconTop Results From Across the Web

clearCookies - Cypress Documentation
Clear browser cookies for a domain. Cypress automatically clears all cookies before each test to prevent state from being shared across tests when...
Read more >
How to delete cookies from all domain after running tests of a ...
So my question is: Is there a way to tell Cypress to delete all cookies from the domains auth0, SSO, .com? I have...
Read more >
Cypress clearCookies Command - ProgramsBuzz
Cypress clearCookies command is a way in Cypress to clear out browser cookies for the current domain and subdomain.To prevent the state from ......
Read more >
Cypress Clear Cookies - YouTube
Cypress clearCookies command is a way in Cypress to clear out browser cookies for the current domain and subdomain.To prevent the state from ......
Read more >
Cookies | Cypress examples (v8.5.0) - Gleb Bahmutov
To clear all browser cookies, use the cy.clearCookies() command. <div id="clearCookies"> ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found