首页 > 解决方案 > 当我们要检查该类是否存在时,如何使用条件语句?

问题描述

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

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

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

我写了一个例子:

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

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

但是条件总是正确的,即使我使用 find() 和 children() 并且“TestClass”类不存在。“TestClass”是“footerWrapper”类的孩子

应该怎么写?有谁有想法吗?

标签: javascripthtmlautomated-testscypress

解决方案


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

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

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

推荐阅读