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

4 comments:

Unknown said...

Good article with nice explanation. Thanks

Unknown said...

Hi,

I find this to be an expertly written script. Kudos

Jack

Anonymous said...

This is a wonderful article with great explanation.. Thanks for sharing!

Anonymous said...

he i need to hower a mouse over google map can u give a example for the same

thanks and regards
abhilash112@aol.com