getAllCookies
Get all browser cookies.
Syntax
cy.getAllCookies()
cy.getAllCookies(options)
Usage
Correct Usage
cy.getAllCookies() // Get all cookies
Arguments
options (Object)
Pass in an options object to change the default behavior of
cy.getAllCookies().
| Option | Default | Description |
|---|---|---|
log | true | Displays the command in the Command log |
timeout | defaultCommandTimeout | Time to wait for cy.getAllCookies() to resolve before timing out |
Yields
cy.getAllCookies() yields an array of cookie objects. Each cookie object has
the following properties:
domain: (String)expiry: (Number) (if specified)hostOnly: (Boolean) (if specified)httpOnly: (Boolean)name: (String)path: (String)sameSite: (String) (if specified)secure: (Boolean)value: (String)
cy.getAllCookies() is a query, and it is safe to chain further commands. The
query automatically retries until all chained
assertions have passed, or until it times out, re-reading the cookies from all
domains on each retry so the yielded array stays up to date.
Because cy.getAllCookies() is a query, it re-reads the cookies 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 Cypress doesn't retry it — so prefer chaining
.should() assertions over asserting inside .then()
when you need Cypress to wait for the cookies to be set.
When no cookies are present:
On each read, cy.getAllCookies() yields the current array of cookie objects, or
an empty array if none exist yet. The yielded array is the subject your chained
assertions run against. If an assertion fails, because the array is empty, or
because its contents don't match yet, the query re-reads the cookies and re-runs
the assertion, looping until it passes or the command times out.
Examples
Get Cookies
Get cookies after logging in
In this example, you log in through an identity provider that sets a cookie and redirects back to your site, which sets a session cookie.
cy.contains('Log in').click()
cy.origin('https://example.cypress.io', () => {
cy.get('[type=password]').type('*****')
cy.contains('Log in').click()
})
cy.url().should('include', 'profile')
cy.getAllCookies()
.should('have.length', 2)
.then((cookies) => {
expect(cookies[0]).to.have.property('name', 'identity_session_id')
expect(cookies[1]).to.have.property('name', 'session_id')
})
Rules
Requirements
cy.getAllCookies()requires being chained off ofcy.
Assertions
cy.getAllCookies()automatically retries until all chained assertions have passed.
Timeouts
cy.getAllCookies()can time out waiting for assertions you've added to pass.
History
| Version | Changes |
|---|---|
| 16.0.0 | cy.getAllCookies() is now a query and retries assertions until they pass or time out. |