admin管理员组

文章数量:1312826

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.

// Visit the site
cy.visit(
  "=%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();

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.

// 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();
Share Improve this question edited Aug 28, 2019 at 12:43 Joshua 3,1863 gold badges26 silver badges41 bronze badges asked Aug 19, 2019 at 14:29 Dominik SkálaDominik Skála 8315 gold badges14 silver badges30 bronze badges 2
  • I might be pletely off here and pretty interested in what answers you'll get. But have you considered using cookies (docs.cypress.io/api/cypress-api/cookies.html#Syntax)? Cypress deletes cookies after each test but you could preserve a cookie of logged in user a based on its value decide if you need to click the login button. It's just an idea though, never done that before.. – DurkoMatko Commented Aug 19, 2019 at 14:41
  • Yeah, I was trying cookies but it doesn't work. Maybe I don't get it right and I was using them wrong so I'm asking. – Dominik Skála Commented Aug 19, 2019 at 14:49
Add a ment  | 

1 Answer 1

Reset to default 9

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. 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.

What I do is using the custom mand below in every beforeEach() that could use a login:

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. If it does not exist the browser isn't logged in, so Cypress needs to click the .login-button button.

本文标签: javascriptCypresshow to logout from siteStack Overflow