Search code examples
javascriptautomationcypress

Automatically logged out while interacting with elements after login in Cypress


Automation Testing using Cypress + JavaScript

Issue

I am unable to stay logged in after login while running my script. A few seconds after logging in, it redirects me to a login page.

Expected Result

I want to log in until the entire script runs.

Please find below code I used:

LoginPage.js
//Page objects and functions for login page 

class LoginPage{
    
    navigate() {
        cy.visit('stageURL')
    }

    firstLogin(){
        //cy.wait(10000)
        cy.get('.MuiButton-root', {timeout: 30000})
            .should('be.visible')
            .and('be.enabled')
            .and('have.text', 'Sign In')
            .click()
    }

    enterEmail(username) {
        cy.get('#username')
            .clear()
            .type(username)
        return this
    }

    enterPassword(pswd) {
        cy.get('#password')
            .clear()
            .type(pswd)
        return this
    }

    submit() {
        cy.get('#kc-login').click()
    }

}
export default LoginPage
LoginSpec.js
/// <reference types="cypress" />

import LoginPage from "./PageObjects/LoginPage"
describe("Cypress POM Test Suite", function () {
    before(function () {
        cy.fixture('credentials').then(function (testdata) {
            this.testdata = testdata
        })
    })
    it("Login with valid credentials", function () {
        const login = new LoginPage();
        login.navigate();
        login.firstLogin();
        login.enterEmail(this.testdata.username)
        login.enterPassword(this.testdata.password)
        login.submit();
    });
});

Suite.js
import './LoginSpec'
import './SitesSpec'
import './LogoutSpec'

I run a Suite.js file. (I didn't attach other files here except login. Let me know if you require for solution)

What I tried:

I tried both of available options on how do I stop a website from automatically logging me out when I'm using cypress?. But it's not working for me.

Any solution? Thanks!

Note: It's working fine on my colleague's machine. I am using Windows 10


Solution

  • Thank you guys for your efforts!

    When I used cy.session() it was giving me an error for experimentalsessionandorigin. After enabling experimentalsessionandorigin:true, it was showing me another error for testIsolation. But adding testIsolation:false came with a new error which resolved by updating a Cypress.

    In short, the following steps fix my problem.

    1. Upgrade cypress - "cypress": "^13.3.0"
    2. Add testIsolation: false inside cypress.config.js file
    module.exports = defineConfig({
      
      video: true,
       
      e2e: {
        setupNodeEvents(on, config) {
          // implement node event listeners here
        },
        chromeWebSecurity: false,
        testIsolation: false,
        specPattern: 'cypress/integration/examples/*.js'
      },
      
    });
    

    Here is the link where I found the solution: https://github.com/cypress-io/cypress/issues/24497

    You can find more information from Cypress documentation.

    Thanks!