https://cypress.io logo
Not entirely sure if this is hacky or not, but it'...
# e2e-testing
a
Not entirely sure if this is hacky or not, but it's worked flawlessly for us for a while now.
Copy code
let LOCAL_STORAGE_MEMORY = {};

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

Cypress.Commands.add("restoreLocalStorage", () => {
    cy.window().then(window => {
        Object.keys(LOCAL_STORAGE_MEMORY).forEach(key => {
            window.localStorage.setItem(key, LOCAL_STORAGE_MEMORY[key]);
        });
    });
});
put that into your commands.js, and then at then at the top of your tests after your
describe(
and before your first
it(
test, call it this way:
Copy code
beforeEach(() => {
        cy.restoreLocalStorage();
    });

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