Automation Testing

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

By Test Guild
  • Share:
Join the Guild for FREE

There are three ways to use keyboard input in QuickTest Professional and Unified Functional Testing: 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 tree views 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:
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:
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.

Also Occasionally you may have issues with event not being  triggered when using one of these methods. In that case check out my post of how to use the FireEvent method.

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:
Dim mySendKeys
set mySendKeys = CreateObject("WScript.shell")
mySendKeys.SendKeys(“{TAB}”)

To send the text you would use:
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:
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:
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.

Since I posted this I recently came across some more Device Replay Key codes for the PressKey, Keydown, Keyup and PressNKeys that I’d like to share with you. These methods are particularly helpful in certain situations in which you need to perform an action using a code that does not have an ASCII equivalent. Check out my QTP Secret Code Chart Revealed for more info: https://testguild.com/2011/07/19/qtp-secret-code-chart-revealed-for-devicereplay-presskey-keydown-keyup-and-pressnkeys/

Hope this helps.

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

    Should be

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

  2. 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

  3. 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”

  4. 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

  5. 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

  6. 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?

  7. 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.

  8. 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.

  9. 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!

  10. 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?

  11. 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!

  12. 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.

  13. 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 ?

  14. 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

  15. 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.

  16. 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

  17. 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?

  18. 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?

  19. 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

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

  21. hi Joe,

    i have assiged a virtual object in a webpage .but the page is scrolled a little bit up or down every time .

    so please suggest a better solution to select the virtual object or else to scroll the page up inorder to make the virtual object available in the same location

  22. Hello Joe,
    When I record java application in QTP11.0 for scripting (with java addin), the right mouse click contents are not selecting, it will open the right click but it does not select the the required field.
    Kindly let me how to select it.
    And another question is in the java scripting at QTP 11.0 , the java page opening but it not recording the object in the page, like selection radio button and java button.I try to spy those two tabs but spy is identifying.
    Thanks

  23. 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.

  24. 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??

  25. 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.

  26. Hi Joe: How to use Mouse Double Click using the Send or Type method? Kindly Advise

  27. Hi Joe,

    Is it possible to check which mnemonic is underlined after pressing the “Alt” key? In one of our scenarios, we need to verify whether character “b” is underlined when we press “Alt” key for a checkbox control. Any idea?

    Regards

  28. I’m not sure when you spy on the control after pressing an Alt key is there any property that changes? If so you can check for that property in your test to confirm that the Alt occurred. I don’t know if there is an ASCII character you could look for that represents an underline character. Anyone else have any suggestions?

  29. I am writing a QTP function to send emails through Outlook 2007. I am unable to send the email programmatically using Outlook object because I am getting security Dialog Popup.
    Keyboard shortcut to send Email from Outlook is Alt+s. How to press Alt+s using Presskey method.

  30. Please HELP!!!!!
    I’ve been using in IE8 Grid a .fireevent “ondblclick” and a .set methods to enter data in a grid.
    A IE had a lot of isues in many ways, we’ve migrated to Firefox 12.
    Trying to call the same funtion, neither the .fireevent and .Set methods worked.

    Have tried several ways to set the information using:

    “set mySendKey = CreateObject(“WScript.shell”)
    var1 = “Datatable (“parameter1”, “global”)
    mySendKeys.SendKeys (var1)”
    And
    “myDeviceReplay.SendString var1”
    without any success.
    In both cases the var1 is entered VERY FAST! and only remains on the object the last 3 letters of such variable.
    have tried one by one on a loop with “wait 1”

    Please help!!!!!

    Ragards and thank you!

    Alfonso

  31. Hi,
    I am using following command :-
    set WshShell = CreateObject(“WScript.Shell”)
    WshShell.SendKeys “{ENTER}”
    WshShell.SendKeys “+:techstack”
    I observed it is not writing ‘+’ symbol, the data written by script is “:techstack” instead of “+:techstack”
    Please help me with this issue.
    Thanks,
    Pooja

  32. someone can help me?
    I can’t make the keyboard button PAUSE work.
    What happnes?
    i’m used this code :

    wait 2
    Set myReplay = creatobject (“Mercury.DeviceReplay”)
    myReplay.PressKey 116 ‘PAUSE
    wait 2

    I need to clear the screen of Mainframe.

  33. Hi Joe,

    I am using Jenkins slave and trying to run the scripts in a remote machine. I am having issue in executing the SendKeys or DeviceReplay steps (to enter data in a WebElement). There are executing only when i am logged into the Remote. Is there any other alternative?

    Thanks,
    Sudharshana

Comments are closed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Leveraging AI and Playwright for Test Case Generation

Posted on 11/22/2024

Two BIG trends the past few years in the software testing space been ...

Symbolic AI vs. Gen AI: The Dynamic Duo in Test Automation

Posted on 09/23/2024

You've probably been having conversations lately about whether to use AI for testing. ...

8 Special Ops Principles for Automation Testing

Posted on 08/01/2024

I recently had a conversation, with Alex “ZAP” Chernyak about his journey to ...

Register for AutomationGuild 2025 Online Event (Before Early Price Increase) - Join Now! >>