Skip to main content
Cypress App

getCookies

Get browser cookies for the current domain or the specified domain.

Syntax​

cy.getCookies()
cy.getCookies(options)

Usage​

Correct Usage

cy.getCookies() // Get cookies for the currrent domain

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
timeoutdefaultCommandTimeoutTime 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: (String)
  • expiry: (Number) (if specified)
  • hostOnly: (Boolean) (if specified)
  • httpOnly: (Boolean)
  • name: (String)
  • path: (String)
  • sameSite: (String) (if specified)
  • secure: (Boolean)
  • value: (String)

cy.getCookies() is a query, and it is safe to chain further commands. It will automatically retry until all chained assertions have passed, or until it times out, re-querying the list of cookies on each retry so the yielded array stays up to date.

caution

Because cy.getCookies() is a query, it re-reads the cookies on every retry, and assertions you chain with .should() drive those retries until they pass or the command times out. A .then() callback, by contrast, runs only once after the command settles and is not retried — so prefer chaining .should() assertions over asserting inside .then() when you need Cypress to wait for the cookies to be set.

When no matching cookies are present:​

On each read, cy.getCookies() yields the current array of cookie objects, or an empty array if none exist yet. The yielded array is the subject your chained assertions run against. If an assertion fails, because the array is empty, or because its contents don't match yet, the query re-reads the cookies and re-runs the assertion, looping until it passes or the command times out.

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

Forward the session to another destination​

cy.request() already sends the browser's cookies for the request URL automatically, so you don't need to build a Cookie header to call your own logged-in API. Reach for this pattern only when you need to forward the current session somewhere Cypress won't attach cookies on its own, such as:

  • a different domain (cy.request() only sends cookies that match the request URL)
  • something outside the browser, like a cy.task() or an external HTTP client

In those cases, combine the cookies into a single Cookie header string:

cy.getCookies().then((cookies) => {
const cookieHeader = cookies.map((c) => `${c.name}=${c.value}`).join('; ')

cy.request({
url: 'https://api.other-service.com/protected',
headers: {
Cookie: cookieHeader,
},
})
})

Rules​

Requirements ​

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

Assertions ​

  • cy.getCookies() will automatically retry until all chained assertions have passed.

Timeouts ​

  • cy.getCookies() can time out waiting for assertions you've added to pass.

Command Log​

Get browser cookies and inspect all properties

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
16.0.0cy.getCookies() is now a query and retries assertions until they pass or time out.
5.0.0Removed experimentalGetCookiesSameSite and made sameSite property always available.
4.3.0Added sameSite property when the experimentalGetCookiesSameSite configuration value is true.

See also​