Sign out happens if i try to perform operation on dashboard in cypress+cucumber

Viewed 204

I am trying to automate a website(https://opensource-demo.orangehrmlive.com/index.php/auth/login) I am using Cypress+cucumber

The use case is

1.Login to the application

2.Click on Admin module

3.Click on Add Employee.

However after step 2 the page goes back to the application login page.

I know that cypress clears cookies after each step.

For that i have whitelisted the session id but still its not working.

My question how do we preserve the session state after login so that i can pursue the further steps.

Below is my step defintion file and screenshot.

Step Definition
import { Given,When,Then} from 'cypress-cucumber-preprocessor/steps';
import LoginPage from '../../support/Pages/LoginPage';
import User_AddPage from '../../support/Pages/User_AddPage';
before(() =>
{
Cypress.Cookies.defaults({
whitelist: "token"

})
beforeEach(() => {
Cypress.Cookies.preserveOnce('token');
})

})
Given('I open login page',()=>{
cy.visit("https://opensource-demo.orangehrmlive.com/index.php/auth/login");

})
When('I fill username with {string}',username=>{
cy.get("#txtUsername").type(username);

})

When('I fill password with {string}',password=>{
cy.get("#txtPassword").type(password);
})

And('I click on submit login',()=>{
cy.get("#btnLogin").click();
})

 Then('I should see homepage',()=>{
 cy.get("h1").contains("Dashboard");

 })

Given('I have logged into system',()=>
{
cy.get("a").contains("PIM").should("be.visible");
})
When('I click on Add Employee link',()=>
{
cy.get("a").contains("PIM").click();
cy.get("menu_pim_addEmployee").click();

})

enter image description here

1 Answers

In your second test case, you have started with Given I have logged into system The problem is when you separate the login as a different test case, in the second test case the user session is not preserved (this is the default behavior).

Since you don't have the session details in the second test case it will automatically redirect you to the login screen. To check this you can add the login steps to the second test case and it will work perfectly.

But for you to fix the problem, in your beforeach method you need to add the 'session_id'

beforeEach(() => {
Cypress.Cookies.preserveOnce('session_id','token');
})

If that doesn't work, you might have to move the login into a before each step. That will solve the problem too. There is no problem in your code.

refer this for more information cypress preserve session

Related