Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How effective is it adding a logout command at the end of a test case?

I am running a test suite (test1, test2, test3) in Cypress. I noticed when I run this suite for the first time, they all pass. On the next test run, they fail. It seems that it is failing because the data from test3 has not been removed from the cypress browser when test1 begins to run. Could this be a load issue or would adding a logout command at the end of each test case be the solution? Not sure what would be the solution for this.

like image 488
csb00 Avatar asked Nov 19 '25 23:11

csb00


2 Answers

It seems that it is failing because the data from test3 has not been removed from the cypress browser. First step is to find out where that data being stored ex: cookie, session storage or local storage? Once you found it, you can choose appropriate method to delete data. Incase you are interested in clearing all u can do something as below

beforeEach(() => {
 cy.window().then(win => win.sessionStorage.clear());
 cy.clearCookies();
 cy.clearLocalStorage();
 });

Make sure above syntax is correct from cypress.io.

like image 131
Dileep17 Avatar answered Nov 22 '25 16:11

Dileep17


Have you tried adding a step to clear the cookies?

Cypress.Commands.add('logout', () => {
    cy.clearCookies().then(() => {
        cy.getCookies().should('be.empty').visit('/')
    })
});
like image 38
Kidby Avatar answered Nov 22 '25 16:11

Kidby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!