如何在多个 Cypress 测试中使用 cookie?
How can I use cookies across multiple Cypress tests?
我正在使用 cypress
来测试我的注册和登录表单。我想在我的 register.js
测试中设置 cookie,然后在我的 login.js
测试中使用它们。
我将 cookie 保存在 cypress/support/index.js:
Cypress.Cookies.defaults({
preserve: ['test_email', 'test_password'],
});
我的register.js测试是这样的:
describe('Create new account', () => {
it('Visit Register', () => {
cy.visit(Cypress.env('URL_REGISTER'));
cy.setCookie('test_email', 'fake@email.com');
cy.setCookie('test_password', 'abc123');
});
it('Fill out register form', async () => {
const email = await cy.getCookie('test_email');
const password = await cy.getCookie('test_password');
cy.get('[data-cy="register-email"]').type(email.value);
cy.get('[data-cy="register-password"]').type(password.value);
});
});
当我运行这个测试时,我得到这个错误:
Cannot read property 'value' of undefined
我知道 cy.getCookie()
returns 是一个对象并且是异步的,这就是我尝试使用 await
的原因,但出于某种原因,这似乎不是正在工作。我如何在多个 cypress 测试中 set 和 get cookies?
使用 Cypress.preserveOnce() 在测试之间保留 cookie,或者在 beforeEach()
中为所有测试保留 cookie。
beforeEach(() => {
Cypress.Cookies.preserveOnce('test_email')
})
it('set cookie', async () => {
cy.setCookie('test_email', 'fake@email.com')
})
it('once', async () => {
const email = await cy.getCookie('test_email')
expect(email).to.have.property('value', 'fake@email.com') // passes
})
it('twice', async () => {
const email = await cy.getCookie('test_email')
expect(email).to.have.property('value', 'fake@email.com') // passes
})
it('thrice', async () => {
const email = await cy.getCookie('test_email')
expect(email).to.have.property('value', 'fake@email.com') // passes
})
您应该坚持使用 getCookie 的记录方式。
如果我运行
const email = await cy.getCookie(...)
在 cy.visit()
之后,我变得不确定。
如果我使用
cy.getCookie(...).then(...)
在下一行获取值。
由于 Cypress 没有在任何地方记录 await 语法,您可能会得到一些不稳定的结果。
标准模式是:
it('once', () => {
cy.getCookie('test_email').then(email => {
expect(email).to.have.property('value', 'fake@email.com') // passes
})
})
如果您 运行 在 CI 中进行测试,它们可能 运行 并行,因此最好在 before()
或 beforeEach()
.[=20= 中设置 Cookie ]
before(() => {
cy.setCookie('test_email', 'fake@email.com')
})
beforeEach(() => {
Cypress.Cookies.preserveOnce('test_email')
})
it('uses cookie once', () => {
...
})
it('uses cookie twice', () => {
...
})
it('uses cookie thrice', () => {
...
})
我正在使用 cypress
来测试我的注册和登录表单。我想在我的 register.js
测试中设置 cookie,然后在我的 login.js
测试中使用它们。
我将 cookie 保存在 cypress/support/index.js:
Cypress.Cookies.defaults({
preserve: ['test_email', 'test_password'],
});
我的register.js测试是这样的:
describe('Create new account', () => {
it('Visit Register', () => {
cy.visit(Cypress.env('URL_REGISTER'));
cy.setCookie('test_email', 'fake@email.com');
cy.setCookie('test_password', 'abc123');
});
it('Fill out register form', async () => {
const email = await cy.getCookie('test_email');
const password = await cy.getCookie('test_password');
cy.get('[data-cy="register-email"]').type(email.value);
cy.get('[data-cy="register-password"]').type(password.value);
});
});
当我运行这个测试时,我得到这个错误:
Cannot read property 'value' of undefined
我知道 cy.getCookie()
returns 是一个对象并且是异步的,这就是我尝试使用 await
的原因,但出于某种原因,这似乎不是正在工作。我如何在多个 cypress 测试中 set 和 get cookies?
使用 Cypress.preserveOnce() 在测试之间保留 cookie,或者在 beforeEach()
中为所有测试保留 cookie。
beforeEach(() => {
Cypress.Cookies.preserveOnce('test_email')
})
it('set cookie', async () => {
cy.setCookie('test_email', 'fake@email.com')
})
it('once', async () => {
const email = await cy.getCookie('test_email')
expect(email).to.have.property('value', 'fake@email.com') // passes
})
it('twice', async () => {
const email = await cy.getCookie('test_email')
expect(email).to.have.property('value', 'fake@email.com') // passes
})
it('thrice', async () => {
const email = await cy.getCookie('test_email')
expect(email).to.have.property('value', 'fake@email.com') // passes
})
您应该坚持使用 getCookie 的记录方式。
如果我运行
const email = await cy.getCookie(...)
在 cy.visit()
之后,我变得不确定。
如果我使用
cy.getCookie(...).then(...)
在下一行获取值。
由于 Cypress 没有在任何地方记录 await 语法,您可能会得到一些不稳定的结果。
标准模式是:
it('once', () => {
cy.getCookie('test_email').then(email => {
expect(email).to.have.property('value', 'fake@email.com') // passes
})
})
如果您 运行 在 CI 中进行测试,它们可能 运行 并行,因此最好在 before()
或 beforeEach()
.[=20= 中设置 Cookie ]
before(() => {
cy.setCookie('test_email', 'fake@email.com')
})
beforeEach(() => {
Cypress.Cookies.preserveOnce('test_email')
})
it('uses cookie once', () => {
...
})
it('uses cookie twice', () => {
...
})
it('uses cookie thrice', () => {
...
})