How to set cookies within Cypress testing?

∥☆過路亽.° 提交于 2019-12-08 12:37:17

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/53050294/how-to-set-cookies-within-cypress-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!