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

2.6 KiB

title, comments
title comments
getCookies true

Get all of the browser cookies.

Syntax

cy.getCookies()
cy.getCookies(options)

Usage

cy.getCookies() 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.getCookies()    // Get all cookies

Arguments

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

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

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

Yields

cy.getCookies() yields an array cookie objects. Each cookie object has the following properties:

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

Timeout

cy.getCookies() will continue to look for cookies for the duration of the defaultCommandTimeout.

Examples

Get Cookies

Get cookies 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.getCookies()
  .should('have.length', 1)
  .then(function(cookies) {
    expect(cookies[0]).to.have.property('name', 'session_id')
  })

Command Log

Get cookies

cy.getCookies().should('have.length', 1).then(function(cookies) {
  expect(cookies[0]).to.have.property('name', 'fakeCookie1')
  expect(cookies[0]).to.have.property('value', '123ABC')
  expect(cookies[0]).to.have.property('domain')
  expect(cookies[0]).to.have.property('httpOnly')
  expect(cookies[0]).to.have.property('path')
  expect(cookies[0]).to.have.property('secure')
})

The commands above will display in the command log as:

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

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

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

See also