我要针对元素数组的值进行测试,并且每个元素的文本内容应为'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");
});
但是,我对此不满意,并且仍然希望获得一些反馈,以便我们在声明多个值之一时正确的测试方式。谢谢。
答案 0 :(得分:0)
听起来好像您正在尝试conditional testing,但这不是最佳实践。
无论如何,您都可以这样做:
cy.get("div.tag").each(x => {
expect(x.text()).to.be.oneOf([
"a",
"b"
]);
});