Login   /   Register

The Undocumented DeviceReplay

Rate this article
     6 votes, average: 4.33 out of 56 votes, average: 4.33 out of 56 votes, average: 4.33 out of 56 votes, average: 4.33 out of 56 votes, average: 4.33 out of 5
Loading ... Loading ...
April 4th, 2008 by Yaron Assa

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" )

Why do i introduce this .NET class with the DeviceReplay object?

DeviceReplay is a really powerful undocumented object but has limitations.

One of the limitations is to determine if a control key is already pressed.

before we want to print a capital letter, we want to know if CAPS-LOCK already pressed. Before we use the numpad we want to verify that if NUM-LOCK already pressed. Otherwise we’ll just toggle the state, to maybe an unwanted state.

The Devices.Keyboard class Provides properties for accessing the current state of the keyboard, such as what keys are currently pressed, and provides a method to send keystrokes to the active window.

The required properties for our purpose are

clip_image003       AltKeyDown - Gets a Boolean indicating if the ALT key is down.

clip_image003       CapsLock - Gets a Boolean indicating if CAPS LOCK is turned on. 

clip_image003       CtrlKeyDown - Gets a Boolean indicating if a CTRL key is down.

clip_image003       NumLock - Gets a Boolean indicating if the NUM LOCK key is on. 

clip_image003       ScrollLock - Gets a Boolean indicating whether the SCROLL LOCK key is on. 

clip_image003       ShiftKeyDown - Gets a Boolean indicating if a SHIFT key is down.

Set Keyboard = DotNetFactory.CreateInstance( "Microsoft.VisualBasic.Devices.Keyboard", "Microsoft.VisualBasic" )

Print CBool( Keyboard.AltKeyDown )

Print CBool( Keyboard.CapsLock )

Print CBool( Keyboard.CtrlKeyDown )

Print CBool( Keyboard.NumLock )

Print CBool( Keyboard.ScrollLock )

Print CBool( Keyboard.ShiftKeyDown )

Important! When Using DotNetFactory data types must be converted.

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

clip_image005

 

Sends one or more keystrokes to the active window, as if typed on the keyboard.

clip_image007

 

object.SendString( str )

clip_image009

 

Parameter

Description

 

object

Always a Mercury.DeviceReplay object

 

str

the String to be typed

clip_image011

 

None

clip_image013

 

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

clip_image005

 

Simulates a key press and hold via keyboard ( KEY_DOWN event in Win32 )

clip_image007

 

object.KeyDown( key )

clip_image009

 

Parameter

Description

 

object

Always a Mercury.DeviceReplay object

 

key

the key numeric code for more information on key codes see Key Codes Reference

clip_image011

 

None

clip_image013

 

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

clip_image015

 

clip_image001        Since the key remains pressed use always the KeyUp Method.

clip_image001        The method is just like human presses a key and left it pressed all the time.

KeUp Method

clip_image005

 

Simulates a key released via keyboard.

clip_image007

 

object.KeyUp( key )

clip_image009

 

Parameter

Description

 

object

Always a Mercury.DeviceReplay object

 

key

the key numeric code for more information on key codes see Key Codes Reference

clip_image011

 

None

clip_image013

 

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

clip_image015

 

clip_image001        Use always KeyUp Method after the KeyDown.

clip_image001        Multiple KeyUp keys does not affect the application.

clip_image001        When you need to combine a hotkey, just treat the events like as a human would do it.

PressKey Method

clip_image005

 

Simulates a key pressed and immediately released via keyboard.

clip_image007

 

object.PressKey( key )

clip_image009

 

Parameter

Description

 

object

Always a Mercury.DeviceReplay object

 

key

the key numeric code for more information on key codes see Key Codes Reference

clip_image011

 

None

clip_image013

 

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

clip_image005

 

Simulates a key pressed and immediately released several times via keyboard.

clip_image007

 

object.PressNKey( key, N )

clip_image009

 

Parameter

Description

 

object

Always a Mercury.DeviceReplay object