Clear data in localStorage for current domain and subdomain.
Cypress automatically runs this command before each test to prevent state from being shared across tests. You shouldn't need to use this command unless you're using it to clear localStorage inside a single test.
cy.clearLocalStorage()
cy.clearLocalStorage(key)
cy.clearLocalStorage(options)
cy.clearLocalStorage(keys, options)
Correct Usage
cy.clearLocalStorage() // clear all local storage
keys (String, RegExp)
Specify key to be cleared in localStorage.
options (Object)
Pass in an options object to change the default behavior of cy.clearLocalStorage().
| Option | Default | Description |
|---|---|---|
log |
true |
Displays the command in the Command log |
cy.clearLocalStorage() yields null . cy.clearLocalStorage() cannot be chained further. cy.clearLocalStorage()
cy.clearLocalStorage('appName')
/app-/ RegExpcy.clearLocalStorage(/app-/)
cy.clearLocalStorage() requires being chained off of cy . cy.clearLocalStorage() cannot have any assertions chained. cy.clearLocalStorage() cannot time out. cy.clearLocalStorage(/prop1|2/).then((ls) => {
expect(ls.getItem('prop1')).to.be.null
expect(ls.getItem('prop2')).to.be.null
expect(ls.getItem('prop3')).to.eq('magenta')
})
The commands above will display in the Command Log as:
When clicking on clearLocalStorage within the command log, the console outputs the following:
© 2017 Cypress.io
Licensed under the MIT License.
https://docs.cypress.io/api/commands/clearlocalstorage