Search code examples
cypress

How to stop Cypress saving login details


I am using cypress to automate the testing of a website

After I login with some credentials, Cypress saves them so next time I do cy.visit('/') instead of me being able to enter a second set of credentials, Cypress automatically logs me in using the first set. Is there a way i can stop cypress doing this?

Has anyone else ran into this issue?

I have tried enabling testIsolation and also adding Cypress.session.clearAllSavedSessions() to my beforeEach but neither do the job.


Solution

  • This should only occur if you have explicitly turned off test isolation. See Documentation for test isolation.

    The test code with the second credentials should be in a second test. Between the two tests, Cypress will clean up all the login artifacts for you (provided you have test isolation turned on).

    For example, see this test for saucedemo.com. (Make sure you have chromeWebSecurity: false set in cypress.config.js to run this example)

    With testIsolation set to false

    describe('login example saucedemo', {testIsolation:false}, () => {
    
      it('inventory page is blocked, requires a login', () => {
        cy.visit('https://www.saucedemo.com/inventory.html', {failOnStatusCode: false})
        cy.get('h3')
          .should('have.text', "Epic sadface: You can only access '/inventory.html' when you are logged in.")
    
        cy.get("#user-name").type("standard_user");
        cy.get("#password").type("secret_sauce");
        cy.get("#login-button").click();
    
        cy.contains('.app_logo', 'Swag Labs').should('be.visible')
      });
      
      it('visits the inventory page using prior test login', () => {
    
        // In this test, the login credentials persist
        // and we go straight to the inventory page
    
        cy.visit('https://www.saucedemo.com/inventory.html', {failOnStatusCode: false})
        cy.contains('.app_logo', 'Swag Labs').should('be.visible')
      })
    })
    

    enter image description here

    With testIsolation set to true

    describe('login example saucedemo', {testIsolation:true}, () => {
    
      it('inventory page is blocked, requires a login', () => {
        cy.visit('https://www.saucedemo.com/inventory.html', {failOnStatusCode: false})
        cy.get('h3')
          .should('have.text', "Epic sadface: You can only access '/inventory.html' when you are logged in.")
    
        cy.get("#user-name").type("standard_user");
        cy.get("#password").type("secret_sauce");
        cy.get("#login-button").click();
    
        cy.contains('.app_logo', 'Swag Labs').should('be.visible')
      });
      
      it('prior test login has been cleared, must login again', () => {
        cy.visit('https://www.saucedemo.com/inventory.html', {failOnStatusCode: false})
        cy.get('h3')
          .should('have.text', "Epic sadface: You can only access '/inventory.html' when you are logged in.")
    
        cy.get("#user-name").type("visual_user");        // using a different user
        cy.get("#password").type("secret_sauce");
        cy.get("#login-button").click();
    
        cy.contains('.app_logo', 'Swag Labs').should('be.visible')
      })
    })
    

    enter image description here