skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    Thank you guys for your efforts!

    I found the easiest way of 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'
      },
      
    });
    

  2. The logout is happening as soon as you run the second spec. That’s because Cypress clears down data between tests.

    If you follow the documentation at Cypress cy.session(), you should find the first example is close to what you currently do.

    Cypress.Commands.add('login', (username, password) => {
      cy.session([username, password], () => {
        cy.visit('/login')
        cy.get('[data-test=name]').type(username)
        cy.get('[data-test=password]').type(password)
        cy.get('form').contains('Log In').click()
        cy.url().should('contain', '/login-successful')
      })
    })
    

    Note, the login would be called before every test, but cy.session() does not run it on every call. On 2nd and 3rd calls it uses cached data to reinstate the login.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search