微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

赛普拉斯-if语句-如果元素在特定页面上不可用,请跳过部分测试代码

如何解决赛普拉斯-if语句-如果元素在特定页面上不可用,请跳过部分测试代码

页面上存在tile元素时,此代码可以完美运行,但是我试图在不同页面上运行相同的测试。因此,有些页面没有“ tile”元素标签。 我正在尝试找到一种方法来在元素不存在时跳过这段代码。感谢您抽出宝贵的时间对此进行调查。 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);              
         });
     }
    });
  }
}

解决方法

我能够使用IF语句克服此问题,因为您可能已经知道IF语句不能直接运行。但是我们可以使用这样的东西:

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 


      }
,

Cypress Real World Appa payment application to demonstrate real-world usage of Cypress testing methods,patterns,and workflows中有一个示例。如果测试在移动视口下运行,则在transactions feeds spec中有条件地关闭日期范围选择器。有关如何有条件地应用或避免选择和期望的示例,请参阅RWA和RWA中的其他测试。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。