简体   繁体   English

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

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

I need some assistance.This is a weird one.Basically I am writing out some testcafe tests where: a user goes to the order summary page and verifies that the total on the check out page matches the total on the order details page.我需要一些帮助。这是一个奇怪的问题。基本上我正在写一些 testcafe 测试,其中: 用户转到订单摘要页面并验证结帐页面上的总数是否与订单详细信息页面上的总数匹配。

The issue: There are instances where the order details total is off by a penny(not a bug worthy of fixing any time soon. according to the developers).So for example, on the checkout page your order total is $3.50.问题:在某些情况下,订单详细信息总额相差一分钱(不是一个值得很快修复的错误。根据开发人员的说法)。例如,在结帐页面上,您的订单总额为 3.50 美元。 On the order details page the total is $3.51在订单详细信息页面上,总额为 3.51 美元

Is there a way to combat a penny extra in a testcafe assertion?有没有办法在 testcafe 断言中打击额外的一分钱?

Here is what my assertion looks like:这是我的断言的样子:

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

totalOnCheckoutPage and totalOnOrderDetails are selector variables. totalOnCheckoutPage 和 totalOnOrderDetails 是选择器变量。

You can write custom util to round.您可以将自定义 util 编写为 round。 Sample样本

 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))

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

You can try Within assertion method您可以尝试内断言方法

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

extract the inner text of both values, and add the needed round off range提取两个值的内部文本,并添加所需的舍入范围

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM