1

I want to check the format of UI text against certain format e.g. C165433, A636673 etc. One way would be retrieving the text from UI and matching using regex if the result of matches() is true then test case is passed else throw exception saying not matching. I know there is no matches() in webdriver any other way in xpath to match the string from UI

nikhil udgirkar
  • 367
  • 1
  • 7
  • 23

2 Answers2

2

Yes, matches() function is not available for use via webdriver at the moment.

You have to do the matching using the programming language, for example, in Java:

WebElement element = driver.findElement(By.id("myid"));
String value = element.getText();

assertTrue(value.matches("[A-Z][0-9]{6}"));
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

Actually webdriver supports Xpath 1 .0 while matches() is the standard of Xpath 2.0 function.

Using Xpath you can use contains() to match strings from UI, but it doesn't support pattern.

But if you want to use regex pattern to match string, there is only one way as you know to retrieve text from UI and use String.matches() from programming language Java as below :-

WebElement el = driver.findElement(By..);
return (element.getText().matches("[A-Z][0-9]+"))
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73