This post is a continuation of QTP code snippets. You can check QTP Code Snippets – Part 1 here.

  • Formatting the date and time value

FormatDateTime() function returns an expression formatted as date and time. The function has Date part and NamedFormat part. Example: If the application returns the time stamp and we need only date and not time then we can use the function as shown:

createdTime = FormatDateTime(“06/05/2009 10:15 AM”,2)

Msgbox(CreatedTime)

Output

06/05/2009

  • Comparing difference in date

DateDiff() function returns the number of intervals between two dates

DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])

Example: We are going to compare the present date with the date passed through FormatDateTime() function to check if it Today’s date.

createdTime = FormatDateTime(“06/05/2009 10:15 AM”,2)

If DateDiff(“d”,createdTime,date) = 0 Then

Msgbox(“Its Present Date”)

Else

Msgbox(“Its not Todays date, its another day”)

End If

  • Passing parameter from one action to another

Before passing a parameter from one action to another input parameter needs to be created for the action which is going to accept a value as input from another action. Lets take Action 2 is going to get an value from Action 1 as a parameter, then first create an input parameter for the Action 2. This can be done by right clicking on the Action 2 Action Properties à Clicking on Properties Tab and Clicking the “+” sign to create a parameter. Now lets say we are going to pass an integer variable from Action 1 to Action 2, hence I can create a number parameter in the input parameter of Action2. The code snippet will look like the following:

Action1

a = 10

b= 20

c= a+b

RunAction “Action2″, oneIteration,c

 

Action2

d = 10

result = Parameter(“c”)

e = result+d

msgbox(e)

Note: Set c as input parameter in Action2

More code snippets in the next post.

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.