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.

Whitelisted cookies preserved over runs, clearCookies() does not clear whitelisted cookies

See original GitHub issue
  • Operating System: OSX 10.12.5

  • Cypress Version: Cypress package version: 1.0.2 Cypress binary version: 1.0.2

  • Browser Version: Canary 64

Is this a Feature or Bug?

Bug

Current behavior:

Having the following test code:

describe('Example', () => {

  Cypress.Cookies.defaults({
    whitelist: /SESSION_COOKIE/
  });

  before('we should log in first', () => {
    cy
      // clearCookies() does not clear whitelisted cookies
      .clearCookies()
      .visit('application_url, which sets a NEW SESSION_COOKIE to use throughout the following tests');
  });

  it('should still have SESSION_COOKIE', function() {
    cy
      .visit('application_url/subpage')
      .wait(5000); 

      // .wait() was just added so you can inspect the list of cookies.
      // Notice that SESSION_COOKIE stays the same over several runs.
  });

  it('should really still have SESSION_COOKIE', function() {
    // More tests.
    cy
      .visit('application_url/subpage')
      .wait(5000);
  });

  it('should really really still have SESSION_COOKIE', function() {
    // More and more tests.
    cy
      .visit('application_url/subpage')
      .wait(5000);
  });
});

In the current phase my tests keeps failing, because the first run logs in and because I preserve the cookie, it is persisted throughout every test that runs.

Technically I’m not able to clear cookies between runs/during tests.

Desired behavior:

Few suggestions 😃

  • clearCookies() should be able to remove all cookies, even whitelisted cookies (to avoid a breaking change, it can be an option to clearCookies(), e.g. clearWhitelisted: true ?)
  • Next to ‘preserveOnce()’ I’d also rather see a ‘preserve’ that will at least preserve the cookies throughout the test suite (test suite = 1 file). If I want to achieve what I actually want right now I have to call preserveOnce() in every single test.

How to reproduce:

Use the above code, start coding on the test and press Save. Cypress will run the test, but preserve the whitelisted cookie even though it says ‘clearCookies()’. Also when you use the ‘Run all tests’.

Test code:

See above 😃

Additional Info (images, stack traces, etc)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
brian-manncommented, Oct 25, 2017

Also if you want to clear a cookie even if its been preserved you can just do cy.clearCookie('nameOfCookie')

That will blow it away no matter what.

3reactions
bvdrcommented, Dec 23, 2019

And here is my workaround to clear cookies that are set using whitelist regex and set them again:

it("Clear browser cookies and localstorage", function(){
  
  //Visit URL
  const host_url = Cypress.env('host_url');
  cy.visit(host_url)

  // Empty defaults
  Cypress.Cookies.defaults({
    whitelist: []
  });

  //Clear localStrage
  cy.clearLocalStorage()

  //Clear Cookies
  cy.clearCookies()

  // Set defaults
  Cypress.Cookies.defaults({
    whitelist: /wordpress_.*|woocommerce_.*|wp_woocommerce_.*/
  })
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

When set to clear cookies on exit, the whitelisted sites is also ...
When I have Edge set to clear cookies on exit, websites I list under "Don't clear" disappear each time I close and relaunch....
Read more >
Clear, enable, and manage cookies in Chrome - Google Support
Clear, enable, and manage cookies in Chrome. You can choose to delete existing cookies, allow or block all cookies, and set preferences for...
Read more >
Cookies - Cypress Documentation
Log when cookie values are created, modified or deleted. By turning on debugging, Cypress will automatically generate logs to the console when it...
Read more >
Preserve cookies / localStorage session across tests in Cypress
To update this thread, there is already a better solution available for preserving cookies (by @bkucera); but now there is a workaround ...
Read more >
Does Keeping a Cookie "Whitelist" Slow Down Chrome?
I would think scenario B, with cookies being blocked and specific websites whitelisted would be more of a negative impact on webpage ...
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