赛普拉斯保存cookie值

欧尼斯

我想使用赛普拉斯保存cookie值,但是不幸的是,使用此代码我总是在日志控制台中未定义

let cookieValue;
cy.getCookie('SOME_COOKIE')
    .should('have.property', 'value')
    .then((cookie) => {
        cookieValue = cookie.value;
    })
cy.log(cookieValue);

当我尝试这个

let cookieValue;
cy.getCookie('SOME_COOKIE')
    .should('have.property', 'value', 'Dummy value')
    .then((cookie) => {
        cookieValue = cookie.value;
    })
cy.log(cookieValue);

我可以在错误消息中看到所需的实际值。

诺里斯特

赛普拉斯是异步工作的,您不能像以前那样使用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
    })

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章