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.
Now you may go to VB Script and QTP – Part 3. Here is the first post in this series in case you missed it – VB Script and QTP – Part 1
Hello Ankur,
I have only used QTP for creating GUI recordings in SAP. Can you give me an example where you would use coding when it comes to SAP automation for BPT.
Hi Guys, Static arrays are those where upper boundary can not be changed. E.G: dim varr(4) which means 4 can not be changed during run time wheras Dynamic arrays are those which takes different values at different point of time or the value keep on changing during runtime. You can create dynamic arrays using Redim keyword. Lets see here:
Dim varr()
N=Cint (“how many numbers”)
Redim varr(n-1)
For I=0 to n-1
Varr(I)= inputbox(“number”)
Next
for I=0 to n-1 step1
vstr=vstr& vbnewline&varr(I)
Next
msgbox vstr
Hi Ankur,
Thanks for sharing your knowledge on QTP.
Is it necessary dat one should know HTML to code in VB Script?
I have started my profession as an automation tester and need to learn QTP and VB scripting.
I read many pages on web saying that to learn VB scripting i must know HTML.
@Sushma: No it is not mandatory but it will be very helpful in your career if you learn the basics of HTML/CSS. Learning to write HTML/CSS won’t take much time but will help you a long way in your job as an automation tester. In case of any issue, you will be able to think like a developer and that thing alone would help you to resolve those little issues that come up every now-and-then during automation. There are various free and paid courses available on the internet. My recommendation is to try HTML/CSS courses at TeamTreeHouse, I have personally gone through some of their courses and can whole-heartedly recommend them.
i am facing overflow error if i am able to find a factorial of 987456123..or some high range.it is saying overflow error.when i am giving 5 it is giving 120.can u please tell the soloution how to avoid overflow errors.
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 very interested to learn QTP .
And also send me a material or URL for descriptive programming and framework
Thanks in advance
@Arun: Please check our step by step guide to learn QTP
hi
i want to import data from ms access database and print all the information in application and then export all the data to a excel sheet . can u help
hi ankur, this is vijaya lakshmi.
i want to know how to write vb script program for this sentence “rama is a good boy” in this sentence good is exsist then result will be pass, if not exsist result will be fail “with using of if condition” how to write the script, kindly send the answer for me
thanking you,
vijaya lakshmi
Hi,I am very much impressed by your posts, I need to know which aspects needs to be understood & clear during web application testing specially client-server related. can you provide me some QTP scripts related to Web Testing. Means How & what I can test in any web application with QTP. SO If you ever test it then send me doc.
Thanks in advance,
Mail ID: abhaydk5990@yahoo.co.in
Regards,
Abhay
@pankaj sharma
1)first add the object(ok button)into object repository
ex:-assume your ok button existed in window-w1,dialog-d1,button-ok
2)write script as
Window(“w1”).dialog(“d1”).WinButton(“ok”).click
hope it will be use full
Hi ankur ,
Could you please tell me that how can we click on OK button with the help of script it self .
Regards
Pankaj sharma
Small correction in Msgbox sytax:
dim a,b
a=cint(inputbox(“enter the number for which u want Tables”))
for b=1 to 10 Step 1
c=a*b
MsgBox a&”x”&b&”=”&c
next
dim a ,b
a=cint(inputbox(“enter the number for which u want Tables”))
for b=1 to 10 Step 1
c=a*b
MsgBox a&”x”&b&”=”c
next
dim a ,b
a=cint(inputbox(“enter the number for which u want Tables”))
c=a*1
d=a*2
e=a*3
msgBox a&”*1=”&c&vbCrLf&a&”*2=”&d&vbCrLf&a&”*3=”&e&vbCrLf
Hi, I am using QTP 10.0 and am using Window based application. My application contains Tree view which is in one frame itself. Can anyone suggest how can I identify the tree view nodes in QTP?
hi,
Please could you help we writting the I want to write a vb script to print multiple of any number from 1 to 10
eg the program asks the user to enter the number ,the user enters 3
so my script should return me
3 x 1=3
3 x 2=6
.
.
.
.
3 x 10=30
the above should be printed in the same format and in a single msgbox.
I have written the code
dim a ,b
a=cint(inputbox(“enter the number for which u want Tables”))
for b=1 to 10
c=a*b
msgbox “the multiple is”&c
next
which does give me the answer but does not print the final result in a single message box and not in the format 3×10=30
Hello,
I am a beginner to QTP and VB scrit.I am a BCOm Graudate so dont have any idea reagrding this.But i got a oppurtunity to work as testing analyst and company is telling to learn QTP ples help me in this i wolud be very much thank full to you.
Please forward me some document so that i can learn
Thanks
abhi_sharma861@yahoo.co.in
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.
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……..
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
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
Hi
You can preserve the array by using Redim Preserve arrname
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.
Hi All,
Plz tell me how “getElementsByTagName” method works? and where should we use this?
Pramod
any body can help me my application shouldnt accept duplication email ids…please send me code….my id is shakila.shetty@sysfore.com
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
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
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
Hi Ankur,
Do we need to know VBScript indepth to be a good at QTP
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.
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
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
Can you provide the examples for Static array and dynamic array..
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.
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.
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
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
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.
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.
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
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
Hi Ankur,
Superb job!
Can you please provide some material on VB scripting in detail with examples.
Thanks in advance.
email:arshnraj@gmail.com
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
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
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
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
@ Amar:
Please use Testing Tools forum for posting QTP related questions.
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)
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
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
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
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.
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
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
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…