The question of how to perform conditional checking in Cypress.io has been asked multiple times, but provided solutions I have found do not work:
Using Cypress.io, I wish to have a conditional to check for the following element, and if it exists then perform some action:
<a data-cy="foo"><span>ABCDE</span></a>
I tried the following conditional using Cypress.io, which works fine if element 'foo' exists:
cy.log('check if element foo exists');
cy.get('a[data-cy="foo"]')
.then(($element) => {
cy.log('element foo exists');
if ($element.is(':visible')) {
cy.log('element foo visible');
}
})
...however, it errors if element 'foo' does not:
Timed out retrying: Expected to find element: a[data-cy="foo"], but never found it.
This is expected to be conditional and not an assertion.
Thank you, much appreciate any assistance.