3 ways to use keyboard input in QuickTest Professional: Type, SendKeys and Device Replay

by Joe Colantonio on September 23, 2010

Post image for 3 ways to use keyboard input in QuickTest Professional: Type, SendKeys and Device Replay

There are three ways to use keyboard input in QuickTest Professional: Type, SendKeys and Device Replay. Why, you might ask, use the Type or Sendkeys method at all?  While you wouldn’t want to automate a whole script using SendKeys only, sometimes you may, on occasion, need to automate a section of an application where there is no object recognition. 

For example, some unsupported grids or treeviews in Quick Test Pro can be navigated using a combination of the HOME, END and arrow keys.  Also -- I’ve seen applications where some fields need to have a keyboard event occur to trigger some other behavior (like tabbing of a field) using the objects standard Set method –- which would not work in this case.

1. QTP’s TYPE METHOD

Most objects support the TYPE method.  Type will enter the specified string into an object or perform a certain keyboard combination against an application.  For example:

To tab off an object you would use the following syntax:

SwfObject(“swfname:=Blank”).Type micTab

To enter text:

<br />SwfObject(“swfname:=Blank”).Type “This is my string”

To send an enter keystroke:

SwfObject(“swfname:=Blank”).Type “ “

You can also send a combination of keystrokes at one time. The following holds down the CTRL and the Shift Keys, then presses the “L” key and releases the CTRL and Shift keys:

<br />SwfObject("swfname:=Blank").Type micCtrlDwn + micShiftDwn + "L" + micShiftUp + micCtrlUp

*Important to remember: If you send a down keystroke, be sure to follow it with its corresponding up stroke, as seen in the above example.  Problems can arise by sending a micCtrlDwn and forgetting to release it using micCtrlUp. So, rule of thumb -- if you are pressing a key, make sure to also release it.

2. VBScript SendKeys Method ( Also check out: QTP's VBscript SendKeys FAQ)

There are instances in which QTP’s Type method does not trigger certain events, or is unable to mimic certain keystrokes.  In these cases, VBScript SendKeys method can be used. To use the SendKeys you need to first set the WshShell object:

<br />Dim mySendKeys<br />set mySendKeys = CreateObject("WScript.shell")<br />mySendKeys.SendKeys(“{TAB}”)

To send the text you would use:

<br />mySendKeys.SendKeys(“This is my string”)

To send an enter keystroke:

mySendKeys.SendKeys(“~”)

*A few important tips: Unlike the QTP TYPE method, you will need to use curly braces for arguments like a TAB or Up arrow key strokes. Also -- SendKeys has the following special characters:  Alt(%), Ctrl(^), Shift(+) and Enter(~) keys.

So, to send a CTRL and press the A key you would send:

<br />mySendKeys.SendKeys("^A")

If you need to perform the same keystroke multiple times, you can create a compound string argument. This will allow you to perform a specific keystroke and repeat it any number of times. To select, say, the 10th row in a grid control you might use:

mySendKeys.SendKeys(“{DOWN 10}”)

This will send the down key ten times. (For a more detailed explanation of SendKeys check out VBScript Programmer's Reference.)

Important – You’ll need to make sure that the application or object you wish to receive the keystroke has focus before sending the keystroke.

*Common issues with this method:

(Sometimes multiple keystrokes will not work. If this is the case, try executing each one in a separate line.)

3. Device Replay

This is an undocumented and unsupported QuickTest method, but can be used as a last resort. To employ this method, you’ll need to create a Device Replay object.(Check out the Device Replay Chart of Codes)

To tab off an object:

<br />Dim myDeviceReplay

Set myDeviceReplay = CreateObject("Mercury.DeviceReplay")

myDeviceReplay.PressKey 15

*Remember that Device Replay uses ASCII characters
To send text you would use:

myDeviceReplay.SendString “This is my string”

To send an enter keystroke:

myDeviceReplay.PressKey 28 ‘ASCII code for enter

From The HP knowledge base:

The functions that can be used with the Device Replay object are (all coordinates are relative to the top left corner of the screen):

 

Function Description
MouseMove x, y Move the mouse to the screen coordinate (x,y).
MouseClick x, y, button Move the mouse to the screen coordinate (x,y) and click the button
(0=left; 1=middle; 2=right).
MouseDblClick x, y, button Move the mouse to the screen coordinate (x,y) and double-click the button
(0=left; 1=middle; 2=right).
DragAndDrop x, y, dropx, dropy, button Drag the mouse from screen coordinate (x,y) to (dropx,dropy) with the button
(0=left; 1=middle; 2=right) pressed.
PressKey key Press a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
MouseDown x, y, button Press the mouse button on screen coordinate (x,y).
MouseUp x, y, button Release the mouse button on screen coordinate (x,y).
KeyDown key Press a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
KeyUp key Release a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
SendString string Type a string.
 
 

 

Hope this helps.

{ 33 comments… read them below or add one }

Joyce Noel November 11, 2010 at 2:06 pm

Hi Joe, I love this website you created! <3

Reply

Eric December 3, 2010 at 2:20 pm

Dim mySendKey
set mySendKey = CreateObject(“WScript.shell”)
mySendKeys.SendKeys(“{TAB}”)

Should be

Dim mySendKey
set mySendKey = CreateObject(“WScript.shell”)
mySendKey.SendKeys(“{TAB}”)

Reply

Daniel June 7, 2011 at 8:53 pm

Thanks Eric!

Reply

Anish Pillai September 12, 2011 at 1:12 pm

Thanks Joe.. Good Article. I didnt know about Device Replay

Reply

Farhana Ashraf October 25, 2011 at 8:58 am

Hi Joe,

I used press key method that works for ie and doesn’t work for Fire fox. Given below is the code. Please help. I am using QTP 11.

i=0

Set enter_key_candidate = CreateObject(“Mercury.DeviceReplay”)
Set enter_key_recruiter = CreateObject(“Mercury.DeviceReplay”)

While Browser(“hwnd:=”& hwnd_ie).Page(“title:= .*Brazen.*”).WebEdit(“name:=.*chatInputBox”).Exist AND Browser(“hwnd:=”& hwnd_ff).Page(“title:= .*Brazen.*”).WebEdit(“name:=.*chatInputBox”).Exist

””””””’ie user types message
‘ If Browser(“hwnd:=”& hwnd_ie).Page(“title:= .*Brazen.*”).WebEdit(“name:=.*chatInputBox”).Exist Then
‘ Browser(“hwnd:=”& hwnd_ie).Page(“title:= .*Brazen.*”).WebEdit(“name:=.*chatInputBox”).Set “candidate message”&i+1
‘ Browser(“hwnd:=”& hwnd_ie).Page(“title:= .*Brazen.*”).WebEdit(“name:=.*chatInputBox”).Click
‘ enter_key_candidate.PressKey 28
‘ Browser(“hwnd:=”& hwnd_ie).Page(“title:= .*Brazen.*”).WebElement(“html id:=chatDiv”).Click

‘ End If
””””””’FF user types message
‘ If Browser(“hwnd:=”& hwnd_ff).Page(“title:= .*Brazen.*”).WebEdit(“name:=.*chatInputBox”).Exist Then
‘ Browser(“hwnd:=”& hwnd_ff).Page(“title:= .*Brazen.*”).WebEdit(“name:=.*chatInputBox”).Set “candidate message”&i+1
‘ Browser(“hwnd:=”& hwnd_ff).Page(“title:= .*Brazen.*”).WebEdit(“name:=.*chatInputBox”).Click
‘ enter_key_candidate.PressKey 28
‘ Browser(“hwnd:=”& hwnd_ff).Page(“title:= .*Brazen.*”).WebElement(“html id:=chatDiv”).Click

‘ End If

i=i+1
wait(20)
Wend

Reply

Joe Colantonio November 20, 2011 at 5:35 pm

Farhana Ashraf » Sorry not sure why this is happening. What happens if you do a quick QTP record against the object does QTP recognize it? Also Here are some additional points to keep in mind when working with Mozilla Firefox:

You must be logged-in with Administrator privileges (or have write permissions to the browser’s installation folder) on the QuickTest Professional computer when launching Mozilla Firefox with QuickTest Professional for the first time. Adding QuickTest Professional support for Mozilla Firefox requires a file to be created in the browser’s installation folder.

You can record tests on Microsoft Internet Explorer and run them on Mozilla Firefox. You cannot record tests on Mozilla Firefox if using versions lower than QuickTest 11, for which there are two ways to create tests to run on Mozilla Firefox on such lower versions:

Record the test on Microsoft Internet Explorer.
Use the keyword-driven methodology: create an object repository for your application using the Object Repository window (local object repository) or Object Repository Manager (shared object repository), and then add steps using the Keyword View or Step Generator. When you use the keyword-driven methodology, you can add objects using Mozilla Firefox if you want; you do not have to use Microsoft Internet Explorer.

Tabbed browsing is not supported by QuickTest Professional for any browser type. In Mozilla Firefox, you must specifically configure your browser to open a new browser window instead of a new tab. This configuration is dependent on the nature of your application and therefore configuration instructions are not provided. In most cases, you can add objects to the object repository and run tests on the active browser tab, even if there are additional tabs currently open.

Reply

Icahn November 10, 2011 at 2:40 pm

Hi

I have Q I am working on hotkeys on web application with QTP 10, IE 6. My Code works in IE 6, but not working in IE6. I get error “Object is not supported ….
Here is the snippent of the code.. what could be wrong…
Set WshShell = CreateObject(“WScript.Shell”)
vhwnd=Browser(“Main Browser”).GetROProperty(“hwnd”)
Window(“hwnd:=”&vhwnd).activate —– This is where it is giving error
WshShell.SendKeys “%1″

Reply

Joe Colantonio November 20, 2011 at 5:25 pm

Icahn » I’m not sure – it should work for both. I’ve seen issues when IE8 has the ‘Enable Tabbed Browsing’ turned on. Anyone else have any ideas?

Reply

Abhijeet May 3, 2012 at 9:43 am

This statement is not clear : “My Code works in IE 6, but not working in IE6″

I’m assuming Icahn is trying to use IE8. For IE8, Browser(“”).GetROProperty(“hwnd”) will not provide the hwnd, it will have to be retrieved using native methods.

You should try using the below code instead :

Browser(“r”).Object.HWND

Reply

Kim Arild November 16, 2011 at 7:02 pm

Hi
im trying to push a key down in x sec to a program with this code :
Dim Wsh
Set Wsh = Wscript.CreateObject(“Wscript.Shell”)
Wsh.AppActivate “My App Title”
Wsh.KeyDown “Z”
WScript.Sleep(3000)
Wsh.KeyUp “Z”

Whats Wrong With The Script ? :(
Tnx :D

Reply

Joe Colantonio November 18, 2011 at 12:44 pm

Kim Arild » HI Kim does this work for you?

Const VK_Z = 44

Set myDeviceReplay = CreateObject(“Mercury.DeviceReplay”)
Window(“regexpwndtitle:=you app title”).Activate
myDeviceReplay.KeyDown VK_Z
wait 3
myDeviceReplay.KeyUp VK_Z

Reply

Kim Arild September 18, 2012 at 5:02 am

Get This Error :
——————————————-
Active X cant create object:
“Mercury.DiveceReplay”
Code:800A01AD
———————————————

Reply

saras February 7, 2012 at 7:01 am

Browser(“Sign In”).Page(“Course Settings”).WebTable(“name:=Save Changes”).WebElement(“html tag:=TD”,”abs_x:=2″,”abs_y:=647 “).click

i am running the above script for clicking a webelement in my application.Still the button is not clicked.But the webpage opens correctly.The application I am using for script in .Net application.

Reply

Joe Colantonio February 11, 2012 at 2:12 pm

saras » Hi – have you tried using the fire event to trigger the click? If not check out my post on how to use the fire event Cheers~Joe

Reply

Anita March 2, 2012 at 12:32 am

Hi Joe, thank you for the great website. Super helpful.
I’m wondering if you can help answering my question. Is there a way to limit the duration QTP click on a button? (Kim’s question is on holding sendkeys for 3 secs, mine is kind of the opposite). So I have the following code:
JavaWindow(“API GUI”).JavaList(“LBS:”).Select “basic”
JavaWindow(“API GUI”).JavaButton(“Submit”).Click

When QTP’s executing Submit code, it seems to be holding down the Submit button for too long, ends up mimicking a second click on the button after getting a response back.

Thank you for your help!

Reply

Joe Colantonio March 2, 2012 at 6:26 pm

Anita » Hi Anita – that is some odd behavior. I’m not aware of any setting in QTP that limits the amount of time a click is performed on a button. What happens if you use the fireevent instead to do the click? Does it behave the same way?

Reply

Anita March 2, 2012 at 9:10 pm

Thank you for the suggestion! I’ll try it. :)

Reply

Anita March 2, 2012 at 9:46 pm

Hi Joe, FireEvent for Java seems to have micMouseClick vs. “onclick” or “ondblclick”. Syntax for it is: object.FireEvent EventType, EventArguments()
where
EventType is micMouseClick and
EventArguments is: (per Help) Required. An array of Variant values. Any parameters of the event’s constructor except for the source and EventID. You can pass any Java constant that is used as one of the event’s constructor parameters using its string instead of its value. Enter the values in a comma separated list. If no additional parameters are required, you do not need to supply a value for the EventArguments argument.

I don’t understand what it means or how to use this eventArguments. I tried the below and it doesn’t work :(
JavaWindow(“WPP for HLAPIs v1.3:_2″).JavaList(“LBS:”).Select “basic”
JavaWindow(“WPP for HLAPIs v1.3:_2″).JavaButton(“Submit”).FireEvent micMouseClick, 0

Would you be able to explain a bit more on how to use FireEvent with micMouseClick? thanks!

Reply

Anita March 2, 2012 at 9:54 pm

oh, it gives an “Invalid number of parameters” error

Joe Colantonio March 2, 2012 at 10:02 pm

Anita » Sorry I assumed the java fireevent would be the same as a web objects fireevent — I’m not sure what the exact event arguments should be. I need to research it. In the meantime how about trying
JavaButton.Object.doClick() instead of FireEvent. For example:

JavaWindow(“WPP for HLAPIs v1.3:_2″).JavaButton(“Submit”).Object.doClick()

I did verify this method works on my machine.

Anita March 5, 2012 at 5:49 pm

Thanks Joe! It works with Object.doClick().
Out of curiosity, what’s QTP doing differently between JavaWindow(“WPP for HLAPIs v1.3:_2″).JavaButton(“Submit”).Object.doClick()
and JavaWindow(“WPP for HLAPIs v1.3:_2″).JavaButton(“Submit”).Click ?

Ganesh Kodali March 28, 2012 at 7:26 am

Dear Joe,
I try to set data in webedit field but it gets cleared after tabout to next object. Tried with sendkeys, type, Device replay. but no luck.
could you please provide solution
below url http://www.mscgva.ch/schedule.html
while entering Port of Load and Port of discharge

Thanks in advance
Ganesh

Reply

Joe Colantonio March 28, 2012 at 12:47 pm

Ganesh Kodali » Hi Ganesh – what data are you using for Port of Load and Port of discharge? This looks like a web application issue not a QTP issue. Make sure you are using valid data. When I manually enter bogus port names the text is cleared but if I enter a valid port like KANDLA,INDIA it works for me.

Reply

Ganesh Kodali March 29, 2012 at 6:51 am

Hi Joe
No doubt I am using correct data, but Web application works
only with data fetched. If I enter Chennai application fetches
“CHENNAI, INDIA (INMAA)” , should be selected and then proceed to next Singapore application fetches “SINGAPORE, SINGAPORE (SGSIN)” finally to click search
Using QTP tried with webedit, sendkeys, type, Device replay to set data directly as “CHENNAI, INDIA (INMAA)”, “SINGAPORE, SINGAPORE (SGSIN)” it displays Please enter Port of loading or Port of discharge.
http://www.mscgva.ch/schedule.html
Here issue is with web application its not straight forward for us to handle. Is there any tricky solution to handle. Whatever we do manually should be able to do using QTP.

Thanks,
Ganesh

Reply

Ganesh April 16, 2012 at 8:11 am

Joe,
Any clue on my query / problem.

Thanks,
Ganesh

Reply

Joe Colantonio April 20, 2012 at 4:14 pm

Ganesh » Sorry Ganesh I have not had time to look at this yet.

Abhishek April 11, 2012 at 11:33 am

Hi Joe

I have Putty application to test and to enter any command it only recognises numpad enter key.

How I can press NUMPAD ENTER key using QTP..

I have tried .type, sendkey, presskey and also terminal Emulator addin.. but unable to succeed.

Can you pls guide here?

Reply

Joe Colantonio April 12, 2012 at 1:47 pm

Abhishek » Hi – this is trickier then I thought it would be. I’ve tried using {NUMPADENTER} and other things without success. Does anyone else know how to do this?

Reply

Harshit Shrivastava April 22, 2013 at 11:01 am

Hi Joe,
Thanks for creating an awesome website.
I am trying automate a machine in which normal enter key and numpad enter key have different functions.
If I record, both the enter keys show the same recorded VBScript code. Have you found out any way it can be done??

Reply

Joe Colantonio April 22, 2013 at 10:59 pm

Harshit Shrivastava » Thanks Harshit – not sure about the two different enter keys – you might be able to do it using Extended ASCII characters.

Reply

Sweety April 20, 2013 at 3:16 pm

Hi Joe,
The issue that I am currently facing to click on the “save as” when I right click on a Link in a web page.

The link is a document which I need to save it in some location. I am using Qtp 11v.

Any help will be great.

Reply

Joe Colantonio April 22, 2013 at 11:07 pm

Sweety » Hi – does this code help?

Reply

Rosh May 8, 2013 at 11:39 am

Hi Joe, I am trying to use sendkeys method to send ENTER.
set mySendKey = CreateObject(“WScript.shell”)
mySendKeys.SendKeys“{ENTER}”

but the above code doesn’t send the enter key. is there any other way that I can make sure it works? Any idea?

Thanks,
Rosh.

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: