Javascript Cypress保存cookie值
我想使用Cypress保存cookie值,但不幸的是,我总是使用此代码在日志控制台中未定义cookie值Javascript Cypress保存cookie值,javascript,node.js,cypress,Javascript,Node.js,Cypress,我想使用Cypress保存cookie值,但不幸的是,我总是使用此代码在日志控制台中未定义cookie值 let cookieValue; cy.getCookie('SOME_COOKIE') .should('have.property', 'value') .then((cookie) => { cookieValue = cookie.value; }) cy.log(cookieValue); 当我尝试这个的时候 let cookieVa
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);
我可以在错误消息中看到我想要的实际值。Cypress异步工作,您不能像以前那样使用cookie值 从 想直接进入命令流程,直接掌握主题吗?没问题,只需在命令链中添加一个.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
})
您还可以使用Async和wait,而不是在then()中添加代码 在测试中使用它,并确保it块是异步的
it('Sample Test', async function () {
... some code
const cookie = await getmyCookie('cookiename')
cy.log(cookie)
... some code
})
如果它解决了你的问题,请接受答案,它可能会对其他用户在这里登陆时遇到同样的问题非常有帮助!thx再次:)
it('Sample Test', async function () {
... some code
const cookie = await getmyCookie('cookiename')
cy.log(cookie)
... some code
})