Javascript 如何使用硒化物检查元素是否为2位数字?

Javascript 如何使用硒化物检查元素是否为2位数字?,javascript,java,junit,selenide,Javascript,Java,Junit,Selenide,我不知道如何检查元素是否有2位数字,在这种情况下,答案是19.97999999,作为阴性测试 最终字符串主机=”https://mainortest1.azurewebsites.net/shopping"; @试验 公共无效总小数2(){ $(“数量1.产品计数1”).setValue(“2”); $(“#add1.add”)。单击(); $(“总金额”)。应为(可见); //↓在这里我不知道如何检查这个总金额是否是2位数,比如XXX.YY 美元(“#总金额”)。应具有(??); }我猜你指

我不知道如何检查元素是否有2位数字,在这种情况下,答案是19.97999999,作为阴性测试

最终字符串主机=”https://mainortest1.azurewebsites.net/shopping";
@试验
公共无效总小数2(){
$(“数量1.产品计数1”).setValue(“2”);
$(“#add1.add”)。单击();
$(“总金额”)。应为(可见);
//↓在这里我不知道如何检查这个总金额是否是2位数,比如XXX.YY
美元(“#总金额”)。应具有(??);

}
我猜你指的是小数点后的两位数字

试试下面的方法

一次性解决方案:

assertTrue($("#total_amount").getText().matches("\\d*\\.\\d{2}"));
 public static Condition elementHasExactlyNumberOfDecimalDigits(final int digits) {
    return new Condition("elementHasExactlyNumberOfDecimalDigits") {
        @Override
        public boolean apply(Driver driver, WebElement webElement) {
            String text = webElement.getText();
            if (text == null || text.isEmpty()) {
                return false;
            }
            return text.matches("\\d*\\.\\d{" + digits + "}");
        }
    };
}

//then anywhere
assertTrue($("#total_amount").has(elementHasExactlyNumberOfDecimalDigits(2)));
可重用解决方案:

assertTrue($("#total_amount").getText().matches("\\d*\\.\\d{2}"));
 public static Condition elementHasExactlyNumberOfDecimalDigits(final int digits) {
    return new Condition("elementHasExactlyNumberOfDecimalDigits") {
        @Override
        public boolean apply(Driver driver, WebElement webElement) {
            String text = webElement.getText();
            if (text == null || text.isEmpty()) {
                return false;
            }
            return text.matches("\\d*\\.\\d{" + digits + "}");
        }
    };
}

//then anywhere
assertTrue($("#total_amount").has(elementHasExactlyNumberOfDecimalDigits(2)));