Extends Cypress' cy commands with localStorage methods. Allows preserving localStorage between tests.
You want to preserve localStorage between Cypress tests.
This solution allows you to use all browser localStorage methods through Cypress commands, and preserve it between tests.
This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:
npm i --save-dev cypress-localstorage-commands
cypress-localstorage-commands
extends Cypress' cy command.
Add this line to your project's cypress/support/commands.js
:
import "cypress-localstorage-commands"
You can now use all next commands:
Save current localStorage values into an internal "snapshot":
cy.saveLocalStorage();
Restore localStorage to previously "snapshot" saved values:
cy.restoreLocalStorage();
Clear localStorage "snapshot" values:
cy.clearLocalStorageSnapshot();
Get localStorage item. Equivalent to localStorage.getItem
in browser:
cy.getLocalStorage("item");
Set localStorage item. Equivalent to localStorage.setItem
in browser:
cy.setLocalStorage("item", "value");
Remove localStorage item. Equivalent to localStorage.removeItem
in browser:
cy.removeLocalStorage("item");
Use saveLocalStorage
to save a snapshot of current localStorage
at the end of one test, and use the restoreLocalStorage
command to restore it at the beginning of another one:
it("should hide privacy policy message on click accept cookies button", () => {
cy.get("#accept-cookies").click();
cy.get("#privacy-policy").should("not.be.visible");
cy.saveLocalStorage();
});
it("should not show privacy policy message after reloading page", () => {
cy.restoreLocalStorage();
cy.reload();
cy.get("#privacy-policy").should("not.be.visible");
});
Contributors are welcome. Please read the contributing guidelines and code of conduct.
MIT, see LICENSE for details.