Cypress getCookies Command

Profile picture for user devraj

Cypress get cookies command will get all the browser cookies. It yields an array of cookie objects.

Syntax

In options, you can specify two values:

cy.getCookies()
cy.getCookies(options)
  • log: to display the commands in the command log. The default value is true
  • timeout: time to wait for the get Cookies command to resolve before timing out. The default value is responseTimeout 

Properties of Cookie Objects

Each cookie object has the following properties:

  1. domain
  2. expiry (if specified)
  3. httpOnly
  4. name
  5. path
  6. sameSite (if specified)
  7. secure
  8. value

Rules

  • cy.getCookies() requires being chained off with cy.
  • cy.getCookies() will only run assertions you have chained once and will not retry.
  • cy.getCookies() should never time out practically. However, It is technically possible for there to be a timeout while talking to the internal Cypress automation APIs because this command is asynchronous. 

Examples

Demo Link: https://www.programsbuzz.com/user/login

it.only('Get Cookies Demo', () => {
        cy.visit('https://www.programsbuzz.com/user/login')
        cy.getCookies()
            // Verify Number of elements in cookie are 5
            .should('have.length', 5)
            .then((cookies) => {
                // Verify first eleement contains secure and httpOnly propery
                expect(cookies[0]).to.have.property('secure')
                expect(cookies[0]).to.have.property('httpOnly')
                // Verify 2nd cookie element domain property contains value .programsbuzz
                expect(cookies[1]).to.have.property(
                    'domain',
                    '.programsbuzz.com'
                )
            })
    })

Output

Cypress getCookies Command