Sendkeys method in QTP

By | May 10, 2010
Sendkeys method in qtp with examples:
Sendkeys method is like boon for QTP testers, In some cases QTP tool becomes dumb and not able to identify or record the windows based application, In that case rather than not doing anything we can still automate it using sendkeys method. You can send any character to application to perform specific action i.e If you want to close the notepad then you can do it by sending,
1. ALT+F and
2. X.
Or
only ALT+F4

In QTP how can we perform this action? Let’s take one simple and practical example.

Following is the simple example which perform closing notepad action using sendkeys method in qtp.
==============================
Set WshShell = CreateObject(“WScript.Shell”)
WshShell.SendKeys “%f”
wait 3
WshShell.SendKeys “x”
wait 3
Set WshShell = Nothing
==============================
OR
==============================
Set WshShell = CreateObject(“WScript.Shell”)
wait 2
WshShell.SendKeys “%{F4}”
wait 3
Set WshShell = Nothing
==============================
In above example you can see that I have sent “ALT+F” by “%f”, The special character for ALT is “%” same for SHIFT is “+” and for CTRL is “^”.
But make sure that active window must be notepad not the QTP itself otherwise you may end up with closing QTP.
So Keep in mind that whenever you are sending any key you first need to activate the specific application on which you want to send the keystroke or want to perform some action. You can also use some QTP scripting in combination with sendkey method which can make your application more reliable and robust.

Following is the example of QTP scripting and sendkeys method combination, In following simple example I first make my player window active and then sending SPACE to play or pause the file.
===================   
Window(“FLVPlayer”).Activate   //This statement is recorded by QTP which activate the player window first.       
wait 3
WshShell.SendKeys ” ” //To play or pause the stream
wait 2
===================

In following table you will find all keys and their argument that you may need.

Name of Key

Argument

BACKSPACE

{BACKSPACE}, {BS}, or {BKSP}

BREAK

{BREAK}

CAPS LOCK {CAPSLOCK}

{CAPSLOCK}

DEL or DELETE

{DEL}

DOWN ARROW

{DOWN}

END

{END}

ENTER

{ENTER} or ~

ESC

{ESC}

HELP

{HELP}

HOME

{HOME}

INS or INSERT

{INSERT} or {INS}

LEFT ARROW

{LEFT}

NUM LOCK

{NUMLOCK}

PAGE DOWN

{PGDN}

PAGE UP

{PGUP}

PRINT SCREEN

{PRTSC}

RIGHT ARROW

{RIGHT}

SCROLL LOCK

{SCROLLLOCK}

TAB

{TAB}

UP ARROW

{UP}

Y 10 times

{Y 10}

Space

” “

plus sign      

 “+”

caret            

“^”

percent sign

“%”

tilde      

“~”

Note: To send SPACE you can use ” ” as a argument. I have taken above table from QTP help you can look in to QTP help for more details.

Other than sendkeys method in QTP you can also use AutoIT tool for more advance automation. AutoIT v3 is a freeware BASIC-like scripting language designed for automating the windows GUI and general scripting. You can write autoIT script for automating your application and can trigger autoIT script through QTP. You can visit and learn more about AutoIT v3 and widen the area of automation to the next level.

If you like this article, You can subscribe through mail.

Leave a Reply

Your email address will not be published. Required fields are marked *