【问题标题】:Cypress Testing - expect text to be one of赛普拉斯测试 - 期望文本是其中之一
【发布时间】:2019-06-20 15:40:02
【问题描述】:

我想针对元素数组的值进行测试,每个元素的文本内容应该是“a”或“b”之一。

it("should display for adventure & cabin charter when both are the only ones selected", () => {
    cy.get("button.expCategoryBtn")
      .contains("a")
      .click();
    cy.get("button.expCategoryBtn")
      .contains("b")
      .click();
    // the following line doesnt work
    cy.get("div.tag").each(x => {
    // the problem line:
    // I want to get the text value of each el & expect
    // it to be one of a or b
      expect(cy.wrap(x).invoke("text")).to.be.oneOf([
        "a",
        "b"
      ]);
    });
  });

编辑: 我是这样做的:

  it("should display for adventure & cabin charter when both are the only ones selected", () => {
    cy.get("button.expCategoryBtn")
      .contains("Adventure")
      .click();
    cy.get("button.expCategoryBtn")
      .contains("Cabin Charter")
      .click();
    cy.get("div.tag")
      .invoke("text")
      .should("include", "adventure")
      .should("include", "cabin_charter")
      .should("not.include", "science_and_nature");
  });

但是,我对此并不满意,并且仍然希望得到一些反馈,了解当我们要断言多个值中的一个时,正确的测试方法是什么。谢谢。

【问题讨论】:

    标签: javascript testing bdd cypress


    【解决方案1】:

    听起来你好像在尝试conditional testing,这不是最佳做法。

    无论如何,您都可以这样做:

        cy.get("div.tag").each(x => {
          expect(x.text()).to.be.oneOf([
            "a",
            "b"
          ]);
        });
    

    【讨论】:

      猜你喜欢
      • 2023-02-14
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多