Ways to send keyboard input to an application using QTP when it does not record using normal recording | Quick Test Professional(QTP)
.

Ways to send keyboard input to an application using QTP when it does not record using normal recording

There are occasions when keyboard inputs are not recorded or replayed successfully using the normal recording methods of QTP. Also many applications depend on keyboard events to trigger the opening of a window/pop-up window or displaying an edit field. To send keyboard inputs to an application using QTP we have many ways. I suggest using them in the order in which they appear below. (I prefer using co-ordinates recording as the last option in QTP.

1. Use the simplest .Type ( key ) to send keyboard input.
Eg: Window("Notepad").WinEditor("Edit").Type micF5

2. Use the Windows Scripting SendKeys method
There are some Web/Windows objects which will perform actions when certain key commands, such as CTRL+SHIFT+ESC are entered. These Web/Windows objects do not have a type method associated with them that can be used to replay these keys. In these cases you can use SendKeys method to send keyboard input to your application.
Download and install the Windows Scripting Host.
1. Create a WScript.Shell object.
2. Activate the browser in which you want to execute the keys.
3. Use the SendKeys method to type the key combination.
Example:
' This code executes the CTRL+F key combination (search) on a browser.
Set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate "Put the label of the browser" ' Activate the browser window
wait(3)
WshShell.SendKeys "^f" ' The caret (^) represents the CTRL key.
wait(2)
The SendKeys method will send keystroke(s) to the active window. To send a single character (for example, x), use "x" as the string argument. To send multiple characters (for example, abc), use "abc" as the string argument. You can also send special characters such as SHIFT, CTRL, and ALT, which are represented by the plus sign (+), the caret (^), and the percent sign (%), respectively.
For more information on the SendKeys method, please refer to the MSDN SendKeys Method page.
  
3. Use Analog or low-level recording when entering keyboard input

4. Use Mercury Device Replay feature
The Device Replay feature is used to perform mouse and keyboard actions against screen co-ordinates that are provided. The Device Replay functions are not automatically recorded, but must be programmed manually in the Expert View. (I refrain from using this feature generally)

The steps involved are:
1.    Creating the Device Replay Object.
2.    Calling the Device Replay function. 
3.    Destroying Device Replay object.

Here is an example to simulate F5 function key using DeviceReplay.
Set obj = CreateObject("Mercury.DeviceReplay")
Window("Notepad").Activate
obj.PressKey 63Note:
The PressKey method uses the appropriate ASCII or IBM Scan Code value for the key. "63" is the IBM Scan Code value for F5.
For more information on ASCII and IBM Scan Codes, refer to Lookup Tables. For the ASCII characters, click the "ASCII Table" tab at the top of the page. For IBM Scan Codes, click the "Scan Codes & EBCDIC" tab. The value needed for the PressKey method is the decimal value.

Reference: QTP Documentation

If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS Feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.

6 comments:

roh said...

hi ankur,

when i try to download some file and try to record this action,it does get recorded but when i try to run the action it gives an error as(can't find the parent object) can u help me out to fix this problem

Vamsi said...

Hey Ankur,

gr8 work. Its helping me a lot in my daily routine. Thank u very much......

Anonymous said...

hey ankur,
im abi, u did a great job can you help me how to write scripts frm QTP 9.2 give me some samples...
im very new from this projects...

Raj said...

Hey Ankur,
i am raj, this is a great help for me.
can you help me how to work on unix and linux through QTP9.5.
how to write qtp script on Linux and unix environment.
Please help.
Provide some sample QTP script which is developed in Unix and linux.
Thanks,
Raj.

Debashis said...

if any one know how to scroll the web page by qtp script, please post with suggestions

Anonymous said...

Hi Ankur,
First i am thanful to u for the blog,which is i found realy very helpful.
i faced a question that how u will enter a value in a webedit box without using .set method in qtp?
still this is unsolved for me.
Thanx in advance
Alok