How can I overcome a situation when the element is not found. I am automating a form where intentionally I am giving a duplicate name so an error message will be shown "Limit Name already exist" based on that I have written
add.limitName().type('Movie Limits') // Giving duplicate name "Movie Limits"
cy.get('.has-error > .col-sm-6 > [data-bv-validator="remote"]').then((wizard) => {
if(wizard.text().includes('Limit Name already exist.')) // Duplicate Limit Name Check
{
add.limitName().clear()
add.limitName().type('Movie TR Limits') // Giving another Name
}
})
This works perfectly fine if its a duplicate value but if its not a duplicate value then this element wont be found and an error is thrown and the test fails. How can i write in such a way if its not duplicate it carries on and if it is the above code comes into action ?
It seems perfectly legit for the test to fail when the error is missing, but to answer your question
Split off the last selector and check it with jQuery (which does not cause a fail) instead of including it in the Cypress
cy.get().