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{+}"
wait(2)
objKey.SendKeys "2"
wait(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

Keystroke

Keyboard Symbol

Ctrl

^

Alt

%

Shift

+

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: Software.testing.genius@gmail.com

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 :

10 comments ↓

# 1 Saurav Shingote on 2011-12-31 04:56:43
Hi Yogindernath,

Thats a fantastic article for sendkeys tutorial on QTP. I hope this code will run on all versions of QTP. Keep posting more.


# 2 Steven S on 2011-12-31 05:08:14
Hi,

First of all I would like to appreciate all the efforts you have put in for this excellent website on Software testing. I am going to ask a very basic question on QTP as I am a newbie in QTP and looking to build my career in Automation.

In the above Vbscript code, you have used words like "Set" and "CreateObject". Can you please throw some light on what they are and how they can be used.

Please apologize me if I am asking a very basic question.

Steven


# 3 Rameshwari Guha on 2012-01-01 03:32:24
Nice article. I tried the tips . It works. Thanx once again


# 4 Yogindernath on 2012-01-05 10:36:37
Thanks Saurav for the encouraging words. I will try to post more and more articles on QTP soon.


# 5 Yogindernath on 2012-01-05 10:49:40
Hi Steven,

First of all, no question is silly. You are most welcome to ask anything even if its a very basic thing.

Anyways, to answer your query, Both "Set" and "CreateObject" are VBScript specific keywords and not QTP specific. The Set command tells VBScript engine that you intend to use the objKey variable to store an object reference. Thats it. However, the CreateObject() function actually does the real work of creating the object and store the reference of the object in objKey variable.


# 6 Rishi Kurra on 2012-01-06 09:10:26
Hi,

I am going to share a very frustrating issue which you. As far as the HP support goes, I had a very bad experience. Initially, its very difficult to find any information on QTP.

Few days back, I ran into a concurrent license issue in QTP 11 and I called the HP customer support. To my horror, I got this reply 'Quick Test?? is that one of our products?.'


# 7 Ankur Jain on 2012-01-06 09:36:51
Can anyone answer my question on QTP please. Its really urgent and gating issue for us.QTP 11.0 does not record "SystemUtil.Run" statement on windows. Can anyone provide the resolution please.

Regards,
Ankur Jain


# 8 Yogindernath on 2012-01-06 21:44:39
Hi Ankur,

Its a known issue with QTP 11 that it doesnt record SystemUtil.Run statement.As a workaround, you can manually add this yourself till this issue gets fixed.

Cheerz,
Yogindernath


# 9 Nitisha on 2012-01-18 08:42:00
Hi everyone,

I have few doubts related to the mouse movements in order to retrieve the Tool tip for an Object (Link, Image etc). For example my requirement is if the object is located at (x,y) coordinates, I need to move the mouse to that location, wait for some time and then retrieve the tool tip through QTP.

Can anyone please help?

With Regards
Nitisha


# 10 Yogindernath on 2012-01-18 19:52:29
Hi Nitisha,

In order to fetch the tool-tip of an image through QTP, use the below code:

Browser("Browser").Page("Page").GetROProperty("alt")

However in order to fetch the tool-tip of a link through QTP, use the below code:

' Place mouse cursor over the link
Browser("Browser").Page("Page").WebElement("text:=My Yahoo!").FireEvent "OnMouseOver"
wait 1

' Grab tooltip
ToolTip = Window("nativeclass:=tooltips_class32").GetROProperty("text")

Let me know in case you have any questions on QTP.



Leave Your Comments: (*) Marked Fields are Mandatory

You can apply basic formatting to the text

Name *
 
Email Address *
 
Website
 
Speak your mind
characters
sex hikayeleri