【问题标题】:Cypress - Or Condition on Drop Down Button List赛普拉斯 - 或下拉按钮列表的条件
【发布时间】:2021-10-16 05:51:00
【问题描述】:

我有一个当前的赛普拉斯检查,它会迭代一组下拉菜单,检查下拉菜单是否有“从不符合更新条件”作为值。但是,我必须更改应用程序,以便一个下拉菜单具有“有时更新”。 如果这两个值中的任何一个在下拉列表中,我需要修改此测试以使其通过。不知道该怎么做。

cy.get("@updateRules")
  .find("button")
  .each(button => expect(button).to.contain("Never eligible to update"));

【问题讨论】:

    标签: automation cypress


    【解决方案1】:

    您还可以使用 chai 断言 .oneOf 断言目标是给定数组列表的成员。

    cy.get("@updateRules")
      .find("button")
      .each($button => {
        expect($button.text().trim()).to.be.oneOf(["Never eligible to update", "Updates Sometimes"])
      })
    

    【讨论】:

      【解决方案2】:

      可能最简单的就是正则表达式

      cy.get("@updateRules")
        .find("button")
        .each($button => {
          cy.wrap($button).contains(/Never eligible to update|Updates Sometimes/)
        })
      

      cy.get("@updateRules")
        .find("button")
        .each($button => {
          expect($button).to.match(/Never eligible to update|Updates Sometimes/)
        })
      

      Chai 有一个satisfy 方法

      cy.get("@updateRules")
        .find("button")
        .each($button => expect($button.text()).to.satisfy(text => {
            return text.includes('Never eligible to update') || 
                   text.includes('Updates Sometimes')
          })
        )
      

      jQuery OR 条件在备用值之间使用逗号。

      您需要在选择器内将.contains() 转换为:contains()

      cy.get("@updateRules")
        .its('length')
        .then(ruleCount => {
          cy.get("@updateRules")
            .find("button:contains('Never eligible to update'), button:contains('Updates Sometimes')")
            .its('length')
            .then(matchCount => expect(ruleCount).to.eq(matchCount))
        })
      

      【讨论】:

        【解决方案3】:

        你可以OR使用正则匹配它:

        cy.get("@updateRules")
          .find("button")
          .each($button => {
            cy.wrap($button).contains(/Never eligible to update|Updates Sometimes/g)
          })
        

        【讨论】:

        • 这和我的回答一样。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-23
        • 1970-01-01
        相关资源
        最近更新 更多