if else in Cypress:
if else is commonly used in Cypress because sometimes any element is exist and sometimes another element is exist instead of it.
Check if element is present or not, so that certain steps can be performed if element is present. Else certain different steps can be performed if element is not present.
cy.get('.class_where_elements_exist').then($body => { if ($body.find('.class-name-where-element-changing').length > 0) { cy.contains('Add Button').click(); } else { cy.contains('Delete Button').click() } });
Code Explanation:
length > 0 = It means element exist.
$body = It’s a JQuery.
find = Will Find Element in specific mentioned class by using JQuery Where element is repeating/Changing.
For more Valuable blogs Click here
For Cypress blogs Click here
Leave a comment