VB Script and QTP - Part2 | Quick Test Professional(QTP)
.
QTP Blog RSS

VB Script and QTP - Part2

This is in continuation from VB Script and QTP - Part1 on our series of posts on VB Script. Here, we will dwell upon conditional constructs, iterative constructs and arrays.
Conditional Constructs
Conditional Constructs execute statements or repeat certain set of statements based on conditions.
The following conditional constructs are available in VBScript
· If – Then –Else
· Select Case

If – Then – Else Construct

The If – Then- Else Construct is used to evaluate whether a condition is true or false and depending on the result, to specify one or more statements to execute. Usually the condition is an expression that uses a comparison operator to compare one value or variable with another. The If- Then – Else statements can be nested to as many levels as needed.
For example:
Sub ReportValue(value)If value = 0 ThenMsgBox valueElseIf value = 1 ThenMsgBox valueElseIf value = 2 thenMsgbox valueElseMsgbox "Value out of range!"End If

You can add as many ElseIf clauses as you need to provide alternative choices. Extensive use of the ElseIf clauses often becomes cumbersome. A better way to choose between several alternatives is the Select Case statement.

Select Case Construct

The Select-Case structure is an alternative to If Then Else for selectively executing one block of statements from among multiple blocks of statements. The Select Case Construct makes code more efficient and readable.

A Select Case structure works with a single test expression that is evaluated once, at the top of the structure. The result of the expression is then compared with the values for each Case in the structure. If there is a match, the block of statements associated with that Case is executed.

For example:

Select Case Document.Form1.CardType.Options(SelectedIndex).Text

Case "MasterCard"

DisplayMCLogo

ValidateMCAccount

Case "Visa"

DisplayVisaLogo

ValidateVisaAccount

Case "American Express"

DisplayAMEXCOLogo

ValidateAMEXCOAccount

Case Else DisplayUnknownImage PromptAgain

End Select

Iterative Constructs
Looping allows to run a group of statements repeatedly. The loop is repeated based on a condition. The loop runs as long as the condition is true. The following looping constructs are available in VBScript.
· Do – Loop

· While – Wend

· For – Next

Do – Loop

Do – Loop statements are used to execute a block of statements based on a condition. The statements are repeated either while a condition is true or until a condition becomes true. While Keyword can be used to check a condition in a Do – Loop construct. The condition can be checked before entering into the loop or after the loop has run at least once.
The basic difference between a “Do while – Loop” and “Do - Loop while” is that the previous one gets executed only when the condition in the while statement holds true where as a “Do – Loop while” gets executed atleast once, because the condition in the while statement gets checked at the end of the first iteration.
While – Wend

The While...Wend statement is provided in VBScript for those who are familiar with its usage. However, because of the lack of flexibility in while...wend, it is recommended that you use Do...Loop instead.

For..Next

The For-Next loop can be used to run a block of statements a specific number of times. For loops use a counter variable whose value is increased or decreased with each repetition of the loop. The Step Keyword is used to increase or decrease the counter variable by the value that is specified along with it. The For-Next statement can be terminated before the counter reaches its end value by using the Exit For statement.
For example:
Dim j, total
For j = 2 To 10 Step 2

total = total + j

Next
MsgBox "The total is " & total
Arrays

An array is a contiguous area in the memory referred to by a common name. It is a series of variables having the same data type. Arrays are used to store related data values. VBScript allows you to store a group of common values together in the same location. These values can be accessed with their reference numbers.

An array is made up of two parts, the array name and the array subscript. The subscript indicates the highest index value for the elements within the array. Each element of an array has a unique identifying index number by which it can be referenced. VBScript creates zero based arrays where the first element of the array has an index value of zero.

Declaring Arrays

An array must be declared before it can be used. Depending upon the accessibility, arrays are of two types:
· Local Arrays

A local array is available only within the function or procedure, where it is declared.

· Global Arrays

A global array is an array that can be used by all functions and procedures. It is declared at the beginning of the VBScript Code.

The Dim statement is used to declare arrays. The syntax for declaring an array is as follows:

Dim ArrayName(subscriptvalue)

Where, ArrayName is the unique name for the array and SubscriptValue is a numeric value that indicates the number of elements in the array dimension within the array.

Example:

Dim No_Passengers(3)

The No_Passengers can store 4 values.

Assigning values to the array
No_Passengers(0) = 1
No_Passengers(1) = 2

No_Passengers(2) = 3

No_Passengers(3) = 4
Static and Dynamic Arrays:
VBScript provides flexibility for declaring arrays as static or dynamic.

A static array has a specific number of elements. The size of a static array cannot be altered at run time.

A dynamic array can be resized at any time. Dynamic arrays are useful when size of the array cannot be determined. The array size can be changed at run time.

Next we will deal with user defined procedures, functions and subroutines.


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.

Please use the Testing Tools forum for posting questions now!

38 comments:

Anonymous said...

Hi Ankur,
I am a frequent visitor to your blog and I must take this oppurtunity to appreciate your work. It is amazing!!!!!

I started reading your post on VBScript & QTP.
Can you provide extra documentation or links on dynamic arrays?

Thanks...

jala said...

Hi ALL,
I am also using the qtp 9.0 and I am beginner in vb scripting
Can u please guide me from where I have to start and how and also scripting techn
Tell me the major functions and procedures where I have to concentrate
Please forward the supported documents or information to my mail id
Mail id : jalaramu99@gmail.com
Regards & Thanks
Ram

Theja said...

Hello,

I am a beginner to QTP and VB scrit. Please let me know what are the major functional areas I need to start off with. Other than the check points, let me know other areas involving data driven, excepion handling..Please do email anything necessary at thejach@gmail.com

Thanks
Theja

rohit said...

Hi, I am very much impressed by your posted scripts, but can you provide me some QTP scripts related to Web Testing. Means How I can test any web application with QTP. SO If you ever test it then send me doc.

rohit said...

Hi,This is Rohit Tyagi. I am very much impressed by your posted scripts, but can you provide me some QTP scripts related to Web Testing. Means How I can test any web application with QTP. SO If you ever test it then send me doc.

My Email Id: rohit.tyagi83@gmail.com

kathiravan said...

Hi friends am new in QTP 8.2 i don't have much more idea
could you explain how to write scripts and how to store the repository EXPLAIN Briefily

Anonymous said...

hi,
i am varma. I am new to QTP. can you provide me some QTP scripts related to Web Testing. Means How I can test any web application with QTP. SO If you ever test it then send me doc.

My Email id is nadimpalli_2002@rediffmail.com

amar said...

Hi,
I am new to QTP and Vb scripting. please provide me guide lines & links to improve my scritping knowledge and tool administration.

i have read your VBscripting parts.it is really helpfull for me. please provide me links and material.

Nath(amar.madisetti@gmail.com)

Ankur said...

@ Amar:

Please use Testing Tools forum for posting QTP related questions.

ooty said...

HI,
I am murali , I am new to QTP. Can you help me out with QTP descriptive program.

mail: muralishankarp@gmail.com

Regards
Murali Shankar

vishal said...

Hi,

I am vishal. I am a beginner to QTP and VB scrit. Please let me know what are the major functional areas I need to start off with. Other than the check points, let me know other areas. Please mail me at vishalps.sankar@gmail.com


With Regards
Vishal

Ramadevi said...

Hi,

I am Rama. I am new to QTP and Vb scripting. please provide me guide lines & links to improve my scritping knowledge and tool administration.

please provide me links and material.

email: grama.devi@yahoo.com

with regards,
Rama

Anonymous said...

Hi,
I am into manual s/w testing..i am not from programming back ground..pls let me know if i want learn QTP/Automation what is ground work i need to do. I am totally new to automation.

Thanks in advance

email:meandtestin@gmail.com

Anonymous said...

Hi Ankur,
Superb job!

Can you please provide some material on VB scripting in detail with examples.

Thanks in advance.

email:arshnraj@gmail.com

Vinay said...

Hi Ankur,

I am new to QTP, can you please provide me trial version link for QTP 8.2, and some docs. It would helps me lot

Anonymous said...

hi shravan here
i have konwlegde on some basics on qtp but i dont know abt discriptive programing and data table i.e data driven testing methods & recovery scinorio manager would u like to help regarding these topics

Anonymous said...

Hi,
I am Haritha.Can you please provide some material on VB scripting in detail with examples.
And also would you provide me some QTP scripts related to Web Testing. Means How I can test any web application with QTP. SO If you ever test it then send me doc.

Anonymous said...

Hi Ankur,
I am a beginner to QTP and VB Scripting, learning on my own now.
Highly appreciate your work! The questionnaire and interview was really helpful.

I want to give a certification on QTP 9.2. I do not know much of VB Scripting, what areas of it would I need to practise on?
Also could you send me any other questionnaire you have on QTP 9.2, which would be helpful for my preparation.

Could you pls mail me at: nagavarapusridevi@gmail.com
Thanks,
Sri.

sunil said...

hi! ankur
this is sunil ur document is effective for beginers can u jus provide me a basic idea of starting qtp(how to start) i can able to apy an object and add to object repository and can work on it.. but i want to learn in a proper way.. also the script part.. if u can help me out, tht would be great.... hopin for ur response
thanks and regards
sunil

Rekha said...

Ankur,

Hats off to you. The knowledge you are sharing with a vast community of QTP & VB Script is of immense and immesurable value.

When ever I get a doubt in QTP I feel like no problem...Ankur is there and blog will solve my problem....

Thanks You so much...and keep helping Eklavya sishyas like us...

Thanks a billion!

Rekha

Ankur said...

Thanks Rekha... It was nice to hear that :)..

You can also do this site a favor by spreading the word across to your colleagues/friends.

Akber said...

Hi Ankur,

I am interested in taking the exam for QTP certification. Can you please tell me where and how I can find out about the certification exam. I have been reading your posts and all the information you have on your website about the QTP. I am very interested in getting the certification for QTP. Also, I need to find out if I can get the braindumps etc... for studying for the exam.

Anonymous said...

Can you provide the examples for Static array and dynamic array..

Anonymous said...

Hi Ankur ,
I am into manual s/w testing..i am not from programming back ground..pls let me know if i want learn QTP/Automation what is ground work i need to do. I am totally new to automation and also i am vert intrested to learm qtp .
And also send me a material or URL for descriptive programming and framework
Thanks in advance

Email:gudla.vasu9@gmail.com

REDDY said...

Hi ANKUR,
This is SIVA SARAN
I am new to qtp and came to know a little bit abt that
Actually to know it completely also VBScript right?
can u guide ME the important methods and inbuild statements in VBScript like ExecuteFile to call a vbscript via QTP

can u send that
MY Mail id is sivasaran558@gmail.com

sirisha said...

Hi Ankur,
Iam learning QTP from ur blog.
Can u let me know if I need to know VBscript in depth to work on QTP or jus an idea is enough.
I have some knowledge in HTML and C programming.
Thank You.

Anonymous said...

Hi Ankur,
Do we need to know VBScript indepth to be a good at QTP

jayshree said...

Hello i m jayshree.plz can any one let me know how vb script can be use for load testing.
Could you pls mail me at:jayshrichaudhari@yahoo.co.in

masthan 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

pannu said...

Hello Ankur
well can u plz tell me tht the link which is for the qtp download site in that what is the main demo version of QTP there after login all there is the Addin only so in that can u plz tell me what is that link from which should i get the QTP demo version, plz its a heartly request to u ..

thank u

Anonymous said...

any body can help me my application shouldnt accept duplication email ids...please send me code....my id is shakila.shetty@sysfore.com

Pramod Todakar said...

Hi All,
Plz tell me how "getElementsByTagName" method works? and where should we use this?

Pramod

JT said...

Hi,

I am automation engineer and working with QTP 9.5. I am found of the forum due to the knowledge I gain from it. Currently there is no help. I am trying to do a multiple Dimensional Array, but I cannot not preserve myArray with the previous value. Please help me.

Anonymous said...

Hi

You can preserve the array by using Redim Preserve arrname

naveenkumar said...

hi
this is naveen
i want to learn automated tools
like qtp,load runner,win runner.
I dont have any idea about testing can u help me in learning the automated tools.
thank u
if u want to send any thing to me
send to my mail
naveencgr541n@yahoo.co.in

Anonymous said...

How do I use three filters to grab the count of web objects on a page?

I want to get the number of objects on a page where the objects have these values:

micClass = WebElement
html tag = SPAN
x = 180

Here is the code I have so far:

=================================================

'Gather the number of filtered objects on the website.

On Error Resume Next
webCount = 0
Set Doc = Browser("micClass:=Browser").Page("micClass:=Page").Object

'Loop through all the objects in the page.

For Each Element In Doc.all
If Element.TagName = "SPAN" and Element.x = 180 then
webCount = webCount + 1
End If
Next

Msgbox "The number of objects are " & webCount

=================================================

The msgbox should come back with a count of 4 instead it comes back as 93.

I have tried other IF..Else If…Then and just IF...Then statements, but that didn’t work either.

Any ideas?

Steve
Email: slang46188 @ aol.com

Anonymous said...

Hi Steve,

First create description object with the values you specified initially.

Set Odesc = Description.Create
Odesc("MicClass").Value = "WebElement"
Odesc("html tag").Value = "SPAN"
Odesc("x").Value = "180"

Set WebElementCnt = Browser("micClass:=Browser").Page("micClass:=Page").ChildObjects(Odesc)

Msgbox WebElementCnt.Count


By the above thing you can get the number of the objects present in a web page........

soumya said...

hi,
This is Soumya

I want to know whether we can automate the test scrip for testing the word Document. We need to check the page set up,indentation,character spacing,font size and the spelling check in the pages.

Please let me know whether this can be done by atumation.