getCookies

Get all of the browser cookies.

Syntax

cy.getCookies()
cy.getCookies(options)

Usage

Correct Usage

cy.getCookies() // Get all cookies

Arguments

options (Object)

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

OptionDefaultDescription
domainHostname of the current URLRetrieves the cookies from the specified domain
logtrueDisplays the command in the Command log
timeoutresponseTimeoutTime to wait for cy.getCookies() to resolve before timing out

Yields

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

  • domain
  • expiry (if specified)
  • httpOnly
  • name
  • path
  • sameSite (if specified)
  • secure
  • value

cy.getCookies() is not a query. It will not update the returned list if further cookies are added after it initially executes.

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((cookies) => {
    expect(cookies[0]).to.have.property('name', 'session_id')
  })

Rules

Requirements

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

Assertions

  • cy.getCookies() will only run assertions you have chained once, and will not retry.

Timeouts

  • cy.getCookies() should never time out.

Command Log

cy.getCookies()
  .should('have.length', 1)
  .then((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:

Command Log getcookies

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

Console Log getcookies

History

VersionChanges
5.0.0Removed experimentalGetCookiesSameSite and made sameSite property always available.
4.3.0Added sameSite property when the experimentalGetCookiesSameSite configuration value is true.

See also