Wednesday, October 21, 2009

QTP Tutorial: Moving mouse to a particular object

Sometimes its required to place your mouse cursor on an object in your application and then perform some operation like clicking that object or even right clicking. I have created a short QTP tutorial for moving your mouse cursor on any object in your application.

I used Google webpage to create this small tutorial on HP Quicktest Professional. This script will hover over the mouse cursor inside the search box.(It will not click inside the search box, it will simply bring the mouse over the search box)

The QTP Script goes here:

x=Browser("micclass:=Browser").Page("micclass:=Page").WebEdit("name:=q").
GetROProperty("abs_x")
y=Browser("micclass:=Browser").Page("micclass:=Page").WebEdit("name:=q").
GetROProperty("abs_y")

Set obj=CreateObject("Mercury.DeviceReplay")

obj.MouseMove x,y

Please make sure to resize your QTP window and your application before running the above HP QTP script as shown below to see the effect:

As you have noticed, your mouse cursor has been shifted to the extreme left corner of the search box. To place it somewhere in the middle, modify the fourth line in the above script to:

obj.MouseMove x+10,y+10

The output after modifying the QTP script would be as shown below:

Explanation of the above QTP code:

1. "abs_x" and "abs_y" are the object's absolute x and y coordinate relative to the screen. Hence we are finding the extreme top-left coordinate of the search box using the first two lines.

2. Mercury.DeviceReplay object is an undocumented feature in QTP which is usually used to simulate either keyboard inputs or mouse clicks or mouse movements. Also, to use "Mercury.DeviceReplay" object in HP Quicktest Professional, you need to make sure that your Application is currently the active window. So for the above code, we created an object reference for DeviceReplay object in QTP( here in our case, I used obj) and using that object reference, we used the MouseMove method to shift the mouse cursor to the desired location.

Just like we used MouseMove to shift my mouse cursor over the search box, we can also simulate clicking on an object using MouseClick.

Syntax of MouseClick Method

object.MouseClick x,y, Button

object: It should always be Mercury.DeviceReplay object
x: The object's absolute x coordinate relative to the screen
y: The object's absolute y coordinate relative to the screen
Button: It can have 3 values
1. For Left Mouse Click use 0
2. For Middle Mouse Click use 1
3. For Right Mouse Click use 2

Hence, if we want to do a left mouse click inside Google search box, our QTP script would be:

x=Browser("micclass:=Browser").Page("micclass:=Page").WebEdit("name:=q").
GetROProperty("abs_x")

y=Browser("micclass:=Browser").Page("micclass:=Page").WebEdit("name:=q").
GetROProperty("abs_y")


Set obj=CreateObject("Mercury.DeviceReplay")

obj.MouseClick x+10,y+10,0

And, if you want to do a right mouse click, replace the last line of above code with the following:

obj.MouseClick x+10,y+10,2

Note: The above code will run successfully if you will resize your QTP window while the script is running so that the search box is not hidden by the QTP window while the script is running otherwise it may not work as desired.

Do you have any questions related to this QTP Article? Feel free to ask me. Please post your queries in the comments section.

Click here to read how to check if a DataTable Parameter exists or not

Click here to read how to check if a DataTable Sheet exists or not

Click here to read tutorial on Regular Expressions

Sunday, October 11, 2009

QTP Script: Checking if Datatable Parameter exists

In my previous article of Checking if a Data Table Sheet exists, I have shown a QTP script which checks if a sheet exists or not in your Datatable. Today, I am going to show you how to check if a parameter exists or not in your DataTable.

The algorithm goes here:

Step I : Use the On Error Resume Next statement to disable any error popups.
Step II : Use the DataTable's GetSheet method to return the specified sheet. Then add the GetParameter method to seach for the parameter. The following syntax should be used:
DataTable.GetSheet("Your Sheet").GetParameter(" Your Parameter")
Step III : Check for an error number using Err.Number. If case of no error, Err.Number should return 0.

With the above algorithm, our QTP Script goes here:

'Disable Error Reporting in QTP
On Error Resume Next

'Check for the existence of "Global" sheet inside QTP. Then search for "HPQTP" parameter inside "Global" sheet
DataTable.GetSheet("Global").GetParameter("HPQTP")

If Err.Number<>0 Then
Msgbox "The specified parameter does not exists"
Else
Msgbox "The specified parameter exists"
End If

'Enable Error Reporting in QTP
On Error Goto 0

Click here to see the script for Checking if Data table sheet exists

QTP Script: Checking if Data table sheet exists

Is there any way to check if a DataTable sheet exists? Yes, offcourse. Open the DataTable and see manually. Now, is there any way to check through code if a DataTable sheet exists? Hmmmm.. Yes it is.

I have created a small algorithm to check if a HP QTP DataTable sheet exists or not. The algorithm goes here:

Step I : Use the On Error Resume Next statement to disable any error popups.
Step II : Use the DataTable's GetSheet method to return the specified sheet. The following syntax should be used:
DataTable.GetSheet("Your Sheet")
Step III : Check for an error number using Err.Number. If case of no error, Err.Number should return 0.

With the above algorithm, our QTP Script goes here:

'Disable Error Reporting in QTP
On Error Resume Next

'Check for the existence of a sheet inside QTP
DataTable.GetSheet("Global")

If Err.Number<>0 Then
Msgbox "The specified Sheet does not exists"
Else
Msgbox "The specified Sheet exists"
End If

'Enable Error Reporting in QTP
On Error Goto 0

Click here to see the script for Checking if Data table Parameter exists