【问题标题】:How to use condition statement when we want to check that class exist or not?当我们要检查该类是否存在时,如何使用条件语句?
【发布时间】:2019-01-10 12:24:46
【问题描述】:

在 Cypress 中,我想创建一个条件来检查是否创建了一个类,如果是,则执行相同的步骤。

我在以下位置看到了一个文档:

https://docs.cypress.io/guides/core-concepts/conditional-testing.html#A-B-campaign

我写了一个例子:

 cy.get('.footerWrapper').then((div) => {
        if (div.find('.TestClass')) {
            cy.log('xxx')

        } else {
            cy.log('yyy')
        }
    })

但条件始终为真,即使我使用 find() 和 children() 并且“TestClass”类不存在。 “TestClass”是“footerWrapper”类的子类

应该怎么写?有人知道吗?

【问题讨论】:

    标签: javascript html automated-tests cypress


    【解决方案1】:

    您应该检查.length 属性,因为空数组[] 总是在“if”条件内给出true

     cy.get('.footerWrapper').then((div) => {
            if (div.find('.TestClass').length) {
                cy.log('xxx')
    
            } else {
                cy.log('yyy')
            }
        })
    

    【讨论】:

      猜你喜欢
      • 2021-10-04
      • 2019-12-12
      • 2017-04-01
      • 2015-09-04
      • 2013-04-19
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多