-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Cypress Clearing Cookies During Test Execution #747
Comments
This isn't a bug, Cypress specifically does this to prevent tests from building up state. We recommend programmatically logging into your app before each test (as opposed to using the UI) But we agree with you, I've documented in glory here about why we shouldn't be doing this. I'll keep this issue open, but this is something we're definitely aware of, and will be giving you API's to control this to exactly how you'd like. BTW there are API's already there for controlling which cookies don't get removed. https://docs.cypress.io/api/cypress-api/cookies.html#Preserve-Once I also agree that this is very unexpected and not documented well enough in high traffic areas. |
@brian-mann Thank you for the quick response. I believe the "Preserve-Once" will work for what i need here. I will try this out tomorrow morning and respond back. I really like the cypress.io tool. Keep up the good work guys/girls! |
@brian-mann Using the "defaults" method worked perfect. I attached my code for reference. Thanks again for the quick response. describe( "Memberclicks Tests", () =>{
beforeEach(() => {
Cypress.Cookies.defaults({
whitelist: [ "mcid_token", "serviceID" ]
});
})
context( "Authenticate", () => {
_login( orgid );
})
context( "Navigation", () => {
_navigate( formlist_url );
})
context( "Forms List -> ", () => {
describe( "Form List Header Toolbar ->", () =>{
headerTests.executeCyTest();
})
describe( "Create Form Modal ->", () => {
createFormModalTests.executeCyTest();
})
describe( "Show Archived Forms ->", () => {
showArchivedFormsTests.executeCyTest();
})
describe( "Search for Forms ->", () => {
searchFormsTests.executeCyTest();
})
describe( "Table sorting", () => {
tableSortingTests.executeCyTest();
})
describe( "Table Actions", () => {
tableActionTests.executeCyTest();
})
})
}) |
Closing because this is not a bug, and is referenced elsewhere. |
@brian-mann thanks for the tip! Is there anything similar to Also +1 for #686 as is my first week with Cypress and inherently assumed tests within the same context would preserve browser state (cookies & localStorage) (as in, that that is primary purpose of 'context'). Re: same issue, to play devils advocate a little, think having it in config is a good thing (vs Update - have this approach/work-around working currently except for headless mode: var localStorageCache = { 'some_key' : null };
describe(..., function(){
context(..., function(){
before(function(){ // could also be in after(function(){...});
localStorageCache['some_key'] = null;
cy.clearCookies();
});
beforeEach(function(){
if (localStorageCache['some_key']){
localStorage.setItem('some_key', localStorageCache['some_key']);
}
Cypress.Cookies.preserveOnce();
});
afterEach(function(){
localStorageCache['some_key'] = localStorage.getItem('some_key');
});
it('Perform a login (store cookie, localStorage stuff)', ...) // Or have this as before(...) hook (?)
it('Do something as logged in user', ...)
it('Do something else as logged in user', ...)
it('Log out by pressing log out button in UI', ...)
});
}); Will likely move this into commands.js as Update 2 - Formalized for own purposes -- commands.js: /** Session Caching */
var localStorageCache = { 'userSettings' : null, ... }; // Define keys to save/load in between tests here.
var cookieCache = { 'jwtToken' : null, 'searchSessionID' : null, ... };
Cypress.Commands.add('saveBrowserSession', function(options = {}){
_.forEach(_.keys(localStorageCache), function(storageKey){
localStorageCache[storageKey] = localStorage.getItem(storageKey) || null;
});
_.forEach(_.keys(cookieCache), function(cookieKey){
cookieCache[cookieKey] = cy.getCookie(cookieKey) || null;
});
});
Cypress.Commands.add('loadBrowserSession', function(options = {}){
_.forEach(_.keys(localStorageCache), function(storageKey){
if (typeof localStorageCache[storageKey] === 'string'){
localStorage.setItem(storageKey, localStorageCache[storageKey]);
}
});
_.forEach(_.keys(cookieCache), function(cookieKey){
if (typeof cookieCache[cookieKey] === 'string'){
cy.setCookie(cookieKey, cookieCache[cookieKey]);
}
});
});
Cypress.Commands.add('clearBrowserSession', function(options = {}){
_.forEach(_.keys(localStorageCache), function(storageKey){
localStorageCache[storageKey] = null;
});
_.forEach(_.keys(cookieCache), function(cookieKey){
cookieCache[cookieKey] = null;
});
cy.loadBrowserSession();
}); integration test file: describe("Test User Profile", function(){
context(..., function(){
before(function(){
cy.clearBrowserSession();
});
beforeEach(function(){
cy.loadBrowserSession();
});
afterEach(function(){
cy.saveBrowserSession();
});
after(function(){
// Probably unnecessary as Cypress cleans up between tests.
cy.clearBrowserSession();
});
it('Perform a login (store cookie, localStorage stuff)', ...) // Or have this as before(...) hook (?)
it('Test user profile page, ...', ...)
it('Edit own user profile as logged in user', ...)
it('Edit something which have no permissions for', ...)
it('Edit something which have permissions for', ...)
it('Log out by pressing log out button in UI', ...)
it('Do something as anonymous user', ...)
it('Log in as another user, test profile page', ...)
it('Ensure permissions are different from first user', ...)
});
}); Edit: Scratch that, this doesn't seem to work in headless mode (on Travis CI at least). |
Any idea why this does not work on headless mode? |
It turns out that Cypress does something that, at first, seems annoying but is in fact a Very Good Idea: it clears state before each test, to prevent interdependency between tests. (In other words: tests should be able to run in any order without issues.) This *includes* cookies, hence the errors we were seeing before. See cypress-io/cypress#747 for more details.
Is this a Feature or Bug?
This is a bug
Current behavior:
When cypress is executing tests, cypress for some reason clears the browser cookies. This is unexpected behavior and is making the Cypress un-useable.
Desired behavior:
For cypress not to touch browser cookies during testing.
How to reproduce:
See my code
Test code:
This is an angular 2 application.
Additional Info (images, stack traces, etc)
A utility to turn off automatic cookie removal.
The text was updated successfully, but these errors were encountered: