在 testcafe 中对“.1”关闭的数字执行断言的最佳方法是什么

What is the best way to perform an assertion for numbers that are off by ".1" in testcafe

我需要一些 assistance.This 很奇怪 one.Basically 我正在写一些 testcafe 测试,其中: 用户转到订单摘要页面并验证结帐页面上的总数是否与订单详细信息页面上的总数相匹配。

问题: 在某些情况下,订单详细信息总额会相差一分钱(根据开发人员的说法,这不是一个值得很快修复的错误)。因此,例如,在结帐页面上,您的订单总额为 3.50 美元。在订单详情页面上,总计为 3.51 美元

有没有办法在 testcafe 断言中对抗额外的一分钱?

这是我的断言:

await t
        .expect(totalOnCheckoutPage)
        .eql(totalOnOrderDetails);

totalOnCheckoutPage 和 totalOnOrderDetails 是选择器变量。

你可以试试Within断言方法

await t.expect(5).within(3, 10, 'this assertion will pass');

提取两个值的内部文本,并添加所需的舍入范围

https://devexpress.github.io/testcafe/documentation/test-api/assertions/assertion-api.html#within

您可以编写自定义 util 来进行舍入。样本

function round(x, precision) {
    var y = +x + (precision === undefined ? 0.5 : precision/2);
    return y - (y % (precision === undefined ? 1 : +precision));
}
console.log(round(3.51, 0.1))
console.log(round(3.55, 0.1))

参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round