title |
---|
clearCookie |
Clear a specific browser cookie.
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 all cookies inside a single test.
cy.clearCookie(name)
cy.clearCookie(name, options)
Correct Usage
cy.clearCookie('authId') // clear the 'authId' cookie
name (String)
The name of the cookie to be cleared.
options (Object)
Pass in an options object to change the default behavior of cy.clearCookie()
.
Option | Default | Description |
---|---|---|
log |
true |
Displays the command in the Command log |
timeout |
responseTimeout |
Time to wait for cy.clearCookie() to resolve before timing out |
cy.clearCookie()
yields null
.cy.clearCookie()
cannot be chained further.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. Then upon navigating to an unauthorized page, we asset that our server has redirected us back to login.
// assume we just logged in
cy.contains('Login').click()
cy.url().should('include', 'profile')
cy.clearCookie('session_id')
cy.visit('/dashboard') // we should be redirected back to login
cy.url().should('include', 'login')
cy.clearCookie()
requires being chained off ofcy
.
cy.clearCookie()
cannot have any assertions chained.
cy.clearCookie()
should never time out.
Because cy.clearCookie()
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.
Clearing a cookie after setting a cookie
cy.setCookie('foo', 'bar')
cy.clearCookie('foo')
cy.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: