2.6 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| clearcookies | true |
Clear all browser cookies.
{% note warning %} 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. {% endnote %}
Syntax
.clearCookies()
.clearCookies(options)
Usage
.clearCookies() cannot be chained off any other cy commands, so should be chained off of cy for clarity.
{% fa fa-check-circle green %} Valid Usage
cy.clearCookies()
Arguments
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of cy.clearCookies.
| Option | Default | Notes |
|---|---|---|
timeout |
responseTimeout |
Total time to wait for the .clearCookies() command to be processed |
log |
true |
whether to display command in command log |
Yields
.clearCookies() yields null
Timeout
.clearCookies() will wait up for the duration of responseTimeout for the automation server to process this command.
Examples
Clear Cookies
Clear all cookies after logging in
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.
cy
// assume we just logged in
.contains('Login').click()
.url().should('include', 'profile')
.clearCookies()
.visit('/dashboard') // we should be redirected back to login
.url().should('include', 'login')
Command Log
Clear cookies after getting cookies
cy
.getCookies().should('have.length', 1)
.clearCookies()
.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:

