Starting from this post, I am going to publish QTP code snippets that we use in our everyday projects. Starting off, I am listing the basic but very important code snippets.

  • Get an object property

There are two types of methods by which one can get the property of an object: GetROproperty and GetTOproperty

Google Submit Button

For knowing the button type (of Submit button in Google search engine as shown above), we will use the GetROproperty of the object as one shown below the code:

Browser(“Google”).Page(“tectrick – Google “).WebButton(“Search”).GetROProperty(“type”).

The type of button is “Submit”. Hence we can uniquely identify the object in a web page.

  • Setting the run time property for an object

There are situations where the object changes dynamically in applications (Web site mainly) and hence the object stored in Object repository will be different to the object available in application.In order to set the property for the object during run-time we use a fascinating method available in QTP, which is SetTOProperty. Eg: If we are in a situation to change the type of the above shown Google button as “Submit2” from “Submit” then we can use the code snippet as one shown.

Browser(“Google”).Page(“tectrick – Google “).WebButton(“Search”).SetTOProperty “type”, “Submit2”

  • Get value of text using VB script

We can use inbuilt functions Mid () Function available in VB Script.

Eg: If we have a web page with the Screen showing the text as 1-10 of 20. This happens mostly in list screen.

Dim Count

Count = Mid (“1 -10 of 20″, 9,9)

MsgBox(myString)

Now this will return 20 as the Value.

  • Using Regular Expression

There are Totally 11 special characters used Regular Expressions, they are the opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ). These special characters are often called “metacharacters”.

Eg: If there is any word say 1+1=2, then you can use it as in regular Expression as 1\+1=2. Meaning “+” has special meaning, in order to skip the meaning we use “\”

\d matches a single character that is a digit,

\w matches a “word character” (alphanumeric characters plus underscore), and

\s matches a white space character (includes tabs and line breaks).

The actual characters matched by the shorthand depend on the software you’re using. Usually, non-english letters and numbers are included.

As in our above example, the list screen Value 1-10 of 20, which will be recognized by QTP until the value in Object Repository exactly matches with AUT (Application Under Test). But here is the situation were you are adding some data and the application list screen count changes from 20 to 21, and try to run the same script, OOPs !!!!.

The QTP throws Error saying, “No object found in Object repository.” Try to run in Maintenance mode!.

Hence we can change the already recorded value of 1-10 of 20, in Object Repository with Regular Expression, since here 20 is just a digit (Numeric) we can use /d. Now the Object Repository should look like one shown below.

Advertisements