# Cookies

Examples of managing cookies in Cypress, for a full reference of commands, go to docs.cypress.io

#### cy.getCookie()

To get a browser cookie, use the `cy.getCookie()` command.

    cy.get('#getCookie .set-a-cookie').click()

    // cy.getCookie() yields a cookie object
    cy.getCookie('token').should('have.property', 'value', '123ABC')

#### cy.getCookies()

To get browser cookies for the current domain, use the `cy.getCookies()` command.

    cy.getCookies().should('be.empty')

    cy.get('#getCookies .set-a-cookie').click()

    // cy.getCookies() yields an array of cookies
    cy.getCookies().should('have.length', 1).should((cookies) => {

      // each cookie has these properties
      expect(cookies[0]).to.have.property('name', 'token')
      expect(cookies[0]).to.have.property('value', '123ABC')
      expect(cookies[0]).to.have.property('httpOnly', false)
      expect(cookies[0]).to.have.property('secure', false)
      expect(cookies[0]).to.have.property('domain')
      expect(cookies[0]).to.have.property('path')
    })

#### cy.getAllCookies()

To get all browser cookies, use the `cy.getAllCookies()` command.

    cy.getAllCookies().should('be.empty')

    cy.setCookie('key', 'value')
    cy.setCookie('key', 'value', { domain: 'example.com' })

    // cy.getAllCookies() yields an array of cookies
    cy.getAllCookies().should('have.length', 2).should((cookies) => {
      // each cookie has these properties
      expect(cookies[0]).to.have.property('name', 'key')
      expect(cookies[0]).to.have.property('value', 'value')
      expect(cookies[0]).to.have.property('httpOnly', false)
      expect(cookies[0]).to.have.property('secure', false)
      expect(cookies[0]).to.have.property('domain')
      expect(cookies[0]).to.have.property('path')

      expect(cookies[1]).to.have.property('name', 'key')
      expect(cookies[1]).to.have.property('value', 'value')
      expect(cookies[1]).to.have.property('httpOnly', false)
      expect(cookies[1]).to.have.property('secure', false)
      expect(cookies[1]).to.have.property('domain', 'example.com')
      expect(cookies[1]).to.have.property('path')
    })

#### cy.setCookie()

To set a browser cookie, use the `cy.setCookie()` command.

    cy.getCookies().should('be.empty')

    cy.setCookie('foo', 'bar')

    // cy.getCookie() yields a cookie object
    cy.getCookie('foo').should('have.property', 'value', 'bar')

#### cy.clearCookie()

To clear a browser cookie, use the `cy.clearCookie()` command.

    cy.getCookie('token').should('be.null')

    cy.get('#clearCookie .set-a-cookie').click()

    cy.getCookie('token').should('have.property', 'value', '123ABC')

    // cy.clearCookies() yields null
    cy.clearCookie('token')

    cy.getCookie('token').should('be.null')

#### cy.clearCookies()

To clear browser cookies for the current domain, use the `cy.clearCookies()` command.

    cy.getCookies().should('be.empty')

    cy.get('#clearCookies .set-a-cookie').click()

    cy.getCookies().should('have.length', 1)

    // cy.clearCookies() yields null
    cy.clearCookies()

    cy.getCookies().should('be.empty')

#### cy.clearAllCookies()

To clear all browser cookies, use the `cy.clearAllCookies()` command.

    cy.getAllCookies().should('be.empty')

    cy.setCookie('key', 'value')
    cy.setCookie('key', 'value', { domain: 'example.com' })

    cy.getAllCookies().should('have.length', 1)

    // cy.clearAllCookies() yields null
    cy.clearAllCookies()

    cy.getAllCookies().should('be.empty')