Please post your Web Driver questions in official Web Driver forum

Friday, June 17, 2011

Selenium Tutorial: Pattern Mathing using Selenium

I must confess I have never been admirer of Regular Expression but then there are times you can not escape from it, especially while working on a website which has dynamic contents appeared in static text and you want to validate it. like -

"Validate that this text appears and there is 123 here and 456 here"

And the test condition is 123 and 456 could be any three digits but number if digits should not be more than three.

In a crude way we can at least test this -

Assert.assertTrue(selenium.getText("elementLocator").contains("Validate that this text appears and there is"));
but what if text goes wrong after "and there is"... what if more than 3 digits appear in text.
This is where pattern matching/regular expression comes for our rescue and we can use matches method of String class to achieve same. So the assertion would be -

String text = selenium.getText("elementLocator"); 
Assert.assertTrue(text.matches("Validate that this text appears and there is [0-9]{1,3} here and [0-9]{1,3} here"));
Lets try to understand two strings side by side -

Validate that this text appears and there is 123 here and 456 here
Validate that this text appears and there is [0-9]{1,3} here and [0-9]{1,3} here 

Herein 123 has been replaced with [0-9]{1,3} and 456 has been replaced with [0-9]{1,3} (well, the same set)
[0-9] means matching digit should be from 0 to 9 hence all the digits in 123 would fall in this category and so as all digits available.
{1,3} means number of digits should be at most 3
Now if application text contains more than three characters then AssertionError would be thrown.

Consider you want to tests that number of digits should be exactly three, then you could use following -

Validate that this text appears and there is 123 here and 456 here
Validate that this text appears and there is [0-9]{3} here and [0-9]{3} here 
And if you did not want to keep any constraint on number of digits then use -

Validate that this text appears and there is 123 here and 456 here
Validate that this text appears and there is [0-9]{1,} here and [0-9]{1,} here 
Here syntax {1,} is to match any number of digits

Lets see some text matching now

Validate text and dynamic text 
Validate text and [A-Za-z]{1,} text

Here [A-Za-z] means to match, small or capital letters and {1,} means to match any number of characters as discussed above. If we wanted to restrict match to only small characters then we could have used -


Validate text and dynamic text 
Validate text and [a-z]{1,} text


Examples -
("this is 123 and 2 and this is more garbage text".matches(
               "this is \\d{1,3} and \\d and this is more \\w{1,10} text")
or
"this is 123 and 2 and this is more garbage text".matches(
               "this is \\d{3} and \\d{1} and this is more \\w{1} text")
or
("this is 123 and 2 and this is more garbage text".matches(
               "this is \\d{1,} and \\d{1} and this is more \\w{1,} text")
or
"this is 123 and 2 and this is more garbage text".matches(
               "this is \\d+ and \\d and this is more \\w+ text")
or
"this is 123 and 2 and this is more garbage text".matches(
               "this is \\d{0,} and \\d and this is more \\w{0,} text")
or
"this is 123 and 2 and this is more garbage text".matches(
               "this is \\d* and \\d and this is more \\w* text")
or
"this is 123 and 2 and this is more garbage text".matches(
               "this is \\d* and \\d{0,1} and this is more \\w* text")
or
"this is 123 and 2 and this is more garbage text".matches(
               "this is \\d* and \\d? and this is more \\w* text")
or
"this is 123 and 2 and this is more garbage text".matches(".*")
To revise, important shortcuts are -
           \d is shortcut for [0-9]
    \w is shortcut for [A-Za-z0-9_]
*  is shortcut for occurs 0 or more times, that is {0, }
+ is shortcut for occurs 1 or more times, that is  {1, }
? is shortcut for occurs 0 or once, that is {0, 1}
.* matched any character sequence
"true".matches("[t]rue")
or
"true".matches("[tuv]rue")
or
"true or false".matches("[tuv]rue or [fgh][abc]lse")
subexpression and alteration -
selenium vs qtp".matches("selenium (vs|as) \\w+")

So this was some pattern matching with Selenium tests, I would add on more to this post when I encounter new scenarios for testing.
Fork me on GitHub