2.4 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| clearcookie | true |
Clears a browser cookie.
Cypress automatically clears all cookies before each test to prevent state from building up. You shouldn't need to invoke this command unless you're using it to clear a specific cookie inside a single test.
| Returns | null |
| Timeout | cy.clearCookie will wait up for the duration of responseTimeout for the automation server to process this command. |
cy.clearCookie( name )
Clears a browser cookie by its name.
Options
Pass in an options object to change the default behavior of cy.clearCookie.
cy.clearCookie( name, options )
| Option | Default | Notes |
|---|---|---|
timeout |
responseTimeout |
Total time to wait for the cy.clearCookie command to be processed |
log |
true |
whether to display command in command log |
Usage
Clear a cookie after logging in
In this example, on first login our server sends us back a session cookie. After invoking cy.clearCookie('session_id') this clears the session cookie, and upon navigating to an unauthorized page, our server should have redirected us back to login.
cy
.login('bob@example.com', 'p@ssw0rd') // example of custom command
.clearCookie('session_id')
.visit('/dashboard') // we should be redirected back to login
.url().should('eq', 'login')
Command Log
Clearing a cookie after setting a cookie
cy
.setCookie('foo', 'bar')
.clearCookie('foo')
.getCookie('foo').should('be.null')
The commands above will display in the command log as:
When clicking on clearCookie within the command log, the console outputs the following:

