检查柏树测试​​中的近似属性值

Check for approximate attribute value in cypress test

我编写了以下 Cypress 测试来测试我的 Angular 应用程序中的 SVG 元素:

it("should have the right size", () => {
  cy.get(".rectangle")
    .first()
    .should("have.attr", "width", "80")
    .and("have.attr", "height", "60");
});

遗憾的是,widthheight 的值可能略有不同。所以我宁愿允许一些小偏差。

推荐的最短 and/or 最优雅的方法是什么?

我从 jonrsharpe's comment, based on the should-callback and 推导出以下解决方案:

it("should have the right size", () => {
  cy.get(".rectangle")
    .first()
    .should(($rect) => expect(+$rect.attr("width")).to.be.closeTo(80, 10))
    .and(($rect) => expect(+$rect.attr("height")).to.be.closeTo(60, 10));
});

或者,我们可以将两个期望组合到一个函数中:

it("should have the right size", () => {
  cy.get(".rectangle")
    .first()
    .should(($rect) => {
      expect(+$rect.attr("width")).to.be.closeTo(80, 10);
      expect(+$rect.attr("height")).to.be.closeTo(60, 10);
    });
});