首页 > 解决方案 > 如果我尝试在 cypress+cucumber 中的仪表板上执行操作,则会发生注销

问题描述

我正在尝试使网站自动化(https://opensource-demo.orangehrmlive.com/index.php/auth/login)我正在使用 Cypress+cucumber

用例是

1.登录应用程序

2.点击管理模块

3.单击添加员工。

但是,在第 2 步之后,页面会返回到应用程序登录页面。

我知道赛普拉斯在每一步之后都会清除 cookie。

为此,我已将会话 ID 列入白名单,但仍无法正常工作。

我的问题是我们如何在登录后保留会话状态,以便我可以采取进一步的步骤。

下面是我的步骤定义文件和屏幕截图。

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();

})

在此处输入图像描述

标签: cucumbercypress

解决方案


在您的第二个测试用例中,您已经开始了Given I have logged into system问题是当您将登录作为不同的测试用例分开时,在第二个测试用例中,用户会话没有保留(这是默认行为)。

由于您在第二个测试用例中没有会话详细信息,它会自动将您重定向到登录屏幕。要检查这一点,您可以将登录步骤添加到第二个测试用例中,它将完美运行。

但是为了解决问题,您需要在 beforeach 方法中添加“session_id”

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

如果这不起作用,您可能必须在每一步之前将登录名移到 a 中。那也能解决问题。你的代码没有问题。

有关更多信息,请参阅此内容cypress 保存会话


推荐阅读