getAllLocalStorage
Get
localStorage
data for all origins with which the test has interacted.
Syntax​
cy.getAllLocalStorage()
cy.getAllLocalStorage(options)
Usage​
Correct Usage
cy.getAllLocalStorage()
Arguments​
options (Object)
Pass in an options object to change the default behavior of
cy.getAllLocalStorage().
| Option | Default | Description |
|---|---|---|
log | true | Displays the command in the Command log |
timeout | defaultCommandTimeout | Time to wait for cy.getAllLocalStorage() to resolve before timing out |
Yields ​
cy.getAllLocalStorage() yields an object where the keys are origins and the
values are key-value pairs of localStorage data.
For example, if key1 is set to value1 on https://example.cypress.io and
key2 is set to value2 on https://www.cypress-dx.com,
cy.getAllLocalStorage() will yield:
{
'https://example.cypress.io': {
key1: 'value1',
},
'https://www.cypress-dx.com': {
key2: 'value2',
},
}
cy.getAllLocalStorage() is a query, and it is safe to chain further commands.
It will automatically retry until all chained
assertions have passed, or until it times out, re-reading localStorage from all
origins on each retry so the yielded object stays up to date.
Because cy.getAllLocalStorage() is a query, it re-reads localStorage on every
retry, and assertions you chain with .should() drive
those retries until they pass or the command times out. A
.then() callback, by contrast, runs only once after the
command settles and is not retried — so prefer chaining
.should() assertions over asserting inside .then()
when you need Cypress to wait for storage to be populated.
When no localStorage data is present:​
On each read, cy.getAllLocalStorage() yields the current object of origins to
localStorage data, or an empty object if none exists yet. The yielded object is
the subject your chained assertions run against. If an assertion fails, because
the object is empty, or because its contents don't match yet, the query re-reads
localStorage and re-runs the assertion, looping until it passes or the command
times out.
Examples​
Get all localStorage​
cy.visit('https://example.cypress.io', {
onBeforeLoad(win) {
win.localStorage.setItem('key', 'value')
},
})
cy.getAllLocalStorage().then((result) => {
expect(result).to.deep.equal({
'https://example.cypress.io': {
key: 'value',
},
})
})
Rules​
Requirements ​
cy.getAllLocalStorage()requires being chained off ofcy.
Assertions ​
cy.getAllLocalStorage()will automatically retry until all chained assertions have passed.
Timeouts ​
cy.getAllLocalStorage()can time out waiting for assertions you've added to pass.
History​
| Version | Changes |
|---|---|
| 16.0.0 | cy.getAllLocalStorage() is now a query and retries assertions until they pass or time out. |