在柏树规格中等待cookie
我在柏树测试中阅读cookie的读数有问题,
/// <reference types="cypress" />
describe('cookie', () => {
beforeEach(() => {
cy.setCookie('abc', 'def');
});
it('read', () => {
let cookieValue: string;
cy.getCookie('abc').should('exist').then(c => cookieValue = c.value);
cy.wrap(cookieValue).should('not.equal', undefined);
});
});
我从最后一个中出现了
的错误: 预期不确定的不确定
编辑1
我的目标是同步具有cookie值。我设法拥有工作代码:
let cookieValue: string;
it('read', async () => {
cy.wrap(cookieValue).should('equal', undefined);
cookieValue = await new Promise(resolve => cy.getCookie('abc').should('exist').then(c => resolve(c.value)));
cy.wrap(cookieValue).should('not.equal', undefined);
});
但看起来并不柏树。还有其他解决方案吗?
I have problem with reading cookie in my Cypress test
/// <reference types="cypress" />
describe('cookie', () => {
beforeEach(() => {
cy.setCookie('abc', 'def');
});
it('read', () => {
let cookieValue: string;
cy.getCookie('abc').should('exist').then(c => cookieValue = c.value);
cy.wrap(cookieValue).should('not.equal', undefined);
});
});
I have error from last should
:
expected undefined to not equal undefined
Edit 1
My aim is to have cookie value synchronically. I managed to have working code:
let cookieValue: string;
it('read', async () => {
cy.wrap(cookieValue).should('equal', undefined);
cookieValue = await new Promise(resolve => cy.getCookie('abc').should('exist').then(c => resolve(c.value)));
cy.wrap(cookieValue).should('not.equal', undefined);
});
but it doesn't look cypress-way. Any other solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
则可以将cookie存储在变量中,同时设置该cookie ,然后在theeach块中提取,使您具有同步值。
如果已经将cookie设置为测试,
You can store the cookie in a variable at the same time as setting it
If the cookie is already set going in to the test, extracting in beforeEach block gives you a synchronous value.
命令是异步的,您需要在
内包装和测试.。
柏树 以
cookievalue
的值为例,在排队运行之前,它具有最初的空值。您可以使用普通JavaScript同步获取与测试文档相关的所有cookie。
由于返回值是字符串,因此您可能需要解析更复杂的值,例如对象。
您也可以等待
cy.now()
The Cypress commands are async, you need to wrap and test inside the
.then()
More precisely, Cypress commands run in a queue, but
cy.wrap(cookieValue)
will take the value ofcookieValue
before the queue runs, i.e it has the original empty value when enqueued.You can synchronously get all the cookies related to test document using plain javascript.
Since the return value is a string you may need to parse more complex values such as objects.
You can also await
cy.now()
您可以使用Alias
为
来保存cookie值,然后 在外面访问它,然后像这样。要记住的一件事是,这些别名将在同一测试中起作用,因为柏树清除了测试之间的别名。如果要在整个测试中访问该值,我建议您使用cypress.env()
,在下面说明。您也可以使用
cypress.env
在全球范围内保存cookie值并在整个项目中使用它。You can use an alias
as
to save the cookie value andthen
access it outside then like this. One thing to remember is the aliases will work within the same test, because cypress clears aliases between tests. If you want to access the value throughout the tests, I would suggest you to useCypress.env()
, explained below.You can also use
Cypress.env
to globally save the cookie value and use it anywhere throughout the project.