I am having a problem with if element exist then do something. As an example:
JavaScript
x
4
1
if (cypress.$('.row > .text-right > .btn').length > 0) {
2
cy.get('.row > .text-right > .btn').click();
3
}
4
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
JavaScript
1
4
1
if (element.exists) {
2
cy.get(element).click();
3
}
4
Advertisement
Answer
One way you do it is to get the parent of the element in question, which you know would be displayed every time.
JavaScript
1
8
1
cy.get('parent element').then(($ele) => {
2
if ($ele.find('.row > .text-right > .btn').length > 0) {
3
cy.get('.row > .text-right > .btn').click()
4
} else {
5
//Do Something
6
}
7
})
8