Cypress 测试 - 期望文本成为其中之一

Kay*_*ote 4 javascript testing bdd cypress

我想测试元素数组的值,每个元素的文本内容应该是“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"
      ]);
    });
  });
Run Code Online (Sandbox Code Playgroud)

编辑:我是这样做的:

  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");
  });
Run Code Online (Sandbox Code Playgroud)

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

Zac*_*ist 9

听起来有点像您正在尝试进行条件测试,这不是最佳实践。

无论如何,你可以这样做:

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