Files
cypress/docs/source/api/commands/getcookie.md
T
2017-05-25 17:57:56 -04:00

2.9 KiB

title, comments, description
title comments description
getcookie true

Get a browser cookie by it's name.

Syntax

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

Usage

.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')    

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 .getCookie().

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

Yields

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

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

Timeout

.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')

{% 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