getCookie

Improve this doc

Get a browser cookie by its name.

Syntax

cy.getCookie(name)
cy.getCookie(name, options)

Usage

Correct Usage

cy.getCookie('auth_key')     // Get cookie with name 'auth_key'

Arguments

name (String)

The name of the cookie to get.

options (Object)

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

Option Default Description
log true Displays the command in the Command log
timeout responseTimeout Time to wait for cy.getCookie() to resolve before timing out

Yields

cy.getCookie() yields a cookie object with the following properties:

  • name
  • value
  • path
  • domain
  • httpOnly
  • secure
  • expiry

When a cookie matching the name could not be found:

cy.getCookie() yields null.

Examples

No Args

Get session_id cookie after logging in

In this example, on first login, our server sends us back a session cookie.

// assume we just logged in
cy.contains('Login').click()
cy.url().should('include', 'profile')
cy.getCookie('session_id')
  .should('have.property', 'value', '189jd09su')

Using cy.getCookie() to test logging in

Check out our example recipes using cy.getCookie() to test logging in using HTML web forms, logging in using XHR web forms and logging in with single sign on

Rules

Requirements

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

Assertions

  • cy.getCookie() will only run assertions you've chained once, and will not retry.

Timeouts

  • cy.getCookie() should never time out.

Because cy.getCookie() is asynchronous it is technically possible for there to be a timeout while talking to the internal Cypress automation APIs. But for practical purposes it should never happen.

Command Log

cy.getCookie('fakeCookie1').should('have.property', 'value', '123ABC')

The commands above will display in the Command Log as:

Command Log

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

Console Log

See also