I am testing a website that you have to add card details to, in order to do this you need to redirect to a payment providers site to add the details. When you have completed the form and try and return to the main site you get a error saying "session expired or cookies not enabled". I have used the preserve once method to save the session ID but it doesn't seem to be working. I can save auth cookies on the main site, but when I move to the credit card provider url the cookies don't save. Any ideas?
Maybe try this:
In commands.json:
Cypress.Commands.add('storeSessionData',()=>{
let str = [];
cy.getCookies().then((cookie) => {
cy.log(cookie);
for (let l = 0; l < cookie.length; l++) {
if (cookie.length > 0 && l == 0) {
str[l] = cookie[l].name;
Cypress.Cookies.preserveOnce(str[l]);
} else if (cookie.length > 1 && l > 1) {
str[l] = cookie[l].name;
Cypress.Cookies.preserveOnce(str[l]);
}
}
});
})
Then in your test code within the describe block:
afterEach(() => {
cy.storeSessionData();
});
This should store your session data, so you can maintain a session at the end of each test. Note, at the end of each 'it' block, the default behaviour is to clear cookies, so this mitigates that