Cypress Get Session Storage

Profile picture for user arilio666

Cypress allows you to interact with the browser's APIs by providing access to the underlying browser's window object. In Cypress, you can use the window.sessionStorage object to obtain session storage data.

To acquire the window object, use the cy.window() command to access the session storage.

cy.window().then((win) => {

});

The win.sessionStorage object can be accessed within the cy.window() callback function to retrieve session storage.

cy.window().then((win) => {
 const sessionStorage = win.sessionStorage;
});

The getItem() method can be used to retrieve the value of an item stored in the session storage.

cy.window().then((win) => {
 const sessionStorage = win.sessionStorage;
 const itemValue = sessionStorage.getItem('itemName');
});

The setItem() method can store an item in the session storage.

cy.window().then((win) => {
 const sessionStorage = win.sessionStorage;
 sessionStorage.setItem('itemName', 'itemValue');
});

The removeItem() method can remove an item from the session storage.

cy.window().then((win) => {
 const sessionStorage = win.sessionStorage;
 sessionStorage.removeItem('itemName');
});

Conclusion:

That is how you can use Cypress commands to access and manipulate session storage. Remember to customize the commands to your specific test scenario and requirements.