clearCookie

Clear a specific browser cookie.

Syntax

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

Usage

Correct Usage

cy.clearCookie('authId') // clear the 'authId' cookie

Arguments

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().

OptionDefaultDescription
logtrueDisplays the command in the Command log
timeoutresponseTimeoutTime to wait for cy.clearCookie() to resolve before timing out

Yields

  • cy.clearCookie() yields null .
  • cy.clearCookie() cannot be chained further.

Examples

No Args

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')

Rules

Requirements

  • cy.clearCookie() requires being chained off of cy.

Assertions

  • cy.clearCookie() cannot have any assertions chained.

Timeouts

  • cy.clearCookie() should never time out.

Command Log

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:

Command Log for clearcookie

When clicking on clearCookie within the command log, the console outputs the following:

console.log for clearcookie

See also