我正在使用 cy.session() 在 beforeEach 部分登录。
但是 Cypress 会清除重定向上的会话,例如 3DS 支付。 (所以在我的例子中,我被重定向到食谱页面的登录页面)。我发现 Cypress 的行为就像在 Chrome devtools 中打开“禁用缓存”。
主要问题是我无法在测试中的确切位置使用 cy.session() 恢复会话。
示例:
beforeEach(() => {
cy.session('login', () => {
cy.loginAsUser('defaultUser')
})
})
it('Ticket page', () => {
page.enterCreditCardData()
page.clickPayButton()
wait('@payment-done).then(()=>{
//once '@payment-done' interception is occured - the session is cleared
cy.session('login') // this returns an error: "In order to use cy.session(), provide a setup as the second argument"
cy.session('login', () => {
page.assertThePage()
}) // this returns an error: "This session already exists. You may not create a new session with a previously used identifier"
//at this step the session is lost because of 3DS redirects
//I need to restore the session here, but I cant because of cy.session() tries to create a new session with the same name
})
})
我尝试制作“saveSessionCookie”和“RestoreSessionCookies”等自定义命令,但没有帮助
看来只要会话签名两次相同(在
beforeEach()
和测试中途),会话就可以在测试中恢复。
在此示例中,我提取了设置函数以确保在测试中使用相同的函数。
const setup = () => {
console.log('Calling session setup')
cy.setCookie('session_id', '123key')
}
beforeEach(() => {
cy.session('login', setup)
})
it('simple cookie check', () => {
cy.getCookie('session_id')
.its('value')
.should('eq', '123key')
})
it('check cookie, clear it, and restore from session cache', () => {
cy.getCookie('session_id')
.its('value')
.should('eq', '123key')
cy.clearCookie('session_id')
cy.getCookie('session_id')
.should('eq', null)
cy.session('login', setup) // call session again
cy.getCookie('session_id')
.its('value')
.should('eq', '123key')
})