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
Thank you guys for your efforts!
I found the easiest way of my problem.
"cypress": "^13.3.0"
testIsolation: false
insidecypress.config.js
fileThe 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.
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.