oun*_*nis 2 javascript node.js cypress
我想使用 Cypress 保存 cookie 值,但不幸的是,我总是使用此代码在日志控制台中未定义
let cookieValue;
cy.getCookie('SOME_COOKIE')
.should('have.property', 'value')
.then((cookie) => {
cookieValue = cookie.value;
})
cy.log(cookieValue);
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时
let cookieValue;
cy.getCookie('SOME_COOKIE')
.should('have.property', 'value', 'Dummy value')
.then((cookie) => {
cookieValue = cookie.value;
})
cy.log(cookieValue);
Run Code Online (Sandbox Code Playgroud)
我可以在错误消息中看到我想要的实际值。
Cypress 异步工作,您不能像以前那样使用 cookie 值。
从文档
想要跳入命令流程并直接掌握主题吗?没问题,只需将 .then() 添加到您的命令链即可。当前面的命令解析时,它将调用您的回调函数,并将产生的主题作为第一个参数。
您应该在then回调中继续使用您的测试代码,而不是依赖于外部let cookieValue分配。
尝试这个
cy.getCookie('SOME_COOKIE')
.should('have.property', 'value')
.then((cookie) => {
cookieValue = cookie.value;
// YOU SHOULD CONSUME `cookieValue` here
// .. go ahead inside this `then` callback
})
Run Code Online (Sandbox Code Playgroud)