Extends Cypress' cy commands with sessionStorage methods. Allows preserving sessionStorage between tests.
You want to preserve sessionStorage between Cypress tests.
This solution allows you to use all browser sessionStorage 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-sessionstorage-commands
cypress-sessionstorage-commands
extends Cypress' cy command.
Add this line to your project's cypress/support/commands.js
:
import "cypress-sessionstorage-commands";
You can now use all next commands:
Save current sessionStorage values into an internal "snapshot":
cy.saveSessionStorage();
Restore sessionStorage to previously "snapshot" saved values:
cy.restoreSessionStorage();
Clear sessionStorage "snapshot" values:
cy.clearSessionStorageSnapshot();
Get sessionStorage item. Equivalent to sessionStorage.getItem
in browser:
cy.getSessionStorage("item");
Set sessionStorage item. Equivalent to sessionStorage.setItem
in browser:
cy.setSessionStorage("item", "value");
Remove sessionStorage item. Equivalent to sessionStorage.removeItem
in browser:
cy.removeSessionStorage("item");
Use saveSessionStorage
to save a snapshot of current sessionStorage
at the end of one test, and use the restoreSessionStorage
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.saveSessionStorage();
});
it("should not show privacy policy message after reloading page", () => {
cy.restoreSessionStorage();
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.