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

Cypress erase localstorage between tests (Randomly) #16166

Closed
mmonteiroc opened this issue Apr 23, 2021 · 7 comments
Closed

Cypress erase localstorage between tests (Randomly) #16166

mmonteiroc opened this issue Apr 23, 2021 · 7 comments

Comments

@mmonteiroc
Copy link

Current behavior

While executing tests with cypress open ( also we have that issue in cypress run in the CI )(only executing all tests)
Randomly and in different scenarios, cypress erases all localstorage from the page where executes the tests.

That can generate (depending of the structure of the app) redirects unexpected to the login page....

Desired behavior

So that local storage is not erased except if forcedly using cy.clearLocalStorage

Test code to reproduce

Cannot provide tests to make this fail, but willing to test anything you want me to try and give you the feedback :/

Versions

Using cypress 6.8.0 - Also found in 7.1.0 (we rolled back because other issues open with this version, but we still had that issue in that version)

Env: Windows and CI Azure

@mmonteiroc
Copy link
Author

Remark, not only between tests, but we also encountered this issue during a test.

@mmonteiroc
Copy link
Author

mmonteiroc commented Apr 23, 2021

We have this code in the support file, so that executes before ANY TEST and so we are sure that we have localsotrage with our tokens and stuff during the test, but still the test redirects the login cause the localstorage gets cleared.

beforeEach(() => {
  // - This file returns a JSON OBJECT WHICH IS THE LOCALSTORAGE DIRECTLY
  cy.readFile(filePath).then((storage: Storage) => {
    Object.entries(storage).forEach(([key, value]: [string, string]) => {
      localStorage.setItem(key, value);
    });
  });
});

I did add logs, view in live the state of the localstorage.... this code works and adds the localstorage stuff, but then instantly starts the test, it gets erased it ... :/

@mmonteiroc
Copy link
Author

Is there any way to disable this of deleting the localstorage between tests ? otherwise apps which have a login like ours we will have this issue always

@MuckT
Copy link

MuckT commented Apr 25, 2021

@mmonteiroc try these custom commands in your commands.js

// -- Save localStorage between tests
let LOCAL_STORAGE_MEMORY = {};

Cypress.Commands.add('saveLocalStorage', () => {
  Object.keys(localStorage).forEach(key => {
    LOCAL_STORAGE_MEMORY[key] = localStorage[key];
  });
});

Cypress.Commands.add('restoreLocalStorage', () => {
  Object.keys(LOCAL_STORAGE_MEMORY).forEach(key => {
    localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]);
  });
});

In your describe blocks for a spec

afterEach(() => {
  cy.saveLocalStorage();
});

beforeEach(() => {
  cy.restoreLocalStorage();
});

@mmonteiroc
Copy link
Author

mmonteiroc commented Apr 26, 2021

Hey ! @MuckT Thanks for your reply but I still have the issue, I found the following commands in a post and I tried....
We have the feeling that the erase of localstorage is made between the beforeEach and the test execution.... the thing is that we would like to avoid to have to add the call to this command in any single IT() block as we have more than 200....

I have the following:

let LOCAL_STORAGE_MEMORY = {};

Cypress.Commands.add('saveLocalStorageCache', () => {
  Object.keys(localStorage).forEach(key => {
    LOCAL_STORAGE_MEMORY[key] = localStorage[key];
  });
});

Cypress.Commands.add('restoreLocalStorageCache', () => {
  Object.keys(LOCAL_STORAGE_MEMORY).forEach(key => {
    localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]);
  });
});

Cypress.Commands.add('clearLocalStorageCache', () => {
  localStorage.clear();
  LOCAL_STORAGE_MEMORY = {};
});

And then in the suport file, I have in the before each and after each the following (meaning that will be applied to all it() of all files...

beforeEach(() => {
  cy.restoreLocalStorageCache();
});

afterEach(() => {
  cy.saveLocalStorageCache();
});

@mmonteiroc
Copy link
Author

I don't know if would be hard to implement, a FLAG in the config of cypress which if true, then the localstorage is not erased between tests :/ but Im sure way more people appart of me, would enjoy this small feature

@jennifer-shehane
Copy link
Member

LocalStorage is cleared between tests by design. There's a feature request to allow certain localstorage entries to persist between tests: Duplicate of #461

Also related: #686

But our work on Sessions is likely what will resolve your issue in the end. #8301

There are several workarounds to this currently in the localstorage thread, one of which is #461 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants