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. The usage of beforeEach
and afterEach
is recommended for this purpose.
Next example shows how this package can be used to test a "cookies button" (which theoretically set a flag into localStorage
and can be clicked only once)
describe("Accept cookies button", () => {
const COOKIES_BUTTON = "#accept-cookies";
before(() => {
cy.clearLocalStorageSnapshot();
});
beforeEach(() => {
cy.restoreLocalStorage();
cy.visit("/");
});
afterEach(() => {
cy.saveLocalStorage();
});
it("should be visible", () => {
cy.get(COOKIES_BUTTON).should("be.visible");
});
it("should not be visible after clicked", () => {
cy.get(COOKIES_BUTTON).click();
cy.get(COOKIES_BUTTON).should("not.be.visible");
});
it("should not be visible after reloading", () => {
cy.get(COOKIES_BUTTON).should("not.be.visible");
});
});
Note the usage of
beforeEach
andafterEach
for preservinglocalStorage
between all tests. AlsoclearLocalStorageSnapshot
is used in thebefore
statement to avoid possible conflicts with other test files preserving localStorage.
Based on the previous example, assertions could be added to check values of localStorage
:
describe("localStorage cookies-accepted item", () => {
beforeEach(() => {
cy.restoreLocalStorage();
cy.visit("/");
});
afterEach(() => {
cy.saveLocalStorage();
});
it("should be null first time page is visited", () => {
cy.getLocalStorage("cookies-accepted").should("equal", null);
});
it("should be true after clicking cookies button", () => {
cy.get("#accept-cookies").click();
cy.getLocalStorage("cookies-accepted").should("equal", "true");
});
it("should be true after reloading", () => {
cy.getLocalStorage("cookies-accepted").then(cookiesAccepted => {
expect(cookiesAccepted).to.equal("true");
});
});
});
Contributors are welcome. Please read the contributing guidelines and code of conduct.
MIT, see LICENSE for details.