We used to login into our application using the cy.request() method and here the JSESSIONID cookie is set.
login(): void {
cy.clearCookie('JSESSIONID');
cy.request({
method: 'POST',
url: `${apiUrl()}/sessions`,
body: {
userName: 'xxx',
password: 'yyy',
},
});
}
I had the JSESSIONID cookie preserved once with:
Cypress.Cookies.defaults({ preserve: 'JSESSIONID',});
in the file support/index.js
Now we want to migrate to Cypress 12. But there the "Cypress.Cookies.defaults" has been deprecated. It said, you have to use cy.session, instead.
After the migration process the above code is located in e2e.js. But how would you there preserve the cookie? I tried several ways but with now luck so far.
e.g.: in cypress documentation is this code snippet:
// Caching session when logging in via API
cy.session(username, () => {
cy.request({
method: 'POST',
url: '/login',
body: { username, password },
}).then(({ body }) => {
window.localStorage.setItem('authToken', body.token)
})
})
This does not work as body.token does not exist (at least in version 12.3.0)