简体   繁体   中英

Cypress: if element exist then do something

I am having a problem with if element exist then do something. As an example:

if (cypress.$('.row > .text-right > .btn').length > 0) {
            cy.get('.row > .text-right > .btn').click();
          }

the problem here is that cypress aborts the test if the button doesn't exist but that's exactly when cypress shouldn't abort, it should do nothing and continue.

I need a solution for

if (element.exists) {
   cy.get(element).click();
    }

One way you do it is to get the parent of the element in question, which you know would be displayed every time.

cy.get('parent element').then(($ele) => {
    if ($ele.find('.row > .text-right > .btn').length > 0) {
        cy.get('.row > .text-right > .btn').click()
    } else {
        //Do Something
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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