I am logging into application by using Cy.session() . it stores the session and restore the session for Next tests But some times when Cypress test runner automatically reloads on code update then it does not login and gives Error. May be the possible reason is cookie expire or something else . My code is :
login(username,password){
cy.session('sessionId', () => {
cy.visit('/');
this.getUsername().type(username);
this.getPassword().type(password);
this.getLoginButton().click();
cy.url().should('contain', '/inventory.html');
})
cy.visit('/inventory.html');
cy.url().should('contain', '/inventory.html');
}
Screenshot Attached :
I tried by adding the option cacheAcrossSpecs: true but it does not work
You could add a validate function to the session see Examples
- If validation fails after restoring a session, setup will re-run.
This should ensure the cookie is renewed after it expires, or if the cause is another factor you will only need to check the desired effect is true: cy.url().should('contain', '/inventory.html')
cy.session('sessionId',
() => {
cy.visit('/')
this.getUsername().type(username)
this.getPassword().type(password)
this.getLoginButton().click()
},
{
validate: () => {
cy.url().should('contain', '/inventory.html')
}
}
)