Cypress Version:
Cypress package version: 1.0.2
Cypress binary version: 1.0.2
Browser Version:
Canary 64
Bug
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.
Few suggestions :)
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'.
See above :)
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 https://github.com/cypress-io/cypress/issues/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.
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.
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.
Going to close this then - its referenced in the other issue now.
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
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_.*/
})
})
Most helpful comment
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.