Google Search Results
You arrived here after searching for the following phrases:
Click a phrase to jump to the first occurrence, or return to the search results.
|
Somehow HP help files do not provide information about this powerful feature. You can find DeviceReplay property under Java add-in, but for those who doesn’t use Java add-in might think that the feature is not available for them. |
Why Device Replay?
|
Sometimes we need to do specific action on the UI, for example a right click on an object, use Fx keys to activate some hotkey, or just when object.Set and object.Type methods, somehow doesn’t work for you. Also it can be useful to type symbols and letters from different languages, without installing special fonts or changing the keyboard layout, and this can be very usefull for testing multi-language applications. For mouse operation i found very useful the DragDrop method, to drag and drop items from one frame to another or between applications. |
|
The deviceReplay object is used to simulate mouse clicks and movements and also keyboard input. To use DeviceReplay you must ensure that your AUT is the active window. If you want to perform an action on a object, the object must have the focus. For Windows applications you can use the Activate method |
|
Window( "W" ).Activate micLeftBtn |
|
To set the focus on a specific object in windows, usually the Click method will do the job. For web environment applications the Activate method is not supported, so yo can use the following trick : |
|
hwnd = Browser( "B" ).GetROProperty( "hwnd" ) Window( "hwnd:=" & hwnd ).Activate micLeftBtn |
|
To set the focus on any object usually the FireEvent “onfocusin” or object.focus e.g. WebEdit( "WE" ).object.focus or WebEdit( "WE" ).FireEvent "onfocusin" For accessing the device replay methods you need first to create the devicereplay object |
|
Set deviceReplay = CreateObject( "Mercury.DeviceReplay" ) |
System.Windows.Forms.Control Class
|
Another useful feature is to retrieve the current mouse ( cursor ) position in the screen according to the limitation of the DeviceReplay object. The System.Windows.Forms.Control base Class Defines the base class for controls, which are components with visual representation. the MousePosition property Gets the position of the mouse cursor in screen coordinates. The MousePosition property returns a Point that represents the mouse cursor position at the time the property was referenced. |
Where is my mouse?
|
Set ctlr = DotNetFactory.CreateInstance("System.Windows.Forms.Control") For i = 1 To 10 Wait 2 Print "1. X=" & ctlr.MousePosition.X & "; Y=" & ctlr.MousePosition.Y Next |
Mercury.DeviceReplay Methods
SendString Method
|
|
Sends one or more keystrokes to the active window, as if typed on the keyboard. |
|
|
|
object.SendString( str ) |
|
|
|
Parameter |
Description |
|
|
object |
Always a Mercury.DeviceReplay object |
|
|
str |
the String to be typed |
|
|
None |
|
|
|
The Following example will activate notepad and type a string. |
|
|
|
Set deviceReplay = CreateObject( "Mercury.DeviceReplay" ) SystemUtil.Run "notepad.exe", "", "", "open" ' ** this line always identifies the notepad window. Window( "nativeclass:=Notepad", "index:=0″ ).Activate micLeftBtn deviceReplay.SendString( "DeviceReplay" ) Set deviceReplay = Nothing |
|
KeyDown Method
|
|
Simulates a key press and hold via keyboard ( KEY_DOWN event in Win32 ) |
||
|
|
object.KeyDown( key ) |
||
|
|
Parameter |
Description |
|
|
|
object |
Always a Mercury.DeviceReplay object |
|
|
|
key |
the key numeric code for more information on key codes see Key Codes Reference |
|
|
|
None |
||
|
|
The Following example will activate notepad and type a string in uppercase and lowercase in a new line. Note that the shift key remains pressed while sending the first string. |
||
|
|
Const VK_SHIFT = 42 Const VK_RETURN = 28 Set deviceReplay = CreateObject( "Mercury.DeviceReplay" ) SystemUtil.Run "notepad.exe", "", "", "open" Window( "nativeclass:=Notepad", "index:=0″ ).Activate micLeftBtn ' ** Typing uppercase deviceReplay.KeyDown VK_SHIFT deviceReplay.SendString( "devicereplay" ) deviceReplay.PressKey VK_RETURN deviceReplay.KeyUp VK_SHIFT ' ** Typing in lower case deviceReplay.SendString( "devicereplay" ) Set deviceReplay = Nothing |
||
|
|
|
||
|
|
Simulates a key released via keyboard. |
||
|
|
object.KeyUp( key ) |
||
|
|
Parameter |
Description |
|
|
|
object |
Always a Mercury.DeviceReplay object |
|
|
|
key |
the key numeric code for more information on key codes see Key Codes Reference |
|
|
|
None |
||
|
|
The Following example will activate the open menu of notepad using the hotkey Ctrl+O and will close it using Escape. |
||
|
|
Const VK_O = 24 Const VK_CONTROL = 29 Const VK_ESCAPE = 1 Set deviceReplay = CreateObject( "Mercury.DeviceReplay" ) SystemUtil.Run "notepad.exe", "", "", "open" Window( "nativeclass:=Notepad", "index:=0″ ).Activate micLeftBtn ' ** Typing uppercase Wait 1 ' ** Opening the menu Ctrl + O deviceReplay.KeyDown VK_CONTROL deviceReplay.PressKey VK_O deviceReplay.KeyUp VK_CONTROL Wait 2 ' ** Closing the menu deviceReplay.PressKey VK_ESCAPE deviceReplay.SendString "Menu Open, was closed." Set deviceReplay = Nothing |
||
|
|
|
||
PressKey Method
|
|
Simulates a key pressed and immediately released via keyboard. |
|
|
|
object.PressKey( key ) |
|
|
|
Parameter |
Description |
|
|
object |
Always a Mercury.DeviceReplay object |
|
|
key |
the key numeric code for more information on key codes see Key Codes Reference |
|
|
None |
|
|
|
The Following example will activate the open menu of notepad using the hotkey Ctrl+O and will close it using Escape. |
|
|
|
Const VK_O = 24 : Const VK_F = 33 Const VK_CONTROL = 29 : Const VK_ESCAPE = 1 : Const VK_MENU = 56 Set deviceReplay = CreateObject( "Mercury.DeviceReplay" ) SystemUtil.Run "notepad.exe", "", "", "open" Window( "nativeclass:=Notepad", "index:=0″ ).Activate micLeftBtn Wait 1 ' ** Opening the menu Alt + F + O deviceReplay.PressKey VK_MENU deviceReplay.PressKey VK_F deviceReplay.PressKey VK_O Wait 2 ' ** Closing the menu deviceReplay.PressKey VK_ESCAPE deviceReplay.SendString "Open menu was closed." Set deviceReplay = Nothing |
|
PressNKeys Method
|
|
Simulates a key pressed and immediately released several times via keyboard. |
|
|
|
object.PressNKey( key, N ) |
|
|
|
Parameter |
Description |
|
|
object |
Always a Mercury.DeviceReplay object |
|
|
key |
the key numeric code for more information on key codes see Key Codes Reference |
|
|
N |
The number of repetitions. |
|
|
None |
|
Examples
Example 1 - States of USA
|
Option Explicit
Const VK_RETURN = 28 : Const VK_F = 33 : Const VK_O = 24 Const VK_TAB = 15 : Const VK_F5 = 63 Const VK_CAPITAL = 58 : Const VK_NUMLOCK = 69 Const VK_SUBTRACT = 74 : Const VK_MULTIPLY = 55 Const VK_MENU = 56
Dim deviceReplay
Private Sub SetupKeyboard() Const CLASS_NAME = "Microsoft.VisualBasic.Devices.Keyboard" Const ASSEMBLY = "Microsoft.VisualBasic" Dim Keyboard
Set Keyboard = DotNetFactory.CreateInstance( CLASS_NAME, ASSEMBLY ) If CBool( Keyboard.CapsLock ) Then deviceReplay.PressKey VK_CAPITAL End If If CBool( Keyboard.NumLock ) = False Then deviceReplay.PressKey VK_NUMLOCK End If Set Keyboard = Nothing End Sub
Private Sub SetupNotepad() deviceReplay.PressKey VK_MENU deviceReplay.PressKey VK_O deviceReplay.PressKey VK_F deviceReplay.SendString "Courier New" deviceReplay.PressKey VK_TAB deviceReplay.PressKey VK_TAB deviceReplay.SendString "14″ deviceReplay.PressKey VK_RETURN Wait 1 End Sub
Private Sub PrintRow( ByVal state, ByVal usps, byVal capital ) deviceReplay.SendString state deviceReplay.PressKey VK_TAB If Len( state ) < 8 Then deviceReplay.PressKey VK_TAB End If deviceReplay.SendString usps deviceReplay.PressKey VK_TAB deviceReplay.SendString capital deviceReplay.PressKey VK_RETURN End Sub
Set deviceReplay = CreateObject( "Mercury.DeviceReplay" ) SystemUtil.Run "notepad.exe", "", "", "open", 3 Window( "nativeclass:=Notepad", "index:=0″ ).Activate micLeftBtn ' ** Setup Notepad - Font courier new, size 14, ' ** NUM-LOCK pressed and CAPS-LOCK unpressed Call SetupKeyboard() Call SetupNotepad() ' ** inserting date deviceReplay.PressKey VK_F5 deviceReplay.PressKey VK_RETURN ' ** Inserting Title deviceReplay.PressNKeys VK_TAB, 3 deviceReplay.SendString "United States of America" deviceReplay.PressKey VK_RETURN deviceReplay.PressNKeys VK_TAB, 3 deviceReplay.PressNKeys VK_MULTIPLY, Len( "United States of America" ) deviceReplay.PressNKeys VK_RETURN, 2 ' ** Table Headers deviceReplay.SendString "State" deviceReplay.PressKey VK_TAB deviceReplay.PressKey VK_TAB deviceReplay.SendString "USPS" deviceReplay.PressKey VK_TAB deviceReplay.SendString "Capital" deviceReplay.PressKey VK_RETURN deviceReplay.PressNKeys VK_SUBTRACT, 31 deviceReplay.PressKey VK_RETURN ' ** Print Data Call PrintRow( "Alabama", "AL", "Montgomery" ) Call PrintRow( "Alaska", "AK", "Juneau" ) Call PrintRow( "Arizona", "AZ", "Phoenix" ) Call PrintRow( "Arkansas", "AR", "Little Rock" ) Call PrintRow( "California", "CA", "Sacramento" ) Call PrintRow( "Colorado", "CO", "Denver" ) Call PrintRow( "Connecticut", "CT", "Hartford" ) Call PrintRow( "Delaware", "DE", "Dover" ) Call PrintRow( "Florida", "FL", "Tallahassee" ) Call PrintRow( "Georgia", "GA", "Atlanta" ) Call PrintRow( "Hawaii", "HA", "Honolulu" ) Call PrintRow( "Idaho", "ID", "Boise" ) Call PrintRow( "Illinois", "IL", "Springfield" ) Call PrintRow( "Indiana", "IN", "Indianapolis" ) Call PrintRow( "Iowa", "IA", "Des Moines" ) Call PrintRow( "Kansas", "KS", "Topeka" ) Call PrintRow( "Kentucky", "KY", "Frankfort" ) Call PrintRow( "Louisiana", "LA", "Baton Rouge" ) Call PrintRow( "Maine", "ME", "Augusta" ) Call PrintRow( "Maryland", "MD", "Annapolis" ) Call PrintRow( "Massachusetts", "MA", "Boston" ) Call PrintRow( "Michigan", "MI", "Lansing" ) Call PrintRow( "Minnesota", "MN", "Saint Paul" ) Call PrintRow( "Mississippi", "MS", "Jackson" ) Call PrintRow( "Missouri", "MO", "Jefferson City" ) Call PrintRow( "Montana", "MT", "Helena" ) Call PrintRow( "Nebraska", "NE", "Lincoln" ) Call PrintRow( "Nevada", "NV", "Carson City" ) Call PrintRow( "New Hampshire", "NH", "Concord" ) Call PrintRow( "New Jersey", "NJ", "Trenton" ) Call PrintRow( "New Mexico", "NM", "Santa Fe" ) Call PrintRow( "New York", "NY", "Albany" ) Call PrintRow( "North Carolina", "NC", "Raleigh" ) Call PrintRow( "North Dakota", "ND", "Bismarck" ) Call PrintRow( "Ohio", "OH", "Columbus" ) Call PrintRow( "Oklahoma", "OK", "Oklahoma City" ) Call PrintRow( "Oregon", "OR", "Salem" ) Call PrintRow( "Pennsylvania", "PA", "Harrisburg" ) Call PrintRow( "Rhode Island", "RI", "Providence" ) Call PrintRow( "South Carolina", "SC", "Columbia" ) Call PrintRow( "South Dakota", "SD", "Pierre" ) Call PrintRow( "Tennessee", "TN", "Nashville" ) Call PrintRow( "Texas", "TX", "Austin" ) Call PrintRow( "Utah", "UT", "Salt Lake City" ) Call PrintRow( "Vermont", "VT", "Montpelier" ) Call PrintRow( "Virginia", "VA", "Richmond" ) Call PrintRow( "Washington", "WA", "Olympia" ) Call PrintRow( "West Virginia", "WV", "Charleston" ) Call PrintRow( "Wisconsin", "WI", "Madison" ) Call PrintRow( "Wyoming", "WY", "Cheyenne" )
Set deviceReplay = Nothing |
Example 2 - Latin characters and Symbols
|
Option Explicit
Const VK_NUMPAD0 = 82 Const VK_NUMPAD1 = 79 Const VK_NUMPAD2 = 80 Const VK_NUMPAD3 = 81 Const VK_NUMPAD4 = 75 Const VK_NUMPAD5 = 76 Const VK_NUMPAD6 = 77 Const VK_NUMPAD7 = 71 Const VK_NUMPAD8 = 72 Const VK_NUMPAD9 = 73 Const VK_MENU = 56 Const VK_SHIFT = 42 Const VK_RETURN = 28 Const VK_F = 33 Const VK_O = 24 Const VK_TAB = 15 Const VK_F5 = 63 Const VK_NUMLOCK = 69 Dim deviceReplay
Private Sub SetupKeyboard() Const CLASS_NAME = "Microsoft.VisualBasic.Devices.Keyboard" Const ASSEMBLY = "Microsoft.VisualBasic" Dim Keyboard
Set Keyboard = DotNetFactory.CreateInstance( CLASS_NAME, ASSEMBLY ) If CBool( Keyboard.CapsLock ) Then deviceReplay.PressKey VK_CAPITAL End If If CBool( Keyboard.NumLock ) = False Then deviceReplay.PressKey VK_NUMLOCK End If Set Keyboard = Nothing End Sub
Private Sub SetupNotepad() deviceReplay.PressKey VK_MENU deviceReplay.PressKey VK_O deviceReplay.PressKey VK_F deviceReplay.SendString "Courier New" deviceReplay.PressKey VK_TAB deviceReplay.PressKey VK_TAB deviceReplay.SendString "14″ deviceReplay.PressKey VK_RETURN Wait 1 End Sub
Private Sub PrintCharacter( ByVal code ) Dim i, digit
deviceReplay.KeyDown VK_MENU For i = 1 To Len( code ) digit = Mid( code, i, 1 ) Execute "deviceReplay.PressKey VK_NUMPAD" & digit Next deviceReplay.KeyUp VK_MENU deviceReplay.PressKey VK_RETURN End Sub
Set deviceReplay = CreateObject( "Mercury.DeviceReplay" ) SystemUtil.Run "notepad.exe", "", "", "open", 3 Window( "nativeclass:=Notepad", "index:=0″ ).Activate micLeftBtn ' ** Setup Notepad - Font courier new, size 14, ' ** NUM-LOCK pressed and CAPS-LOCK unpressed Call SetupKeyboard() Call SetupNotepad() ' ** inserting date deviceReplay.PressKey VK_F5 deviceReplay.PressKey VK_RETURN ' ** a grave character deviceReplay.SendString "A grave: " Call PrintCharacter( "0192″ ) ' ** O circumflex character deviceReplay.SendString "O circumflex: " Call PrintCharacter( "0212″ ) ' ** s caron character deviceReplay.SendString "s caron: " Call PrintCharacter( "0154″ ) ' ** n tilde character deviceReplay.SendString "n tilde: " Call PrintCharacter( "164″ ) ' ** Y umlaut character deviceReplay.SendString "Y umlaut: " Call PrintCharacter( "0159″ ) ' ** c cedila character deviceReplay.SendString "c cedila: " Call PrintCharacter( "0231″ ) ' ** O with accent character deviceReplay.SendString "O with accent: " Call PrintCharacter( "0211″ ) ' ** Inverted question mark character deviceReplay.SendString "Inverted question mark: " Call PrintCharacter( "168″ ) ' ** Euro character deviceReplay.SendString "Euro: " Call PrintCharacter( "0128″ ) ' ** i with accent character deviceReplay.SendString "i with accent : " Call PrintCharacter( "0237″ ) ' ** Male Sign character deviceReplay.SendString "Male Sign: " Call PrintCharacter( "11″ ) ' ** AE ligature character deviceReplay.SendString "AE ligature: " Call PrintCharacter( "0198″ ) ' ** aa character deviceReplay.SendString "aa: " Call PrintCharacter( "0197″ ) ' ** oethel character deviceReplay.SendString "oethel: " Call PrintCharacter( "0156″ ) ' ** Eth character deviceReplay.SendString "Eth: " Call PrintCharacter( "0208″ ) ' ** Uppercase Sigma character deviceReplay.SendString "Uppercase Sigma: " Call PrintCharacter( "228″ )
Set deviceReplay = Nothing |
DragAndDrop Method
MouseClick Method
|
Method performs a single click ( left or right ) on a specific location in the screen |
|
object.MouseClick( x, y, Button ) |
|
|
Parameter |
Description |
|
object |
Always a Mercury.DeviceReplay object |
|
x |
the x absolute coordinate in a screen for perform the click |
|
y |
the y absolute coordinate in a screen for perform the click |
|
button |
possible values LEFT_MOUSE_BUTTON = 0 MIDDLE_MOUSE_BUTTON = 1 RIGHT_MOUSE_BUTTON = 2 |
|
None |
|
|
The following example requires a preparation. the purpose of the example is to execute a DragAndDrop method on www.advancedqtp.com site. if you record the drag and drop action… nothing will be recorded. so this is an example how to support some kind of actions. this example was tested on IE. open your IE Browser and navigate to www.advancedqtp.com. The example will swap dbx-handle items. those objects can be dragged to customize the display. |
|
|
Open QTP ( with web add-in ), Open a new test, open the object repository and add The Browser and Page to the local object repository. |
|
|
Rename the objects… |
|
|
Option Explicit Const LEFT_MOUSE_BUTTON = 0
Dim oWebElemDesc1, oWebElemDesc2 Dim oWebElem1, oWebElem2 Dim devRep Dim nX1, nX2, nY1, nY2, nH1, nH2, hwnd Dim point1, point2
' ** This class holds a point coordinate Class Point Private mX, mY Property Let X( ByVal value ) mX = value End Property Property Get X() X = mX End Property Property Let Y( ByVal value ) mY = value End Property Property Get Y() Y = mY End Property End Class
' ** Retrieving the handle of the browser hwnd = Browser("QTP").GetROProperty( "hwnd" ) Window( "hwnd:=" & hwnd ).Activate ' ** Create a description for 'Program Professionally’ Set oWebElemDesc1 = Description.Create() oWebElemDesc1( "micclass" ).Value = "WebElement" oWebElemDesc1( "html tag" ).Value = "H3″ oWebElemDesc1( "innertext" ).Value = "Program Professionally" oWebElemDesc1( "class" ).Value = "dbx-handle dbx-handle-cursor" ' ** Create a description for 'Links’ Set oWebElementDesc2 = Description.Create() oWebElemDesc2( "micclass" ).Value = "WebElement" oWebElemDesc2( "html tag" ).Value = "H3″ oWebElemDesc2( "innertext" ).Value = "Links" oWebElemDesc2( "class" ).Value = "dbx-handle dbx-handle-cursor" ' ** Searching for the elements With Browser( "QTP" ).Page( "QTP" ) If .ChildObjects( oWebElemDesc1 ).Count = 1 Then Set oWebElem1 = .WebElement( oWebElemDesc1 ) If .ChildObjects( oWebElemDesc2 ).Count = 1 Then Set oWebElem2 = .WebElement( oWebElemDesc2 ) Else Print "Web Element 'Program Professionally’ was not found." ExitTest( micFail ) End If Else Print "Web Element 'Program Professionally’ was not found." ExitTest( micFail ) End If End With ' ** Retrieve elements dimensions nX1 = oWebElem1.GetROProperty( "abs_x" ) nH1 = oWebElem1.GetROProperty( "height" ) nY1 = oWebElem1.GetROProperty( "abs_y" ) nX2 = oWebElem2.GetROProperty( "abs_x" ) nH2 = oWebElem2.GetROProperty( "height" ) nY2 = oWebElem2.GetROProperty( "abs_y" ) Set point1 = New Point point1.X = nX1 + 10 point1.Y = nY1 + nH1 - 10 Set point2 = New Point ' ** Dragging up If nY1 > nY2 Then point2.X = nX2 + 20 point2.Y = nY2 + nH2 - 20 Else ' ** Dragging down point2.X = nX2 + 20 point2.Y = nY2 + nH2 + 20 End If Set devRep = CreateObject( "Mercury.DeviceReplay" ) devRep.DragAndDrop point1.X, point1.Y, _ point2.X, point2.Y, LEFT_MOUSE_BUTTON |
|
MouseDblClick Method
|
|
Method performs a double click ( left or right ) on a specific location in the screen |
|
|
|
object.MouseDblClick( x, y, Button ) |
|
|
|
Parameter |
Description |
|
|
object |
Always a Mercury.DeviceReplay object |
|
|
x |
the x absolute coordinate in a screen for perform the double-click |
|
|
y |
the y absolute coordinate in a screen for perform the double-click |
|
|
button |
possible values LEFT_MOUSE_BUTTON = 0 MIDDLE_MOUSE_BUTTON = 1 RIGHT_MOUSE_BUTTON = 2 |
|
|
None |
|
MouseDown Method
|
|
Method performs a click ( left or right ) on a specific location in the screen, but leaves the mouse clicked. |
||
|
|
object.MouseDown( x, y, Button ) |
||
|
|
Parameter |
Description |
|
|
|
object |
Always a Mercury.DeviceReplay object |
|
|
|
x |
the x absolute coordinate in a screen for perform the click |
|
|
|
y |
the y absolute coordinate in a screen for perform the click |
|
|
|
button |
possible values LEFT_MOUSE_BUTTON = 0 MIDDLE_MOUSE_BUTTON = 1 RIGHT_MOUSE_BUTTON = 2 |
|
|
|
None |
||
|
|
|||
MouseUp Method
|
|
Method release a previous MouseDown Method |
||
|
|
object.MouseDown( x, y, Button ) |
||
|
|
Parameter |
Description |
|
|
|
object |
Always a Mercury.DeviceReplay object |
|
|
|
x |
the x absolute coordinate in a screen for perform the mouse release |
|
|
|
y |
the y absolute coordinate in a screen for perform the mouse release |
|
|
|
button |
possible values LEFT_MOUSE_BUTTON = 0 MIDDLE_MOUSE_BUTTON = 1 RIGHT_MOUSE_BUTTON = 2 |
|
|
|
None |
||
|
|
|||
MouseMove Method
SetSynchronizationTimeout Method
|
|
Set a new synchronization timeout |
||
|
|
object.MouseDown( x, y ) |
||
|
|
Parameter |
Description |
|
|
|
object |
Always a Mercury.DeviceReplay object |
|
|
|
nSyncTimeout |
the synchronization timeout |
|
|
|
is_sec |
Boolean value that indicates the nSyncTimeout is in seconds |
|
|
|
None |
||
|
|
|||
Key Codes Reference
Posted in QTP Techniques


Yaron Assa




July 14th, 2008 at 3:24 pm
this doc is really very helpful,
thanx lot….
August 28th, 2008 at 6:03 pm
how can i preform a keybourd click on Print scrn and then paste the buffer to jpg
February 23rd, 2009 at 9:51 pm
[…] the key numeric code for more information on key codes see Key Codes Reference […]
April 15th, 2009 at 10:44 am
This doc is very nice to have better understanding of QTP in DOS
June 13th, 2009 at 3:04 am
Fantastically helpful, but I’m having trouble getting this to work when the Windows desktop is extended onto multiple monitors - specifically the MouseClick operation. No problem finding the x/y and setting focus, but the click itself doesn’t happen…any insight? Thank you!
November 12th, 2009 at 4:01 am
[…] http://www.advancedqtp.com/knowledge-base/articles/qtp-tricks4/the-undocumented-devicereplay/ […]
January 5th, 2010 at 8:29 am
Hi Im getting “Object Not visible ” Error whenever i try
hwnd = Browser( “B” ).GetROProperty( “hwnd” )
Window( “hwnd:=” & hwnd ).Activate micLeftBtn
Any help would be appriciated