I want to do a conditional testing in cypress. The situation is, that I want to achieve this:
If an alert modal window is present on the page , then click the YES button, otherwise do nothing.
The structure of the alert is something like:
<div class="swal2-container">
<h2>You are about to leave this page</h2>
<button type="button" class="swal2-confirm">Yes</button>
</div>
I tried it this way:
cy.get('body').then((body) => {
if (body.find('button.swal2-confirm').length > 0) {
cy.get('button.swal2-confirm').contains('Yes').click();
}
});
but this does nothing, it doesn't click the button. In test runner I just see as an output get ... body and it is skipped.
i also tried to replace body with $body with no luck.
cy.wait(4000)before your code and do the action that produces the modal window window. Your code looks correct. - Alapan Das