How to use clearlocalstorage method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

local_storage.spec.js

Source: local_storage.spec.js Github

copy
1/// <reference types="cypress" />
2
3context('Local Storage', () => {
4  beforeEach(() => {
5    cy.visit('https://example.cypress.io/commands/local-storage')
6  })
7  // Although local storage is automatically cleared
8  // in between tests to maintain a clean state
9  // sometimes we need to clear the local storage manually
10
11  it('cy.clearLocalStorage() - clear all data in local storage', () => {
12    // https://on.cypress.io/clearlocalstorage
13    cy.get('.ls-btn').click().should(() => {
14      expect(localStorage.getItem('prop1')).to.eq('red')
15      expect(localStorage.getItem('prop2')).to.eq('blue')
16      expect(localStorage.getItem('prop3')).to.eq('magenta')
17    })
18
19    // clearLocalStorage() yields the localStorage object
20    cy.clearLocalStorage().should((ls) => {
21      expect(ls.getItem('prop1')).to.be.null
22      expect(ls.getItem('prop2')).to.be.null
23      expect(ls.getItem('prop3')).to.be.null
24    })
25
26    cy.get('.ls-btn').click().should(() => {
27      expect(localStorage.getItem('prop1')).to.eq('red')
28      expect(localStorage.getItem('prop2')).to.eq('blue')
29      expect(localStorage.getItem('prop3')).to.eq('magenta')
30    })
31
32    // Clear key matching string in Local Storage
33    cy.clearLocalStorage('prop1').should((ls) => {
34      expect(ls.getItem('prop1')).to.be.null
35      expect(ls.getItem('prop2')).to.eq('blue')
36      expect(ls.getItem('prop3')).to.eq('magenta')
37    })
38
39    cy.get('.ls-btn').click().should(() => {
40      expect(localStorage.getItem('prop1')).to.eq('red')
41      expect(localStorage.getItem('prop2')).to.eq('blue')
42      expect(localStorage.getItem('prop3')).to.eq('magenta')
43    })
44
45    // Clear keys matching regex in Local Storage
46    cy.clearLocalStorage(/prop1|2/).should((ls) => {
47      expect(ls.getItem('prop1')).to.be.null
48      expect(ls.getItem('prop2')).to.be.null
49      expect(ls.getItem('prop3')).to.eq('magenta')
50    })
51  })
52})
53
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)