首页 > 解决方案 > Cypress - if - else 条件不起作用

问题描述

我需要使用 Cypress 自动执行 if-else 条件,但它在我的脚本中不起作用。我需要一个“如果”元素存在(或可见),然后单击“是”,否则继续。但就我而言,如果第一个条件不存在,则测试尝试以任何方式单击“是”。

it('Logout', () => {
      const out = new logout();
      const lged = new logged();
      
      lged.logged().then($button => {
        if ($button.is(':visible')){
          lged.logged().click()
          out.bar().click()
          out.clickout()
        }
        else {
          out.bar().click()
          out.clickout()
        }
      })
    })

在我的 pageObjects 中,我有“记录”类:

    class logged {

    logged(){
        return cy.contains('Yes', { timeout: 10000 })
    }
}

export default logged

标签: cypressqa

解决方案


推荐阅读