Skip to content

MikezBikez/cypress-sessionstorage-commands

Repository files navigation

Build status Coverage Status Quality Gate

NPM dependencies Last commit Last release

NPM downloads License

Cypress sessionStorage commands

Extends Cypress' cy commands with sessionStorage methods. Allows preserving sessionStorage between tests.

The problem

You want to preserve sessionStorage between Cypress tests.

This solution

This solution allows you to use all browser sessionStorage methods through Cypress commands, and preserve it between tests.

Installation

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

Usage

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:

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");

Preserving session storage between tests

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");
});

Contributing

Contributors are welcome. Please read the contributing guidelines and code of conduct.

License

MIT, see LICENSE for details.

About

Expose sessionStorage methods to Cypress

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published