While using Cypress - I would like to understand ways to do action vs not do action based on element found or not. I tried all below with help from Google search - but ended up with just a bunch of hours wasted:
cy.xpath('').then($ele => {}) - throws Timed out retrying after 20000ms: Expected to find element: //div[text()='hello']//div, but never found it.cy.xpath('').then(ele => { eleLenght = Cypress.$(ele).length}) - throws Timed out retrying after 20000ms: Expected to find element: //div[text()='hello']//div, but never found it.cy.xpath('').then($ele => {}).catch({}) - catch is not definedcy.xpath('').its('lenght').then($ele => {}) - throws Timed out retrying after 20000ms: Expected to find element: //div[text()='hello']//div, but never found it.cy.xpath('').should('be:visisble').then($ele => {}) - throws Timed out retrying after 20000ms: Expected to find element: //div[text()='hello']//div, but never found it.My case: If an element is present - I want to try to delete it, but if not present - gracefully continue without throwing the annoying error as above!
Can someone please help here? Conditional testing doc provided in cypress does'nt help at all!
You can easily do it with jquery notation:
if (Cypress.$("#your-element-id").length > 0) {
//do something
} else {
//do nothing
}
Have you tried below?
cy.xpath('').should('have.length.gt',0).then($ele => {console.log($ele)})