Get all of the browser cookies.
cy.getCookies()
cy.getCookies(options)
Correct Usage
cy.getCookies() // Get all cookies
options (Object)
Pass in an options object to change the default behavior of cy.getCookies().
| Option | Default | Description |
|---|---|---|
log |
true |
Displays the command in the Command log |
timeout |
responseTimeout |
Time to wait for cy.getCookies() to resolve before timing out
|
cy.getCookies() yields an array of cookie objects. Each cookie object has the following properties:
domainexpiry (if specified)
httpOnlynamepathsameSite (if specified)
securevalueIn 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')
})
cy.getCookies() requires being chained off of cy.cy.getCookies() will only run assertions you have chained once, and will not retry.cy.getCookies() should never time out.Because
cy.getCookies()is asynchronous it is technically possible for there to be a timeout while talking to the internal Cypress automation APIs. But for practical purposes it should never happen.
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:
When clicking on getCookies within the command log, the console outputs the following:
| Version | Changes |
|---|---|
| 5.0.0 | Removed experimentalGetCookiesSameSite and made sameSite property always available. |
| 4.3.0 | Added sameSite property when the experimentalGetCookiesSameSite configuration value is true. |
© 2017 Cypress.io
Licensed under the MIT License.
https://docs.cypress.io/api/commands/getcookies