This document provides an overview of basic VBScript concepts used in QuickTest Professional (QTP). It discusses VBScript variables, arrays, functions, conditional and looping statements, date/time functions, built-in functions, and classes. VBScript is the scripting language used in QTP and is a lightweight version of Visual Basic. VBScript code is contained in .vbs files and variables can be explicitly or implicitly declared. Arrays allow storing multiple values in indexed elements. Functions and subroutines can perform actions and return values. Conditional statements like If/Then execute code based on conditions, while looping statements like For/Next repeat code. Built-in functions perform tasks like formatting dates and strings. Classes define custom data
2. VBSCRIPT IN QTP
Scripting language for QuickTest Professional
(QTP) is VBScript.
VBScript is a light version of Microsoft's
programming language Visual Basic.
VBScript source code is contained in stand-alone
files, they have the file extension .vbs.
3. AGENDA
1. VBScript Variable
2. VBScript Array Variable
3. VBScript Functions and Subroutines
4. VBScript Conditional Statements
5. VBScript Looping Statements
6. VBScript Date and Time Functions
7. VBScript Built-in Functions
8. VBScript Classes
4. 1. VBSCRIPT VARIABLE
In VBScript all variables are of the type variant that
can store any type of value.
Rules for VBScript variable name:
Must begin with a letter
Cannot contain period (.)
Cannot exceed 255 characters
It must be distinctive (unique) within the scope in
which it is declared.
E.x: Dim b
b = 100
Function TEST()
Dim a,b
a = 1
b = 2
MsgBox a + b
End Function
TEST
MsgBox b
5. 1. VBSCRIPT VARIABLE (CONT.)
Variables can be declared explicitly and implicitly.
Explicitly variables are declared with Dim
statement, Public statement, Private statement.
Dim Name
Dim Name, employee_address, city
Implicitly we can declare them within the script by
just using the variable name. But this practice is
prone to errors.
We can compel VBScript to require all variables to
be explicitly declared by including the statement
Option Explicit at the start of every script.
Variable can be declared as constants (Const).
6. 2. VBSCRIPT ARRAY VARIABLE
Every element of an array is associated with a unique
index number. By default, index number starts from 0.
The number of elements in an array is a fixed number. It
can also be re-adjusted dynamically.
VBScript supports up to 60 dimensions in an array.
Structure:
Dim variable_name()
ReDim [Preserve] variable_name(new_limit)
The first, we declare an array with no upper limit
The 2nd, with ReDim we reset the upper bound to a new
value. The optional key word "Preserve" states that all of
the old elements must be preserved when changing the
array size.
7. 2. VBSCRIPT ARRAY VARIABLE (CONT.)
Example:
Dim First_Array() 'dynamic array
ReDim First_Array(25) 'ReDim sets the initial size of the
dynamic array to 25
ReDim First_Array(2) 'We can resize a dynamic array unlimited
number of times
'Put data to Array
First_Array(0) = “1”
First_Array(1) = “2”
First_Array(2) = “3”
ReDim Preserve First_Array(5) 'Resize the array, but keep the
existing data
MsgBox First_Array(2) ‘=> MsgBox displays 3
Dim arr
arr = Array(5,10,15,20)
8. 2. VBSCRIPT ARRAY VARIABLE (CONT.)
Some of the Array keywords and their uses:
Keyword Function
Dim It will Declare an array
ReDim This is used to size or resize a dynamic array.
IsArray Will Return True if A is an array, False if it is not
LBound Will Return lower bound of an array, in VBScript it
always returns 0
UBound Will Return an upper bound of array
Preserve (Optional) is used to preserve the data in an
existing array when you resize it.
Erase Reinitializes the elements if it is a fixed-size array
and deallocates the memory used if it is a
dynamic array.
9. 3. VBSCRIPT FUNCTIONS AND SUBROUTINES
A Sub procedure:
is a series of statements, enclosed by the Sub and End Sub
statements
can perform actions, but does not return a value
can take arguments
A Function procedure:
is a series of statements, enclosed by the Function and End Function
statements
can perform actions and can return a value
can take arguments that are passed to it by a calling procedure
without arguments, must include an empty set of parentheses ()
returns a value by assigning a value to its name
10. 3. VBSCRIPT FUNCTIONS AND SUBROUTINES
(CONT.)
The main difference between a function and a
subroutine is that a subroutine will do some
processing of the code and then quit, while a
function processes some code and then returns the
result back.
Calling a Procedure:
• Call a Sub • Call a Funtion
11. 4. VBSCRIPT CONDITIONAL STATEMENTS
In VBScript we have 4 conditional statements:
If statement - executes a set of code when a condition is true
If...Then...Else statement - select one of two sets of lines to
execute
If...Then...ElseIf statement - select one of many sets of lines to
execute
Select Case statement - select one of many sets of lines to
execute
Example:
12. 5. VBSCRIPT LOOPING STATEMENTS
In VBScript we have four looping statements:
For...Next statement - runs code a specified number of
times
For Each...Next statement - runs code for each item in
a collection or each element of an array
Do...Loop statement - loops while or until a condition is
true
While...Wend statement – loops while a condition is
true. (recommend to do not use this statement, use the
Do…Loop instead)
Example:
13. 5. VBSCRIPT LOOPING STATEMENTS (CONT.)
1. For … Next
With the help of Step keyword, we can increase or decrease
the counter by the value specified.
You can exit a For...Next statement with the Exit For keyword.
14. 5. VBSCRIPT LOOPING STATEMENTS (CONT.)
2. For Each … Next
A For Each...Next loop repeats a block of code for each item
in a collection, or for each element of an array.
It is useful when we don’t know how many elements are
there in the dynamic array.
15. 5. VBSCRIPT LOOPING STATEMENTS (CONT.)
3. Do … Loop
It will repeat a block of code while a condition is True or until a
condition becomes True
You can exit a Do...Loop statement with the Exit Do keyword.
16. 5. VBSCRIPT LOOPING STATEMENTS (CONT.)
4. While … Wend
While Loop is a simple loop that keeps looping while a condition
is true
While...Wend loops can be nested to any level.
Each Wend matches the most recent While.
The Do...Loop statement provides a more structured and
flexible way to perform looping. Should use it instead.
17. 6. VBSCRIPT DATE AND TIME FUNCTIONS
Date()
Time()
Weekday()
WeekdayName()
18. 6. VBSCRIPT DATE AND TIME FUNCTIONS
(CONT.)
Month()
MonthName()
DateDiff()
19. 6. VBSCRIPT DATE AND TIME FUNCTIONS
(CONT.)
FormatDateTime()
IsDate()
23. 8. VBSCRIPT CLASSES
Above we have created a class (Hello_World) and an instance
(MyHello_World) of that class. VBScript uses the Class...End
Class statements to define the contents of the class. The
property (Location) and procedure (Say_Hello) are also declared
within the class.
Members within the class can be declared as private and public.
Private members are only visible within the class whereas public
members are accessible by any code outside of the class. Public
is default.
Procedures (Sub or Function) declared Public within the class
are methods of the class. Public variables serve as properties of
the class.