İstanbul evden eve nakliyat Beylikd¨¹z¨¹ evden eve nakliyat Tuzla evden eve nakliyat
VBScript Sendkeys Tutorial for QTP 11
Delicious Bookmark this on Delicious
software testing genius

VBScript Sendkeys Tutorial for QTP 11

Welcome to “Software Testing Genius”. Subscribe to my RSS feed for latest content on Software Testing.

VBScript Sendkeys Tutorial for QTP 11.0

Most of the applications like Microsoft Word or Microsoft excel have exposed their object model so that we can directly interact with these applications through their COM interface by instantiating them and later we can access all methods and properties.

The only catch here is that we should know all the methods and properties of the corresponding object. Sometimes, it could also happen that the object model is not exposed by the vendor. But you can still do away with this limitation by using VBScript Sendkeys() method.

Sendkeys() method works well with both web based as well as windows based applications. You can send a single keystroke or a combination of keys to the active window. The only thing you need to keep in your mind is that your keystrokes would be received by any application, which currently has the focus.

To send keystrokes, you would need to instantiate WScript.Shell.

Some examples of using sendkeys are shown below:

Example –1: Open command prompt and send some keystrokes

color=#0000cc>Set objKey = CreateObject("WScript.Shell")
objKey.Run "cmd"
objKey.AppActivate "cmd"
WScript.Sleep 10000
objKey.SendKeys "dir"
WScript.Sleep 10000
objKey.SendKeys "{ENTER}"

Run the above code in a vbs file. It will open command prompt and send "dir" keys and then send the "Enter" key.

WScript provides a function called Sleep which is used to wait for specified milliseconds. In our script, we are waiting for 10000 milliseconds or 10 seconds(10000 milliseconds=10 seconds).

Try to run the above code in QTP. It will error out as WScript is a host specific object which is not recognized by QTP. To run the above code in QTP, replace the WScript.Sleep statement with wait statement.

The modified code is shown below:

Set objKey = CreateObject("WScript.Shell")
objKey.Run "cmd"
objKey.AppActivate "cmd"
wait (2)
objKey.SendKeys "dir"
wait (2)
objKey.SendKeys "{ENTER}"

Example –2: Using Sendkeys to send keystrokes to Calculator

Set objKey = CreateObject("WScript.Shell")
Set objCalc=Window("nativeclass:=SciCalc", "index:=0")
SystemUtil.Run "calc.exe"
wait (4)
objKey.AppActivate "Calculator"
objKey.SendKeys "2{+}"
objKey.SendKeys "2"
objKey.SendKeys "="

We have seen that in order to press enter, we can use objKey.SendKeys "{ENTER}". However, there are two more ways to press enter using Sendkeys.

1. Use the Tilde character: ~

We can use in the following way: obj.Sendkeys ("~")

2. Use the ASCII character for Enter or carriage return which is Chr(13)

obj.SendKeys (Chr(13))

Please note that in this method, we can not use double quotes

Using Sendkeys, we can even send repeated keys to an application. For example,

obj.SendKeys "{D 20}" 'Sends 'D' character 20 times

obj.SendKeys "+{TAB 5}" 'Simulates SHIFT+TAB 5 times

The following chart illustrates the symbols for Control, Alt and shift keys


Keyboard Symbol







Also, we can simulate sending space key using the below code:

obj.SendKeys " "

We can also use Sendkeys to send right mouse click using QTP on some windows which supports simulating right mouse clicking using Shift and F10 key.

We can use the below code for the same:

obj.SendKeys "+{F10}"

Many More Articles on HP QuickTest Professional 

Download Full Study Material - HP QTP & QC Certification Exams

Practical Roadmap to QTP Certification

Rehearsal of QTP in 1 Hr. -  Interview Questions

What Successful Testers say about the Quality of this website

If you want to keep track of further articles on Software Testing,
I suggest you to subscribe my
RSS feed

You can also Subscribe by E-mail
and get All New articles delivered directly to your Inbox.

Quick Navigation of Software Testing Genius

Get your Absolutely Free Copy of Several MS PowerPoint Presentations & E-Books related to ISTQB, HP Load Runner, IBM RFT, HP QTP & QC Certification Exams, prepared by Popular Writers & Trainers, by writing to:

Full Study Material for International Certification Exams:

Largest Database of Sample Papers - 1000+ Unique Questions for ISTQB Foundation Exam

ISTQB Foundation Exam - Full Crash Course for Download

ISTQB Advanced CTAL Test Analysts Exam - Full Crash Course for Download

ISTQB Advanced CTAL Test Manager Exam - Full Crash Course for Download

Consolidated Study Material - All ISTQB Certification Exams

Practical Roadmap to CSTE Certification

Full Study Material to prepare for other Automation Tools & QA:

Study Material - IBM RFT Certification Exam

Study Material - HP LoadRunner Certification Exams for All Modules

Consolidated Study Material - Testing & QA

Rehearsal of LoadRunner in 1 Hr. -  Interview Questions


Comments :

comments ↓

Leave Your Comments: (*) Marked Fields are Mandatory

You can apply basic formatting to the text

Name *
Email Address *
Speak your mind
sex hikayeleri