Issue
I have no idea how to check if the element has 2 digits or not, in this case, the answer is 19.979999999 as a negative test.
final String HOST = "https://mainortest1.azurewebsites.net/shopping";
@Test
public void totalDecimalIs2() {
$("#quantity1.product_count_1").setValue("2");
$("#add1.add").click();
$("#total_amount").shouldBe(visible);
//↓Here I am not sure how to check is this total_amount is 2 digits or not, like XXX.YY
$("#total_amount").shouldHave(???);
}
Solution
I guess you meant 2 digits after the decimal point.
Try the following
Onetime solution :
assertTrue($("#total_amount").getText().matches("\\d*\\.\\d{2}"));
Reusable solution:
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)));
Answered By - Danny Briskin