简体   繁体   English

赛普拉斯-如何从站点注销

[英]Cypress - how to logout from site

Is in Cypress any possibility to log out from page if you are logged in? 如果您已登录,赛普拉斯是否有可能从页面注销? Or to check if I'm logged? 还是检查我是否已登录?

My problem is that if I'm running tests by one in browser then Cypress always need to log in. But if I start it from cmd it only needs to log in on the first test but at the next one you are already logged in. 我的问题是,如果我正在浏览器中运行一个测试,则赛普拉斯总是需要登录。但是,如果我从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();

I've got Cypress tests running for several environments, one of those has issues with the login state. 我已经在多种环境下运行了赛普拉斯测试,其中之一存在登录状态问题。 Deleting cookies helps sometimes, but not always. 删除Cookie有时会有所帮助,但并非总是如此。 So to keep the tests stable I decided to create an if statement (what in itself isn't a best practice) to go to the known application state as fast as possible. 因此,为了保持测试的稳定性,我决定创建一条if语句(本身不是最佳实践)以尽可能快地进入已知的应用程序状态。

What I do is using the custom command below in every beforeEach() that could use a login: 我要做的是在可能使用登录名的每个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()
        }
    })
})

What is does is checking if the browser is already logged in, since .user-profile-name should exist. 这样做是检查浏览器是否已经登录,因为.user-profile-name应该存在。 If it does not exist the browser isn't logged in, so Cypress needs to click the .login-button button. 如果不存在,则浏览器将无法登录,因此赛普拉斯需要单击.login-button按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM