VB Script and QTP - Part1 | Quick Test Professional(QTP)
.

VB Script and QTP - Part1

Going forward, I am starting a series of posts on using VBScript with QTP. It will start from the basics of VB Script and move to the advanced course.

What is VBScript?

VBScript is a subset of Visual Basic 4.0 language. It was developed by Microsoft to provide more processing power to Web pages. VBScript can be used to write both server side and client side scripting. (If you already know Visual Basic or Visual Basic for Applications (VBA)VB_Script_code, VBScript will be very familiar. Even if you do not know Visual Basic, once you learn VBScript, you are on your way to programming with the whole family of Visual Basic languages.)

Data types

VBScript supports only one data type called ‘Variant’. The variant data type is a special kind of data type that can contain different kinds of information. It is the default data type returned by all functions in VBScript. A variant behaves as a number when it is used in a numeric context and as a string when used in a string context. It is possible to make numbers behave as strings by enclosing them within quotes.

Variables

A variable is a placeholder that refers to a memory location that stores program information that may change at run time. A variable is referred to by its name for accessing the value stored or to modify its value.

Variable Declaration

Variables in VBScript can be declared in three ways:
  1. Dim Statement
  2. Public Statement
  3. Private Statement
For example:
Dim No_Passenger
Multiple variables can be declared by separating each variable name with a comma. For example:
Dim Top, Left, Bottom, Right
You can also declare a variable implicitly by simply using its name in your script. That is not generally a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables. The Option Explicit statement should be the first statement in your script.
Note:
Variables declared with Dim at the script level are available to all procedures within the script. At the procedure level, variables are available only within the procedure.
Public statement variables are available to all procedures in all scripts.
Private statement variables are available only to the script in which they are declared.

Naming Convention

There are standard rules for naming variables in VBScript. A variable name:
  1. · Must begin with an alphabetic character.
  2. · Cannot contain an embedded period.
  3. · Must not exceed 255 characters.
  4. · Must be unique in the scope in which it is declared.

Assigning Values to Variables

Values are assigned to variables creating an expression as follows: the variable is on the left side of the expression and the value you want to assign to the variable is on the right. For example:
B = 200

Scalar Variables and Array Variables

Much of the time, you only want to assign a single value to a variable you have declared. A variable containing a single value is a scalar variable. Other times, it is convenient to assign more than one related value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name. In the following example, a single-dimension array containing 11 elements is declared:
Dim A(10)
Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based, so this array actually contains 11 elements. In a zero-based array, the number of array elements is always the number shown in parentheses plus one. This kind of array is called a fixed-size array.

Constants

A constant is a meaningful name that takes the place of a number or a string, and never changes. VBScript in itself has a number of defined intrinsic constants like vbOK, vbCancel, vbTrue, vbFalse and so on.
You create user-defined constants in VBScript using the Const statement. Using the Const statement, you can create string or numeric constants with meaningful names and assign them literal values. For example:
Const MyString = "This is my string."Const MyAge = 49
Note that the string literal is enclosed in quotation marks (" "). Also note that constants are public by default.
Within procedures, constants are always private; their visibility can't be changed.
Next post we will deal with constructs and arrays.

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.

55 comments:

Nikhila said...

Ankur

I am interested in knowing about the QuickTest Professional 9.0 Specialist Exam. I want to know about
1) cost of the exam
2) is it a web based
3) what kind of questions will be asked like is it a multiple choice or theory or practical
4)How much knowledge do you need in qtp to crack this exam.

Please email me at princess_barbie6@yahoo.com

shilpi said...

Hi Ankur,
Ankur i want to know that wats the best way to write an automation script in QTP. Actually i am a beginner so am totaly scared about to write a script. So please help me out..

ponraj said...

hi nikhila,

i m the begineer of learning QTP 8.2. which site is best one to learn properly with VBscript..

smiles said...

hi am alby smiles. i want to know about qtp certification, and the mock questions. can u plz provide me with this details

Anonymous said...

HI

can u tell me

1.Differences in qtp versions.
2. Differnce in testdirector and qualitycenter
3. Briefly explain abt qtp frameworks and which is the better one

thank you

Anonymous said...

HI

can u send me below details to k.malakondaiah@gmail.com

1.Differences in qtp versions.
2. Differnce in testdirector and qualitycenter
3. Briefly explain abt qtp frameworks and which is the better one

thank you

abdal said...

Dear Ankur,

I have gone through the VBScript material you have provided in this site. It is very clear and easy to catch all those defined. I want to write and execute some scripts on my own. Is there any VBScript editor available for free. If it is, please tell me the link from where I can get. Or otherwise please tell the tool to write and execute VBscipts. But I don't want to test through notepad and IE.

Ankur said...

@ Anon:
You can refer my posts on Differences in QTP versions and about frameworks

Refer http://www.geekinterview.com/question_details/17367 for difference between TD and QC

@ Abdal:
You can use QTP Expert View to practice on VB Script.

tc said...

i have one year exp in manual testing. web testing.. and also i know little bit about QTP.(how to use & all). but i never used in any project..
so wt to do now..hw to learn more than this..
plz inform mr to my mail id..(tc.raghavendra@gmail.com)

Kumari said...

Hi Ankur,

Thank you very much for this wonderful blogspot... Continue this good work for the benefit of so many... thanks a ton... I specially appreciate the style of your writing. It has inspired me to read about QTP(I am a kind of person who really runs away from technical stuff). But your blog is truely magnificient.

Best Regards,
Vandy.

srinivas said...

Hi Ankur

I am interested in knowing about the Quick Test Professional 9.0 Specialist Exam. I want to know about
1) cost of the exam
2) is it a web based
3) what kind of questions will be asked like is it a multiple choice or theory or practical
4)How much knowledge do you need in qtp to crack this exam.

If u have any website send me my mail Address.

Please email me at srinivas_4444@yahoo.com

neeharika said...

I am interested to know about testing cerftications like CSTE. I want to know about cost of the exam,exam pattern,how much knowledge do we need for this exam all that stuff.
Please email me at neehavijay@gmail.com

Kiran said...

This post is indeed very useful & Descriptive .Thanks

laxminarayan said...

I am interested to know about testing cerftications like CSTE. I want to know about cost of the exam,exam pattern,how much knowledge do we need for this exam all that stuff.
Please email me at buna.tester@gmail.com

Anonymous said...

hi this is vish
my request is to send a complete flow of vb script for any web appl.
for one flow or scenario.

thanking u.

Anonymous said...

hi this is vish
my request is to send a complete flow of vb script for any web appl.
for one flow or scenario.

thanking u.

pls mail me to kvishnu1281@yahoo.co.in

snig said...

good post

Anonymous said...

Thanks a bunch Ankur..!!

Anonymous said...

Thanks Ankur...

Anonymous said...

CAN ANY BODY POST ME SOME SCRIPS PREPARED ON ANY WEB DOMAINS. SO THAT I'LL PRACTICE. YOU CAN MAIL ME TO MY MAiL ID: kumar.k111@gmail.com

Raj said...

hi this is raj,

we r trying to get the sum of price of tickets (in flight application of QTP)with keeping order numbers in data table.
we want to add the total of each order with their next order and so on.
i=Window("text:=Flight Reservation").WinEdit(attached text:=Total:").GetVisibleText
msgbox i
we r unable to write further so
please help us if u can.

u can send me to rajendra_js@yahoo.co.in

with thanks

Khalida said...

I want to know what certification i need to do in Testing.
What is the best method to learn and practice VBScripting.

Please let me know via email - khalidahuda@gmail.com

Khalida said...

Hi Ankur,

I know Winrunner and currently learning QTP 9.2 on my own. Not sure how to go ahead with it as i dont know VB Scripting.
Also i need to know abt the certification... wht is it? How is it? Does it have any weightage? How is the certification help us? Cost and abt the exams of it?

Please let me know - khalidahuda@gmail.com

Thanks a lot...

Anonymous said...

Hi Ankur,
Appreciate your help for creating this blogger.I'm using QTP 9.2.If I need to run the tests by writing the scripts,whats the command for calling object repository.(as this will help the application in identifying the objects during testing)In 8.0 we used the following syntax
App.test.settings.resources.objectrepositorypath = "D:\bank_new\bank_obj_repo\bank.tsr"

where App is the variable defined as
Set App = Createobject("Quicktest.application").
Please help me with this.my email address is prasannamadhuri@gmail.com
Thanks in advance

amit said...

Set RepositoryTo = CreateObject("Mercury.ObjectRepositoryUtil")
RepositoryTo.Load PathOfTSR

PANDA said...

Hi there,
This is Pranav DAve I m new to QTP and i wann know how to use VB script in QTP pls jsuggest me any link where i can find any of the samples.
My Email ID is
parth.engineer@gmail.com

Anonymous said...

Hi Guys,

Just would like to mention few things here.
Don't be scared of any automation tool.Once u get hold of concept then they are prety same. Move in follwoing way:
Go throuh User guide it has everything in it.
1. Understand the QTP framework.
2. Understant the UI of QTP.
3. Practice lesson by lesson the QTP functionalization namely:
a. Object declaration.
b. Create a UDF file (U need to include this in mail file from where u run it)where u can declare global variables and write User defined functions and proceedures.
c. record test cases.
d. learn cla ofss each object like text box, browser etc is identified by QTP.Once u record u will be able to see this.
e. Lear how to add check points,etc.
f. VB script is nothing but VB language.Learn some main functions like getting windowhandles, control structure, how to put custom messages in res file.
g. Synchronize test.

U will not learn it in day but if u go with pateince and give it about 2 months u will be become an intermediate expert.

Don't go into too complicated things initially. Like checking is IIS is running wtc.U can do that later.

Best of luck.

Karan
kbsingh1@gmail.com

sonika said...

Hi Ankur,

I am interested in knowing about the Quick Test Professional 9.0 Specialist Exam. I want to know about
1) cost of the exam
2) is it a web based
3) what kind of questions will be asked like is it a multiple choice or theory or practical
4)How much knowledge do you need in qtp to crack this exam.

If u have any website send me my mail Address.

nickychoudhary@gmail.com

srinivas said...

Can i get a reply for this Ankur..

Nikhila said...

Ankur

I am interested in knowing about the QuickTest Professional 9.0 Specialist Exam. I want to know about
1) cost of the exam
2) is it a web based
3) what kind of questions will be asked like is it a multiple choice or theory or practical
4)How much knowledge do you need in qtp to crack this exam.

Please email me at princess_barbie6@yahoo.com
12:43 AM, October 05, 2007

Thanks
Sriniv

dhruv said...

Hi Ankur,

Right now am using 9.5 version of QTP and am automating swing based client server application.Sometimes QTP is not recognizing the same object which it recognised in its previous runs.
can you suggest a solution for this iisue.

thanks

dhruv

nimi said...

I am interested in knowing about the QuickTest Professional 9.0 Specialist Exam. I want to know about
1) cost of the exam
2) is it a web based
3) what kind of questions will be asked like is it a multiple choice or theory or practical
4)How much knowledge do you need in qtp to crack this exam.

Please email me at nishritha1018@gmail.com

Ankur said...

@nimi: Please check the link-QTP certification details- given on right hand side

Anonymous said...

Nikhila/Srinivas
Please mail me the details that you had asked for to my id 2411985@gmail.com
Me too interested to know.
Thanks

Anonymous said...

I have gone through the VBScript material you have provided in this site. It is very clear and easy to catch all those defined. I want to write and execute some scripts on my own. Is there any VBScript editor available for free. If it is, please tell me the link from where I can get. Or otherwise please tell the tool to write and execute VBscipts. But I don't want to test through notepad and IE.

balur said...

Hi Ankur,
I am in a big problem i.e my system is restarting when i start to record any application.
My system is working for QTP8.2 but when installing 9.1,9.2 or 9.5 it is the problem.
To out of this i have formated the system once even though it is restarting again.
Plz send some solution to over come this to me on balur.sangamesh@gamil.com
PLzzzzzzzzzzzzzzzzzzzzzzzzzz

Anonymous said...

Hi there,
This is Ashish. I m new to QTP and i wanna know how to use VB script in QTP pls suggest me any link where i can find any of the samples.
My Email ID is
ashuniks@gmail.com

Vandana Sharan said...

Hello ...

this is the link where u can easily learn the vbscript..

http://www.w3schools.com/Vbscript/vbscript_examples.asp

Vandana`

Anonymous said...

Hai

How to declare array variables as Global varibles.kindly let me know its urgent.

I created a code but its showing type mismatch error.Please chk this .


QTP Code
----------------------


Dim MyArray(2)

MyArray(0) = "Sunday"
MyArray(1) = "Monday"
MyArray(2) = "Tuesday"

Call callweekdays()

Function Code(QFL)
----------------
(Added to Test Resources)

Public Function callweekdays()

For i=0 to Ubound(MyArray)
Msgbox MyArray(i)
Next

End Function



After execution its showing type mismatch error.

Kindly reply to this mail ID "adithya.m.kumar@gmail.com"


Thanks
Adithya Kumar

Anonymous said...

Hi Adithya Kumar,

Your VB script code looks to be perfect. Pls try executing it again.

Thanks

Bharathi

Anonymous said...

hi Ankur,
This is a great post,

although.. i've one question
while declaring fixed size arrays do we have to specify only integer value as its subscript value?

e.g.,
subscriptval = 3
dim MyArray1(subscriptval)

cant we decalre array as above?


thanks in advance,
Prashant

Anonymous said...

@adithya.m.kumar@gmail.com:
hi pass the array to your function available in the function library..

Dim myarray(2)
myarray(0) = "sunday"
myarray(1) = "monday"
myarray(2) = "tuesday"

Call xzy(myarray)

Lopamudra said...

Hi Ankur,

i want to know from where I have to start QTP scripting.

Shreyansh said...

hi,
i know the qtp but i never use it in any live project ..so pls help me to use the QTP in live projects.If u have any links regarding live project with QTp pls provide me..this is my mail id malagouda1008@gmail.com

umangi said...

Hi ankur,

Can you please please tell me a sample site which i can start automating using QTP where i can exercise VBscript most as i m totallw new to it.

Ankur said...

@Shreyansh, umangi: You can hone your skills on the windows based and web based applications that comes pre-installed with QTP.

Anonymous said...

I can use QTP to create automated tests using the GUI, but I do not have an understanding of VBScript and therefore I'm looking for a book which I can use to form the foundations and compliment the series of VB Script and QTP - Part1, Part 2, Part 3, etc on this website. Can you please suggest the most relevant book for an absolute beginning with no programming experince please.

Anonymous said...

Hi Ankur,
In a recent interview i was asked a question to write a VBScript for this

If provided the salary amount (ask for salary amount) and
calculate and output tax on the income using VB Script

Taxable income Tax on this income
"$0 – $6,000" Nil
"$6,001 – $34,000" 15c for each $1 over $6,000
"$34,001 – $80,000" $4,200 plus 30c for each $1 over $34,000
"$80,001 – $180,000" $18,000 plus 40c for each $1 over $80,000
"$180,001 and over" $58,000 plus 45c for each $1 over $180,000

I wrote the code using select case, i'm not sure is i was correct.
Could you please tell how to put this VBScript

renuka said...

Hi Ankur,

I am trying to download the free trail version of QTP but could not do it. Please help me how to install the free trial version of QTP

Anonymous said...

Hi Ankur ,
I was into Manual testing all these days , But Now My Company has permited me to use Automation tool, Can we test D2K Applications Using QTP, If Yes, Can I know the Steps and Procedures for testing. i have QTP 8.2 Can i Install this Version or do u recommend other Versions ?

Deepa said...

Hi Ankur,

I am new to testing. I am interested in learning QTP..
Pls guide me how to start and how
to use QTP in live projects.
Thanks in advance.

suru said...

hi ankur
m new to testing n m trying 2 learn qtp 9.0... i hv read sm ppt of QTP but wanna learn hw 2 write scripts....cud u send me sme material or documents n pls suggest me any link where i can find any of the samples.
My Email ID is
suruchivirmani@gmail.com

soni said...

Hi Ankur,

I am have been working into manual testing from last 1.5 yrs & have sum knowledge of QTP but i never used it.
I want to learn the scripting of QTP .can you plz provide the guidelines for beginner.And also wnt to know is there any tool we can use for Microsoft Dynamics AX since my platform is AX.
Thnxs
mail id:vaishali.bankar@gmail.com

Aman said...

hi,
i know the qtp but i never use it in any live project ..so pls help me to use the QTP in live projects.If u have any links regarding live project with QTp pls provide me..this is my mail id richa.singh.mn@gmail.com

Bamboo said...

Hi Ankur,
I am a tester and i worked in software testing for 2 years. I always manual test software and never use test tool before. Now, i want to learn QTP for my job but i don't know how to start? Could you please show me the way to learn QTP sufficiently?

stacy said...

Hi Ankur
Can u ples send me some easy examples of qtp scripts .so that i can do some R&D and learn about the process.I need some scripts in whoch VB scripting is Used.
MY email ID is
xavierstacy0@gmail.com

Thnaks In advance