Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
cypress-localstorage-commands
Advanced tools
Extends Cypress' cy commands with localStorage methods. Allows preserving localStorage between tests
The cypress-localstorage-commands package provides custom commands for Cypress to interact with the browser's localStorage. It allows you to set, get, and clear localStorage items, which can be useful for testing purposes.
Set LocalStorage Item
This command sets a localStorage item with the specified key and value. It is useful for pre-populating localStorage with data before running tests.
cy.setLocalStorage('key', 'value');
Get LocalStorage Item
This command retrieves the value of a localStorage item by its key. It can be used to verify that the correct data is stored in localStorage during tests.
cy.getLocalStorage('key').then(value => { console.log(value); });
Clear LocalStorage
This command clears all items from localStorage. It is useful for ensuring a clean state before running tests.
cy.clearLocalStorage();
Remove LocalStorage Item
This command removes a specific item from localStorage by its key. It can be used to test the behavior of your application when certain localStorage items are missing.
cy.removeLocalStorage('key');
This package provides snapshot testing capabilities for Cypress. While it focuses on visual and DOM snapshots rather than localStorage, it offers a way to ensure that your application's UI remains consistent over time.
Extends Cypress' cy commands with localStorage methods. Allows preserving localStorage between tests and disabling localStorage.
This solution allows you to use all browser localStorage methods through Cypress commands, and preserve it between tests. It also allows to simulate that localStorage is disabled in the browser.
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:
cy.saveLocalStorage()
Saves current localStorage values into an internal "snapshot".
cy.restoreLocalStorage()
Restores localStorage to previously "snapshot" saved values.
cy.clearLocalStorageSnapshot()
Clears localStorage "snapshot" values, so previously saved values are cleaned.
cy.getLocalStorage(item)
Gets localStorage item. Equivalent to localStorage.getItem
in browser.
item
(String): Item to get from localStorage
.cy.setLocalStorage(item, value)
Sets localStorage item. Equivalent to localStorage.setItem
in browser.
item
(String): Item to set value.value
(String): Value to be set.cy.removeLocalStorage(item)
Removes localStorage item. Equivalent to localStorage.removeItem
in browser.
item
(String): Item to be removed.cy.disableLocalStorage(options)
Disables localStorage. It produces localStorage methods to throw errors.
options
(Object): Options to use when disabling localStorage
.
withError
(Error): If provided, invocations to localStorage
methods will throw this error.Use cy.saveLocalStorage()
to save a snapshot of current localStorage
at the end of one test, and use the cy.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 theorically sets 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. Alsocy.clearLocalStorageSnapshot
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");
});
});
});
Use cy.disableLocalStorage()
to simulate that localStorage
is disabled, producing that any invocation to localStorage.setItem
, localStorage.getItem
, localStorage.removeItem
or localStorage.clear
will throw an error. As MDN docs recommend, "developers should make sure to always catch possible exceptions from setItem()". This command allows to test that possible exceptions are handled correctly.
Note that:
localStorage
disabled, so always use cy.reload
or cy.visit
after executing it.localStorage
only remains disabled for all pages loaded during the current test. If you want to disable it for multiple tests, execute it in all of them, or in a beforeEach
statement.clearLocalStorageSnapshot
) is executed while localStorage
is disabled, it will do nothing but producing a Cypress log as: "localStorage.setItem is disabled"Based on previous "Accept cookies button" example, next tests could be added:
//...
const LOCALSTORAGE_DISABLED_WARNING = "#localstorage-disabled-warning";
const LOCALSTORAGE_ERROR = "#localstorage-error";
//... should not be visible after clicked
it("should still be visible when reloading if localStorage is disabled", () => {
cy.disableLocalStorage();
cy.reload();
cy.get(COOKIES_BUTTON).should("be.visible");
});
it("should display warning if localStorage is disabled", () => {
cy.disableLocalStorage();
cy.reload();
cy.get(LOCALSTORAGE_DISABLED_WARNING).should("be.visible");
});
it("should display localStorage error message", () => {
cy.disableLocalStorage();
cy.reload();
cy.get(LOCALSTORAGE_ERROR).should("have.text", "Error");
});
// ...should not be visible after reloading
describe("when localStorage is disabled", () => {
beforeEach(() => {
cy.disableLocalStorage({
withError: new Error("Disabled by cypress-localstorage-commands"),
});
cy.visit("/");
});
it("should display localStorage warning", () => {
cy.get("#localstorage-disabled-warning").should("be.visible");
});
it("should display localStorage error message", () => {
cy.get("#localstorage-error").should("have.text", "Disabled by cypress-localstorage-commands");
});
it("should display accept-cookies button disabled", () => {
cy.get("#accept-cookies").should("be.disabled");
});
});
For those writing TypesScript tests in Cypress, this package includes TypeScript declarations.
Add "cypress-localstorage-commands" to the types
property of the tsconfig.json
file:
{
"compilerOptions": {
"types": ["cypress", "cypress-localstorage-commands"]
}
}
Or reference the package in the files using it:
/// <reference types="cypress-localstorage-commands" />
Contributors are welcome. Please read the contributing guidelines and code of conduct.
MIT, see LICENSE for details.
[1.4.4] - 2021-04-29
FAQs
Extends Cypress' cy commands with localStorage methods. Allows preserving localStorage between tests
The npm package cypress-localstorage-commands receives a total of 246,660 weekly downloads. As such, cypress-localstorage-commands popularity was classified as popular.
We found that cypress-localstorage-commands demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.