dimanche 23 août 2020

cypress check if element exist, throw exception when element not exist

I'm trying to get an indication of an element exists or not. if the element exists so I want to click on button X if not so click on button Y. but the problem is when the element does not exist it throws an exception and stop the test. enter image description here part of my code

let elementKey = '#up-picker'
Cypress.get('body').then($res=>{
  if($res.find(elementKey).length>0){
   // click on button X
  }else{
    // click on button Y
  }
})

I have an element with this id, but sometimes because the logic in the UI can change. but it still not find me this element. I checked more posts in stack overflow and no answer works for me.

Aucun commentaire:

Enregistrer un commentaire