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

Appium and android mobile app automation

Next appium and Android mobile app automation video tutoria l is live. If you are new to appium then please check - appium-tutorial This video tutorial covers - Start vysor (Just for this session and not mobile automation :)) Start appium and start appium inspector Desired Capabilities platformName - Android deviceName - L2N0219828001013 (as seen on "adb devices") Saved Capability Sets Start Session Scan app elements using appium inspector Get appPackage and appActivity using "APK info" app Install "APK info" app and open app whose appPackage and appActivity are required i.e. calculator Check top section of app icon com.android.calculator2 is app package com.android.calculator2.Calculator is app activity testng.xml file settings for running Android app tests Test details com.seleniumtests.tests.mobile.AndroidAppTest and CalculatorScreen class View beautiful STF test report  

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

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 -