clearCookies

Clear all browser cookies for current domain and subdomain.

Syntax

cy.clearCookies()
cy.clearCookies(options)

Usage

Correct Usage

cy.clearCookies() // clear all cookies

Arguments

options (Object)

Pass in an options object to change the default behavior of cy.clearCookies().

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

Yields

  • cy.clearCookies() yields null.

Examples

No Args

Clear all cookies after logging in
End-to-End Only

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.

// assume we just logged in
cy.contains('Login').click()
cy.url().should('include', 'profile')
cy.clearCookies()
cy.visit('/dashboard') // we should be redirected back to login
cy.url().should('include', 'login')

Rules

Requirements

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

Assertions

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

Timeouts

  • cy.clearCookies() should never time out.

Command Log

Clear cookies after getting cookies

cy.getCookies().should('have.length', 1)
cy.clearCookies()
cy.getCookies().should('be.empty')

The commands above will display in the Command Log as:

Command Log

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

Console Log

See also