问题
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