Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sand3r opened this issue Oct 25, 2017 · 7 comments
Labels
type: duplicate This issue or pull request already exists type: question

Comments

@sand3r
Copy link

sand3r commented Oct 25, 2017

  • 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)

@sand3r sand3r changed the title Cookies preserved over runs, clearCookies() does not clear whitelisted cookies Whitelisted cookies preserved over runs, clearCookies() does not clear whitelisted cookies Oct 25, 2017
@brian-mann
Copy link
Member

brian-mann commented Oct 25, 2017

This session / cookie handling is the worst part of Cypress right now. The API's are horrible, its not explained anywhere, and it's the opposite of what users expect.

This issue goes into a lot more detail about it #686

We will not fix the existing Cookie API's and we will instead opt to fix it more comprehensively as outlined above. I'll keep this issue open as a reference in the mean time.

@brian-mann
Copy link
Member

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.

@sand3r
Copy link
Author

sand3r commented Oct 25, 2017

Thanks Brian!

I was a bit unsure whether I was really missing something, but your answer clarifies. I'll keep track of #686.

Furthermore just wanted to share that Cypress.io is just amazing - I am just really enthusiastic about it and spreading the message here at the office. It really helps while developing; which is what I try to use it mainly for currently.

@brian-mann
Copy link
Member

Going to close this then - its referenced in the other issue now.

@jennifer-shehane jennifer-shehane added type: duplicate This issue or pull request already exists type: question labels Nov 1, 2017
@aronmgv
Copy link

aronmgv commented Nov 18, 2019

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.

On 3.5.0 and does not work using cy.clearCookie('nameOfCookie')..

When I try to change defaults to empty array or undefined, still wont delete the cookie..

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

Any other ideas??

Thanks

@bvdr
Copy link

bvdr commented 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_.*/
  })
})

@jennifer-shehane
Copy link
Member

We released cy.session() as an experimental command (set experimentalSessionSupport to true in config) in Cypress 8.2.0. This command can be used to cache and restore cookies, localStorage, and sessionStorage. We especially see this command as being useful for testing Auth flows and recommend giving it a try to replace your existing tests around authentication and login.

If you have any feedback about cy.session() for general UX improvements, new features, or unexpected behavior, please leave your feedback in our Cypress Session Experimental Feedback Discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: duplicate This issue or pull request already exists type: question
Projects
None yet
Development

No branches or pull requests

5 participants