【问题标题】:Cypress- if statement - skip the part of test code if the element isn't available on the particular pageCypress- if 语句 - 如果元素在特定页面上不可用,则跳过部分测试代码
【发布时间】:2021-01-20 12:55:11
【问题描述】:

当页面上存在 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);              
         });
     }
    });
  }
}

【问题讨论】:

标签: testing automation conditional-statements rendering cypress


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我能够使用 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 
    
    
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-29
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多