title |
---|
clearLocalStorage |
Clear data in localStorage for current domain and subdomain.
{% note warning %} 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. {% endnote %}
cy.clearLocalStorage()
cy.clearLocalStorage(key)
cy.clearLocalStorage(options)
cy.clearLocalStorage(keys, options)
{% fa fa-check-circle green %} Correct Usage
cy.clearLocalStorage() // clear all local storage
{% fa fa-angle-right %} keys (String, RegExp)
Specify key to be cleared in localStorage.
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of cy.clearLocalStorage()
.
Option | Default | Description |
---|---|---|
log |
true |
{% usage_options log %} |
{% yields null cy.clearLocalStorage %}
cy.clearLocalStorage()
cy.clearLocalStorage('appName')
cy.clearLocalStorage(/app-/)
{% requirements parent cy.clearLocalStorage %}
{% assertions none cy.clearLocalStorage %}
{% timeouts none cy.clearLocalStorage %}
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:
{% imgTag /img/api/clearlocalstorage/clear-ls-localstorage-in-command-log.png "Command log for clearLocalStorage" %}
When clicking on clearLocalStorage
within the command log, the console outputs the following:
{% imgTag /img/api/clearlocalstorage/local-storage-object-shown-in-console.png "console.log for clearLocalStorage" %}
- {% url
cy.clearCookies()
clearcookies %}