Clear all browser cookies for current domain and subdomain.
Cypress automatically clears all cookies 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 a specific cookie inside a single test.
cy.clearCookies()
cy.clearCookies(options)
Correct Usage
cy.clearCookies() // clear all cookies
options (Object)
Pass in an options object to change the default behavior of cy.clearCookies().
| Option | Default | Description |
|---|---|---|
log |
true |
Displays the command in the Command log |
timeout |
responseTimeout |
Time to wait for cy.clearCookies() to resolve before timing out
|
cy.clearCookies() yields null . cy.clearCookies() cannot be chained further. In this example, on first login our server sends us back a session cookie. After invoking cy.clearCookies() this clears the session cookie, and upon navigating to an unauthorized page, our server should have redirected us back to login.
// assume we just logged in
cy.contains('Login').click()
cy.url().should('include', 'profile')
cy.clearCookies()
cy.visit('/dashboard') // we should be redirected back to login
cy.url().should('include', 'login')
cy.clearCookies() requires being chained off of cy.cy.clearCookies() cannot have any assertions chained.cy.clearCookies() should never time out.Because
cy.clearCookies()is asynchronous it is technically possible for there to be a timeout while talking to the internal Cypress automation APIs. But for practical purposes it should never happen.
Clear cookies after getting cookies
cy.getCookies().should('have.length', 1)
cy.clearCookies()
cy.getCookies().should('be.empty')
The commands above will display in the Command Log as:
When clicking on clearCookies within the command log, the console outputs the following:
© 2017 Cypress.io
Licensed under the MIT License.
https://docs.cypress.io/api/commands/clearcookies