选择器的转换值

问题描述 投票:0回答:2

我有一个HTML元素<div id="the-value">30 000</div>,将为其计算内容。

我需要断言该元素的值为有效数字(> = 20000),但是value属性返回一个字符串,并且我无法使用.gte()断言它

此断言失败:AssertionError: expected '30 000' to be a number or a date

await t.expect(Selector('#the-value').value).gte(20000);

如何在断言之前将字符串转换为数字?

javascript typescript testing testcafe ui-testing
2个回答
0
投票

.expect(+Selector('#the-value').value).gte(20000)

加号将字符串转换为数字,希望可以使用

另一种方法是使用parseInt,因为如果字符串中的值不是有效数字,它将无声地失败


0
投票

当您使用testcafe时,您应该能够等待选择器并稍后在其上运行测试。所以你可以做:

const selector = await Selector('#the-value').value;
const selectorNumber = parseInt(selector);

await t.expect(selectorNumber).gte(20000);
© www.soinside.com 2019 - 2024. All rights reserved.