简体   繁体   中英

Cypress isn't retaining my authentication cookie when using visit() multiple times in one test

Part of my Cypress E2E tests are validating that certain URLs are fully restricted so users without the correct role cannot access them. To that end, I've written a scenario with multiple visit statements in it:

beforeEach(() => {
    cy.visit(Cypress.env(HOST_URL));
   
});


it('cannot scan without scan role', () => {
    cy.login(standard.userName, password);
    cy.get('#scanDirectoryLink_text')
        .should('not.exist');
    cy.visit(`${Cypress.env(HOST_URL)}/scanning`);
    cy.url()
        .should('not.include', 'scanning')
        .should('not.include', 'login');
});

In this scenario, I both validate that the link to the page is not visible, and that directly navigating to the URL won't succeed. My problem is that when I use the second cy.visit() command, it bumps me back to the login page. So technically it doesn't reach the page, but the reason for this is not what I intended.

I don't want to preserve my cookies between tests, but I'm hoping to preserve them across multiple cy.visit() calls so that I don't loose my authentication. Or else is there a better way to programmatically move to a new URL within my site?

Try add this in support/index.js

Cypress.LocalStorage.clear = function (keys, ls, rs) {
  //keep localStorage
  return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM