1

This code works perfectly when there is a tile element present on the page, but I am trying to run the same test on different pages. So, a few pages don't have a "tile" element tag. I am trying to find a way to skip this piece of code if the element isn't present. Thank you for taking the time out to look into it. HERE is the screenshot of the code

if ((cy.get('bx--tile') {
  .its('length').should('be.gt', 0))) {
    cy.get('bx--tile').each(($el, index, $list) => {
     let url = $el.attr('data-href');
     if (url) {
       let options = Object.assign({}, {url: url});
         cy.request(options).then(resp => {
           expect(resp.status).to.eq(200);              
         });
     }
    });
  }
}
rmlockerd
  • 3,776
  • 2
  • 15
  • 25
  • Try the solution mentioned in https://stackoverflow.com/questions/56145926/how-to-check-if-element-exists-using-cypress-io – A J Oct 06 '20 at 21:08

2 Answers2

1

I was able to overcome this using IF statement, as you might already know IF statement doesn't work directly. But we can use something like this:

cy.get("body").then($body => {

        if  ($body.find('g[class=nodes]').length > 0) {                      

             //this will help you to bypass and desired condition

        }


       else if  ($body.find('section[class=bx--structured-list]').length > 0){   

            //bypassing *Structed list*

       }


      else  {
          //code which you want to executive/run for default case 


      }
-1

There is an example in the Cypress Real World App, a payment application to demonstrate real-world usage of Cypress testing methods, patterns, and workflows. In the transactions feeds spec we conditionally close a date range picker if the test is running under a mobile viewport. Refer to this test and others in the RWA for examples of how to conditionally apply or avoid selections and expectations.

Kevin Old
  • 969
  • 1
  • 9
  • 20