Dom*_*ála 3 javascript testing automation automated-tests cypress
如果您已登录,赛普拉斯是否有可能从页面注销?或者检查我是否登录?
我的问题是,如果我在浏览器中运行一个测试,那么 Cypress 总是需要登录。但是如果我从 cmd 启动它,它只需要在第一个测试中登录,但在下一个测试中你已经登录了。
// Visit the site
cy.visit(
"https://zeteodemo.uat.creasoft.cz/login?ReturnUrl=%2fVehicleInsuranceV2%2fCompare%3fOsobniCislo1%3d1%26ProductsFilter%3dAXA-ONLINE"
);
// Enter username
cy.get(".login__username")
.type("tester")
.should("have.value", "tester");
// Enter password
cy.get(".login__password")
.type("1")
.should("have.value", "1");
// Click the login button
cy.get(".button-login").click();
Run Code Online (Sandbox Code Playgroud)
我已经在多个环境中运行了 Cypress 测试,其中一个环境存在登录状态问题。删除 cookie 有时会有所帮助,但并非总是如此。因此,为了保持测试稳定,我决定创建一个 if 语句(这本身不是最佳实践)以尽快进入已知的应用程序状态。
我所做的是在每个beforeEach()可以使用登录名的地方使用下面的自定义命令:
Cypress.Commands.add('login', function () {
cy.get('body').then($body => {
if ($body.find('.user-profile-name').length === 1) {
cy.log('Already logged in')
} else {
cy.get('.login-button').click()
}
})
})
Run Code Online (Sandbox Code Playgroud)
什么是检查浏览器是否已经登录,因为.user-profile-name应该存在。如果它不存在,则浏览器未登录,因此赛普拉斯需要单击该.login-button按钮。