Files
cypress/docs/source/api/commands/clearcookies.md
T
2017-05-19 12:01:36 -04:00

2.3 KiB

title, comments, description
title comments description
clearcookies true

Clears all of the browser cookies.

Cypress automatically invokes this command before each test to prevent state from building up. You shouldn't need to invoke this command unless you're using it to clear cookies inside a single test.

Returns null
Timeout cy.clearCookies will wait up for the duration of responseTimeout for the automation server to process this command.

cy.clearCookies()

Clears all the browser cookies.

Options

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

cy.clearCookies(options )

Option Default Notes
timeout responseTimeout Total time to wait for the cy.clearCookies command to be processed
log true whether to display command in command log

Usage

Clear cookies after logging in

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.

cy
  .login("bob@example.com", "p@ssw0rd") // example of custom command
  .clearCookies()
  .visit("/dashboard")                  // we should be redirected back to login
  .url().should("eq", "login")

Command Log

Clear cookies after getting cookies

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

The commands above will display in the command log as:

screen shot 2016-05-10 at 12 01 38 pm

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

screen shot 2016-05-10 at 12 02 01 pm

Related