q. wellington
q. wellington

Reputation: 67

How to set cookies within Cypress testing?

Cypress Cookie Setting

this example taken from the Cypress documentation won't work properly:

cy.getCookies().should('be.empty');
cy.setCookie('session_id', '189jd09sufh33aaiidhf99d09');
cy.getCookie('session_id').should('have.property', 'value', '189jd09sufh33aaiidhf99d09');

Every time I try to setCookie(), it appears to set it without issue but always returns this when I call getCookies():

$Chainer {chainerId: "chainer18", firstCall: false}
chainerId: "chainer18"
firstCall: false
__proto__: Object

Is there something I'm missing here?

Upvotes: 0

Views: 6317

Answers (2)

q. wellington
q. wellington

Reputation: 67

Looks like this was related to https://github.com/cypress-io/cypress/issues/1321 and has been patched in v 3.1.2 of Cypress. All is right with the world again.

Upvotes: 0

Stanislau Niadbailau
Stanislau Niadbailau

Reputation: 11

You should use cy.getCookies() without arguments. That function returns array of cookies that you have. Than you can do:

cy.getCookies().should('have.length', 1).then((cookies) => {
  expect(cookies[0]).to.have.property('session_id', '189jd09sufh33aaiidhf99d09')
})

Upvotes: 1

Related Questions