Skip to main content

Selenium Tutorial: Pattern Mathing using Selenium

Note: If you are new to java and selenium then start with selenium java training videos.
 
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.

Popular posts from this blog

Selenium Tutorial: Ant Build for Selenium Java project

Ant is a build tool which could be used to have your tests running either from command line or from Hudson CI tool. There is detailed documentation available for ant here but probably you need to know only a little part of it for you selenium tests. The essentials which are needed to know are: Project Target (ant execution point and collection of tasks) Tasks (could be as simple as compilation) And there would usually be following targets for Selenium tools - setClassPath - so that ant knows where you jar files are loadTestNG - so that you could use testng task in ant and use it to execute testng tests from ant init - created the build file clean - delete the build file compile - compiles the selenium tests run - executes the selenium tests Here is my project set up for ant -

Verify email confirmation using Selenium

Note: If you are new to java and selenium then start with selenium java training videos .     Email confirmation seems to be integral part of any registration process. I came across an application which lets you provide your email address. You can follow the sign up link in you mail and then complete the registration process. Lets consider we provide GMail address for it. Now if were to use only Selenium then we would have to follow following steps - Launch GMail using Selenium; Some how search for new mail in the list of available mails; Some how click on it; Parse the mail message; Get the registration link; Follow up with registration process What do you think of an approach in which you can

Real Time JMeter Result Using Backend Listener

Since JMeter 2.13 Backend Listener has been available to create real time graph of JMeter Test. Following tutorial explain the entire process in detail. At the end of this tutorial you would be able to create JMeter Live Test Result dashboard similar to following - This tutorial borrows information from many sources and my own experiments with JMeter live reporting dashboard. I have added source of information wherever applicable But before we can build such a snazzy JMeter Live Reporting dashboard we need to understand two more components - influxDB (a time series database) and Grafana Dashboard This is a big tutorial, so take deep breath :-) and follow on. Once you complete set up specified in this tutorial then you can watch JMeter Training Video Tutorial to watch this in action. What is Time Series Database? A time series is a sequence of data points , typically consisting of successive measurements made over a time interval . Examples of time ...