Files
cypress/docs/source/api/commands/clearcookie.md
T

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

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

cy
  .setCookie('foo', 'bar')
  .clearCookie('foo')
  .getCookie('foo').should('be.null')

The commands above will display in the command log as:

screen shot 2016-05-22 at 9 21 14 pm

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

screen shot 2016-05-22 at 9 21 32 pm

Related