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

Distributed Load Testing with JMeter

Distributed Testing with JMeter When one JMeter client is not able to offer amount of threads required for load testing then distributed testing is used. In distributed testing - One instance of JMeter client can control number of JMeter instances and collect data from them Test plan does not need to be copied to each server, the client sends it to all servers note - JMeter will run all the threads on all the servers, hence 100 threads on 5 JMeter server would pump 500 threads in total. If many server instances are used, the client JMeter can become overloaded and so the client network connection. This has been improved in latest versions of JMeter by switching to Stripped modes, but you should always check that your client is not overloaded When Client (master) and Server (slave) nodes are on same network (no SSH required) Configure Client Node Herein client is referred as the machine controlling test execution on other JMeter nodes. This is also referred

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