Skip to main content

Regular Expression in QTP

Regular Expression


Regular Expression:
♦  Regular Expressions enable Quick Test to identify objects and text strings with varying values.Regular expressions are
    used when the objects value is dynamically changed.

♦  A regular expression is a string that specifies a complex search phrase. By using special characters such as a period (.),
    asterisk (*), caret (^), and brackets ([ ]), etc. you define the conditions of the search.

Steps to Specify 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.

 a.  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?

 b.  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.


8.  Now specify the regular expression in constant edit box.

9.  Click on OK.

10. Observe that a symbol “A.*” is added in property field. This means, that property is using regular expression.

11. Highlight the object in application to ensure that the provided regular expression is working fine.



In Dynamic descriptive programming we create a descriptive object using description.create.

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.inas a literal string.
Set oDescription=Description.Create
oDescription("micclass").value= "Browser"
oDescription("openurl").value= "http://www.google.co.in"
oDescription("openurl").regularexpression = False
oDescription("openurl").regularexpression = False  
Note:
You can always use forward slash symbol (\) before every special character to treat a property value as literal character.

Use of Regular Expression in QTP
We can use regular expressions in QTP when

1. Defining Properties of an Object

2. Verifying data using check points

3. Parameterize an object property or check point

Note:

♦  You can use regular expressions only for values of type string.

♦  By default all properties which you pass from object property are strings / Constants.

♦  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.

♦  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.

♦  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.

Example:
If you login to gmail.com, the browser title will be “Gmail – Inbox – MailID”
Gmail – Inbox – 
qtpworld@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.

Comments

Popular posts from this blog

JMeter Exceeded Maximum Number of Redirects Error Solution

While running performance test, JMeter allows maximum 5 redirects by default. However, if your system demands more than 5 redirects, it may result in JMeter exceeded maximum number of redirects error. In this post, we have listed down steps to overcome this error. Actual error in JMeter: Response code: “Non HTTP response code: java.io.IOException” Response message: “Non HTTP response message: Exceeded maximum number of redirects: 5” This error is noticed because  JMeter  allows maximum 5 redirects by default and your system may be using more than 5 redirects. You need to increase this count to more than 5 in jmeter.properties file. Follow below steps to achieve this. Navigate to /bin directory of your JMeter installation. Locate jmeter.properties file and open it in any editor. Search for “httpsampler.max_redirects” property in opened file. Uncomment the above property by removing # before it. Change to value to more than 5 Eg. 20. Save the file and restart JMet...

SSO with SAML login scenario in JMeter

SAML(Security Assertion Markup Language) is increasingly being used to perform single sign-on(SSO) operations. As WikiPedia puts it, SAML is an XML-based open standard data format for exchanging authentication and authorization data between parties, in particular, between an identity provider and a service provider. With the rise in use of SAML in web applications, we may need to handle this in JMeter. This step-by-step tutorial shows SAML JMeter scenario to perform login operation. First request from JMeter is a GET request to fetch Login page. We need to fetch two values ‘SAMLRequest’ and ‘RelayState’ from the Login page response data. We can do this by using  Regular Expression Extractor . These two values need to be sent in POST request to service provider. Refer below image to see how to do this. We will get an HTML login page as a response to the request sent in 1st step. We need to fetch values of some hidden elements to pass it in the next request. We...

VBScript Code - Function to convert CSV file into excel and viceversa in QTP using VBScript

We at times are required to convert excel files into csv to read as flat files and sometime require to convert a csv file into excel file to use excel features on the data.   Below function shows how to convert an csv file into excel file and vice versa. We can also convert to other formats based on constants Here constant value 23 is used to create a csv file and constant -4143 to save a file as xls file. Once the destination file is created, we can delete the source file as shown below.  In case of any issue in understanding the code, please add in comment section Call func_ConversionCSVExcel("E:\Test.csv", "E:\Test_converted.xls", "csvtoexcel") Public Function func_ConversionCSVExcel(strSrcFile, strDestFile, Conversion) on error resume next Set objExcel = CreateObject("Excel.application") set objExcelBook = objExcel.Workbooks.Open(strSrcFile) objExcel.application.visible=false objExcel.application.displayalerts=...