Files
cypress/docs/source/api/commands/getcookie.md
2017-06-09 14:04:20 -04:00

2.9 KiB

title, comments
title comments
getCookie true

Get a browser cookie by it's name.

Syntax

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

Usage

cy.getCookie() cannot be chained off any other cy commands, so should be chained off of cy for clarity.

{% fa fa-check-circle green %} Valid Usage

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

Arguments

{% fa fa-angle-right %} name (String)

The name of the cookie to get.

{% fa fa-angle-right %} options (Object)

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

Option Default Notes
log true Whether to display command in Command Log
timeout responseTimeout Total time to wait for the cy.getCookie() command to be processed

Yields

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

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

Timeout

cy.getCookie() will continue to look for the cookie for the duration of the defaultCommandTimeout.

Examples

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

{% note info %} 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 {% endnote %}

Command Log

Get cookie

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

The commands above will display in the command log as:

screen shot 2016-05-10 at 12 12 13 pm

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

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

See also