Cypress clearCookie Command

Profile picture for user arilio666

When the clearCookie command is used in cypress, it deletes the specified browser cookie.

Table of Content

Syntax

cy.clearCookie(name)
cy.clearCookie(name, options)

Arguments Used In clearCookie:

  • Name: The name of the cookie to be cleared should come here.
  • Options: Passing in the option will change the default behavior of cy.clearCookie().
    • Log: Displays commands in the command log
    • .Timeout: Time needed to wait for cy.clearCookie() to resolve.

Rules

  • cy.clearCookie() needs to be chained off of cy.
  • cy.clearCookie() does not support chaining assertions.
  • cy.clearCookie() will never timeout.
  • cy.clearCookie() yields null.

Example

Usage Of clearCookie:

cy.clearCookie('authID')
  • Clears the authID cookie.

For demonstration purposes, let us set a cookie and then clear it using the clearCookie command and asserting it as null to verify that it is removed.

cy.getCookies().should('be.empty')
cy.setCookie('session_id', '22193hewkwh4k4j343')
cy.getCookie('session_id').should(
  'have.property',
  'value',
  '22193hewkwh4k4j343'
)
cy.clearCookie('session_id')
cy.getCookie('session_id').should('be.null')

clear cookie cypress

  • First, we made sure that there were no cookies, to begin with.
  • Then we create a cookie session_id with its value.
  • Then asserting that it is present.
  • After that, the clearCookie command is used to wipe that session_id cookie and assert that the cookie has been wiped.