jeudi 2 décembre 2021

Cypress - If Else based on Presence of element without throwing 'Expected to find element: but never found it.'

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:

  1. cy.xpath('').then($ele => {}) - throws Timed out retrying after 20000ms: Expected to find element: //div[text()='hello']//div, but never found it.
  2. 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.
  3. cy.xpath('').then($ele => {}).catch({}) - catch is not defined
  4. cy.xpath('').its('lenght').then($ele => {}) - throws Timed out retrying after 20000ms: Expected to find element: //div[text()='hello']//div, but never found it.
  5. 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!

Aucun commentaire:

Enregistrer un commentaire