clearAllCookies
Clear all browser cookies across every domain.
While cy.clearCookies() clears cookies for a
single domain, cy.clearAllCookies() doesn't read the current URL, so it works
without cy.origin() even after your app navigates to
another origin.
Cypress automatically clears all cookies before each test to prevent state from being shared across tests when test isolation is enabled. You shouldn't need to use this command unless you're using it to clear all cookies inside a single test or test isolation is disabled.
Syntax​
cy.clearAllCookies()
cy.clearAllCookies(options)
Usage​
Correct Usage
cy.clearAllCookies() // Clear all cookies
Arguments​
options (Object)
Pass in an options object to change the default behavior of
cy.clearAllCookies().
| Option | Default | Description |
|---|---|---|
log | true | Displays the command in the Command log |
timeout | responseTimeout | Time to wait for cy.clearAllCookies() to resolve before timing out |
Yields ​
cy.clearAllCookies()yieldsnull.
Examples​
No Args​
Clear cookies set by multiple domains End-to-End Only​
In this example, logging in through an identity provider sets cookies on both
the identity provider's domain and your app's domain. cy.clearAllCookies()
clears the cookies from both in one command.
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)
cy.clearAllCookies()
cy.getAllCookies().should('be.empty')
Log out in the middle of a test End-to-End Only​
Clearing every cookie removes your app's session cookie along with any cookies the identity provider set, so the next request arrives logged out. In this example, the app redirects unauthenticated visitors to its login page.
cy.visit('/dashboard')
cy.clearAllCookies()
cy.reload()
cy.url().should('include', 'login')
Clear cookies before each test when test isolation is off End-to-End Only​
With test isolation turned off, cookies
from one test carry over to the next. Call cy.clearAllCookies() in a
beforeEach hook to start every test in the suite without cookies.
describe('Account settings', { testIsolation: false }, () => {
beforeEach(() => {
cy.clearAllCookies()
})
it('shows the login prompt', () => {
// no cookies from earlier tests are present
})
})
Options​
Clear cookies without logging to the Command Log​
Pass { log: false } to keep cy.clearAllCookies() out of the
Command Log, which is useful when it
runs in a hook before every test.
cy.clearAllCookies({ log: false })
Rules​
Requirements ​
cy.clearAllCookies()requires being chained off ofcy.
Assertions ​
cy.clearAllCookies()yieldsnull, so there's nothing to assert on. To confirm the cookies are cleared, assert oncy.getAllCookies()afterward, for examplecy.getAllCookies().should('be.empty').
Timeouts ​
cy.clearAllCookies()can time out waiting for the browser to clear the cookies. It waits up to theresponseTimeout(or thetimeoutoption, when passed) before failing.
History​
| Version | Changes |
|---|---|
| 12.1.0 | cy.clearAllCookies() command added |