Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any training

Tuesday, August 4, 2009

Regular Expression in QTP

Use of Regular Expression in QTP
We can use regular expressions in QTP when
  1. Defining Properties of an Object
  1. Verifying data using check points
  1. Parameterize an object property or check point
  •   You can use regular expressions only for values of type string.
o        By default all properties which you pass from object property are strings / Constants.
o        When using action parameters to parameterize a check point or an object repository, those action parameters data type must be string type. If you specify any other data type, that will not be treated as regular expression.
o        We can assign property values to variables in QTP when using descriptive programming. When assigning regular expression to any variable make sure that regular expression is in string format.
  • When any special character in a regular expression is preceded by a backslash (\), QuickTest searches for the literal character.
o        If your searching for a character “*”  in a string, then you need to give the pattern as “\*”
Defining Properties for an Object using Regular Expressions 
If you expect any change in the object property value at run session, Regular Expressions are useful to handle that dynamic change of the property value.
If you login to gmail.com, the browser title will be “Gmail – Inbox – MailID”
Gmail – Inbox – ksrbalu@gmail.com
Here Inbox is the selected folder name in Gmail and MailID is login user mail id.
These two are not constants. If you select “sent mail” folder immediately the browser title will be changed. In this case you may get object identification problems if you’re using the name property for the browser object. To overcome this dynamic change in the application we need to use regular expression in object properties. For this you have to use Gmail - .* - .*@gmail\.com as a Regular Expression.
Like this if you expect any object property is dynamic you can use regular expressions as required.
Specifying Regular Expressions for an object in Object Repository 
  1. Go to Object Repository. Navigation: Resources ->Object Repository
  2. Select the Object which you want to specify regular expression
  3. In the right side you can observe properties of that object
  4. Select the property which you want to specify regular expression
  5. Click on the property value of that property and the property value is editable. There is a configure button appeared with the symbol ”<#>”
  6. Click on the configure button or use Ctrl+F11 to open configure window.
  7. Select Regular Expression check box.
    1. If the property value is having any special characters then QTP will display a dialog with asking
Do you want to add the backslash (\) before each special character in order to treat it literally?
    1. If you click on yes QTP will automatically gives the backslash before every special character. So that the special characters will be treated as literal.
  1. Now specify the regular expression in constant edit box
  2. Click on OK
  3. Observe that a symbol “A.*” is added in property field. This means, that property is using regular expression.
  4. Highlight the object in application to ensure that the provided regular expression is working fine.

Specifying Regular Expressions for an object in Descriptive Programming
We can use regular expressions in static and dynamic descriptive programming types.
Click here to read about Descriptive Programming
In static descriptive programming we specify the hard coded property values in statements. 
Browser("name:=Google").Page("title:=Google").WebEdit("name:=q").Set "QTP"
Here name:=Google is a property specification. We can use regular expressions in the property values likename:=Google .*
We can assign property values with regular expressions to a constant or variable and that will be used in statements. 
Public Const oBrowser="name:=Google.*"
Public Const oPage="title:=Google"
Public Const oSearchEdit="title:=Google"
Browser(oBrowser).Page(oPage).WebEdit(oSearchEdit).Set "QTP"
In Dynamic descriptive programming we create a descriptive object using description.create by adding properties to it.
While specifying properties we can give regular expressions. By default the properties will be treated as regular expression when you added to description object. 
Set oDescription=Description.Create
oDescription("micclass").value= "Browser"
oDescription("openurl").value= "http://www.google.co.in"
Here in this URL property there are three dots (.) which can be treated as regular expressions. These dots will work as regular expressions because by default the property values will be treated as regular expressions.
There is a property called regularexpression for description object which is used to set the property values to work as regular expressions or as a literal string values.
In the Above example, set the regularexpression property to false to treat "http://www.google.co.in" as a literal string.
Set oDescription=Description.Create
oDescription("micclass").value= "Browser"
oDescription("openurl").value= "http://www.google.co.in"
oDescription("openurl").regularexpression = False
You can always use forward slash symbol (\) before every special character to treat a property value as literal character.

1 comment :