Click to open - Sogeti Finland Nimenhuuto.com

VB SCRIPT BASICS TRAINING DOC
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.
VB SCRIPT basics covered in document
 VBScript Variable
 VBScript Array Variable
 VBScript Functions and Subroutines
 VBScript Conditional Statements
 VBScript Looping Statements
 VBScript Date and Time Functions
 VBScript Built-in Functions
 VBScript Classes
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.
Example
Dim b
b = 100
Function TEST()
Dim a,b
a=1
b=2
MsgBox a + b
End Function
TEST()





Variables can be declared explicitly and implicitly.
Explicitly variables are declared with Dim statement, Public statement, Private statement.
o Dim Name
o 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).
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.
Example:
o Dim First_Array() 'dynamic array
o ReDim First_Array(25) 'ReDim sets the initial size of the dynamic array to 25
o ReDim First_Array(2) 'We can resize a dynamic array unlimited number of times
o 'Put data to Array
First_Array(0) = “1”
First_Array(1) = “2”
First_Array(2) = “3”
o ReDim Preserve First_Array(5) 'Resize the array, but keep the existing data
MsgBox First_Array(2) ‘=> MsgBox displays 3
o Dim arr
arr = Array(5,10,15,20)
 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.
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
Example
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
Example

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 Function
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:
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:
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.
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.
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.
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.
VBScript Date and Time Functions

Date()  Returns the current system date

Time()  Returns the current system time


Weekday()  Returns a number that represents the day of the week (between 1 and 7, inclusive)
WeekdayName()  Returns the weekday name of a specified day of the week


Month()  Returns a number that represents the month of the year (between 1 and 12, inclusive)
MonthName()  Returns the name of a specified month

DateDiff()  Returns the number of intervals between two dates

FormatDateTime()  Returns an expression formatted as a date or time

IsDate()  Returns a Boolean value that indicates if the evaluated expression can be converted to a
date


Ucase()  Converts a specified string to uppercase
Lcase()  Converts a specified string to lowercase



Trim()  Removes spaces on both the left and the right side of a string
Ltrim() Removes spaces on the left side of a string
Rtrim() Removes spaces on the right side of a string

StrReverse()  Reverses a string

Round()  Rounds a number

Randomize()  Generates a Random number


Left()  Returns a specified number of characters from the left side of a string
Right()  Returns a specified number of characters from the right side of a string

Replace()  Replaces a specified part of a string with another string a specified number of times

Mid()  Returns a specified number of characters from a string

Split()  Returns a zero-based, one-dimensional array that contains a specified number of
substrings
VBScript Classes



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.