Home > Mobile >  Cypress.io - problem with logging out from page
Cypress.io - problem with logging out from page

Time:06-07

There's an element on a page that I need to test with Cypress to log out from the page :

<a _ngcontent-jee-c99="" > Logout </a>

I'm not familiar with this tool and programming, this is my fresh start. How can I implement solution for that ?

In the same case I had to log in, but it was quiet simple due to button class for it :

cy.get ('input')
    .click()
cy.get('input[type="email"]')
    .should('be.visible')
    .type('[email protected]')
cy.get('button[type="submit"]')
    .click()
cy.get('input[type="password"]')
    .should('be.visible')
    .type('password')
cy.get('button[type="submit"]')
    .click()

CodePudding user response:

A good practice is to add test IDs to elements that you want to interact with e.g.

<a href="https://google.com"  data-testid="logout">Logout</a>

Then you can do:

cy.get('[data-testid="logout"]').click()

The example you provided seems pretty messy. You could probably use something like:

cy.get('.logout').click()

but I would strongly recommend using the test IDs. You can find more information and best practices on selectors here in the Cypress docs.

CodePudding user response:

You can use a combination of selector a and text Logout to find and perform click.

cy.contains('a', 'Logout').click()

CodePudding user response:

The class shown in lang-js prettyprint-override">cy.get('a.logout') // element is <a>, class to find is .logout .should('be.visible') .click()

After the logging-in click here

cy.get('button[type="submit"]').click()

the Logout button may not show for a short period, so adding .should('be.visible') allows the test to wait for the button to appear.

  • Related