Skip to content

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

License

Notifications You must be signed in to change notification settings

MikezBikez/cypress-localstorage-commands

 
 

Repository files navigation

Build status Coverage Status Quality Gate

NPM dependencies Last commit Last release

NPM downloads License

Cypress localStorage commands

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

The problem

You want to preserve localStorage between Cypress tests.

This solution

This solution allows you to use all browser localStorage 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-localstorage-commands

Usage

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:

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

Preserving local storage between tests

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

Contributing

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

License

MIT, see LICENSE for details.

About

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

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 93.9%
  • CSS 3.8%
  • HTML 2.3%