如何在 Cypress 测试中设置 cookie?
How to set cookies within Cypress testing?
这个取自 Cypress 文档的示例将无法正常工作:
cy.getCookies().should('be.empty');
cy.setCookie('session_id', '189jd09sufh33aaiidhf99d09');
cy.getCookie('session_id').should('have.property', 'value', '189jd09sufh33aaiidhf99d09');
每次我尝试 setCookie() 时,它似乎都可以毫无问题地设置它,但是当我调用 getCookies() 时总是 returns 这个:
$Chainer {chainerId: "chainer18", firstCall: false}
chainerId: "chainer18"
firstCall: false
__proto__: Object
我在这里遗漏了什么吗?
您应该使用不带参数的 cy.getCookies()
。该函数 returns 您拥有的 cookie 数组。比你能做的:
cy.getCookies().should('have.length', 1).then((cookies) => {
expect(cookies[0]).to.have.property('session_id', '189jd09sufh33aaiidhf99d09')
})
看起来这与 https://github.com/cypress-io/cypress/issues/1321 有关,并且已在 Cypress 的 v 3.1.2 中进行了修补。世界又好起来了。
这个取自 Cypress 文档的示例将无法正常工作:
cy.getCookies().should('be.empty');
cy.setCookie('session_id', '189jd09sufh33aaiidhf99d09');
cy.getCookie('session_id').should('have.property', 'value', '189jd09sufh33aaiidhf99d09');
每次我尝试 setCookie() 时,它似乎都可以毫无问题地设置它,但是当我调用 getCookies() 时总是 returns 这个:
$Chainer {chainerId: "chainer18", firstCall: false}
chainerId: "chainer18"
firstCall: false
__proto__: Object
我在这里遗漏了什么吗?
您应该使用不带参数的 cy.getCookies()
。该函数 returns 您拥有的 cookie 数组。比你能做的:
cy.getCookies().should('have.length', 1).then((cookies) => {
expect(cookies[0]).to.have.property('session_id', '189jd09sufh33aaiidhf99d09')
})
看起来这与 https://github.com/cypress-io/cypress/issues/1321 有关,并且已在 Cypress 的 v 3.1.2 中进行了修补。世界又好起来了。