Facebook

Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any training

Friday, March 26, 2010

Click on Object by Holding CTRL Key

We may get some situations to click on a link by holding CTRL Key. The normal click operation is not capable of doing that operation. It can click on the object using Left, Right and Middle Buttons of the mouse. Even the Sendkeys method of WSH cannot hold CTRL key.
The other option is Mercury Device Replay. The hidden object of the QTP. It is providing some methods to press and release a particular key of the key board. The below function will click on an object by holding CTRL key.

Function ClickObjectWithCtrlKey(Object)
 
'Declare Variables
Dim oDeviceReplay
Dim X_Axis
Dim Y_Axis
 
'Assign The CTRL key Index number to constant
Const VK_CONTROL = 29
 
'Check for object Existance
If Object.Exist(5) Then
 
' Create Device Replay Object
Set oDeviceReplay= CreateObject( "Mercury.DeviceReplay" )
 
'Press CTRL Key
oDeviceReplay.KeyDown VK_CONTROL
 
'Get X&Y axes of the object from application
X_Axis=Object.GetROProperty("abs_x")
Y_Axis=Object.GetROProperty("abs_y")
 
'Click on the object
oDeviceReplay.MouseClick X_Axis,Y_Axis,LEFT_MOUSE_BUTTON
'Release the CTRL Key
oDeviceReplay.KeyUp VK_CONTROL
 
End If
 
End Function
 
'Register the function to Link Class
RegisterUserFunc "Link","ClickObjectWithCtrlKey","ClickObjectWithCtrlKey"
'Call the function
Browser("Google").Page("Google").Link("Books").ClickObjectWithCtrlKey
'Unregister the Function
UnregisterUserFunc "Link","ClickObjectWithCtrlKey"

Note: Before executing this function the window/Browser of the object should be the first window of the desktop. This function works good in a continuation run. 
Click here to see the script for activating a opened window/browser.

3 comments :