'Validating a string with dynamic number in selenium using Assert
I want to validate this string "Total 13 items are available" using assert. So how can i validate that when the number "13" in string is dynamic i.e can be any numeric value
Solution 1:[1]
- Find the element that contains the text.
- Read the text inside it.
- Assert
It should look something like this:
WebElement myElm = driver.findElementByCss("CSS-Selector here");
// or find element by Xpath
String innerText = myElm.getAttribute("text");
// or "value"
// Take the string and assert it:
bool goodText = "Total 13 items are available";
Assert.areEquals(goodText, innerText);
You can take the number at innerText, remove all the letters and keep only the digits. Then convert the string into an integer and check it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tal Angel |
