2.2 KiB
title, comments
| title | comments |
|---|---|
| clearLocalStorage | false |
Clear data in local storage.
{% 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 %}
Syntax
cy.clearLocalStorage()
cy.clearLocalStorage(key)
cy.clearLocalStorage(options)
cy.clearLocalStorage(keys, options)
Usage
{% fa fa-check-circle green %} Correct Usage
cy.clearLocalStorage() // clear all local storage
Arguments
{% fa fa-angle-right %} keys (String, RegExp)
Specify key to be cleared in local storage.
{% 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 {% helper_icon yields %}
{% yields null cy.clearLocalStorage %}
Examples
Clear Local Storage
Clear all local storage
cy.clearLocalStorage()
Clear Keys
Clear local storage with key 'appName'
cy.clearLocalStorage('appName')
Clear all local storage matching /app-/
cy.clearLocalStorage(/app-/)
Rules
Requirements {% helper_icon requirements %}
{% requirements parent cy.clearLocalStorage %}
Assertions {% helper_icon assertions %}
{% assertions none cy.clearLocalStorage %}
Timeouts {% helper_icon timeout %}
{% timeouts none cy.clearLocalStorage %}
Command Log
cy.clearLocalStorage(/prop1|2/).then(function(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:
See also
- {% url
cy.clearCookies()clearcookies %}

