VBScript for Automation (QTP/UFT) Testing – Part 3
In VBScript – Part 3, let’s see the following topics:
PROCEDURES:
It is a group/collection of statements which will group together and give the name.
There are 2 different types of procedures.
1. Function
2. Sub Routine
Function:
Returns a value
Syntax:
Function functionname ‘set of statements End function
Example:
Dim a,b a=5 b=2 temp=fnaddnum(a,b) msgbox temp function fnaddnum(x,y) result=x+y msgbox result fnaddnum=result end function
Sub Routine:
Returns no value
Syntax:
sub subroutinename ‘set of statements End sub
Example:
Dim c,d c=6 d=8 call subaddnum(c,d) sub subaddnum(x,y) res=x+y msgbox res end sub
Example:
Dim c,d c=6 d=8 subaddnum c,d sub subaddnum(x,y) res=x+y msgbox res end sub
Note: Parentheses is omitted in the above example, since CALL statement is not used.
ARRAY:
Array is a collection of data with different types of data type.
Syntax:
Dim ArrayName(size)
Where, “ArrayName” is the unique name for the array and “size” is a numeric value that indicates the number of elements in the array dimension within the array.
Example:
Dim arrayvalue(3)
It stores 4 values. The array always starts from 0.
Assigning values to the array:
arrayvalue (0) = 1 arrayvalue (1) = 2 arrayvalue (2) = 3 arrayvalue (3) = 4
There are two types of arrays: 1. Static Array, 2. Dynamic Array.
Static Array:
It has a specific number of elements. Once assigned, size of a static array can’t be modified at runtime.
Dynamic Array:
Size of a dynamic array can be modified at runtime.
Lower Bound & Upper Bound of Array:
Dim arrayvalue(3) Msgbox lbound(arrayvalue) Msgbox ubound(arrayvalue)
Size of the Array:
Dim arrayvalue(3) Msgbox “The array size is ”& Ubound(arrayvalue) + 1
Another way to assigning Array:
Syntax:
array(arglist)
Example:
Dim arrayvalue Arrayvalue = array(91,92,93,94) Msgbox Ubound(arrayvalue) Msgbox Arrayvalue(0) Msgbox Arrayvalue(1) Msgbox Arrayvalue(2) Msgbox Arrayvalue(3)
REDIM:
It recreates the array and erases all the old data
PRESERVE:
Preserve should be used along with redim. It will retain the old data.
SINGLE DIMENSION ARRAY:
Dim arrayvalue(1) arrayvalue(0)=10 arrayvalue(1)=20 msgbox arrayvalue(0) msgbox arrayvalue(1)
MULTI DIMENSION ARRAY:
Dim arrayvalue(1,1) arrayvalue(0,0)=10 arrayvalue(0,1)=20 arrayvalue(1,0)=30 arrayvalue(1,1)=40 msgbox arrayvalue(0,0) msgbox arrayvalue(0,1) msgbox arrayvalue(1,0) msgbox arrayvalue(1,1)
Some Examples:
Without Preserve
dim stm() redim stm(1) stm(0)=1 stm(1)=2 msgbox stm(0) msgbox stm(1) redim stm(2) ‘REDIM WITHOUT PRESERVE stm(2)=3 msgbox stm(0) msgbox stm(1) msgbox stm(2)
With Preserve
dim stm() redim stm(2) stm(0)=11 stm(1)=22 stm(2)=33 msgbox stm(0) msgbox stm(1) msgbox stm(2) redim preserve stm(3) ‘REDIM WITH PRESERVE stm(3)=44 msgbox stm(0) msgbox stm(1) msgbox stm(2) msgbox stm(3)
Redim Preserve with Multi-Dimensional Array
dim Stm() redim Stm(1,1) stm(0,0)=10 stm(0,1)=20 stm(1,0)=30 stm(1,1)=40 msgbox stm(0,0) msgbox stm(0,1) msgbox stm(1,0) msgbox stm(1,1) redim preserve stm(1,2) stm(0,2)=50 stm(1,2)=60 msgbox stm(0,0) msgbox stm(0,1) msgbox stm(1,0) msgbox stm(1,1) msgbox stm(0,2) msgbox stm(1,2)
I would like to conclude VBScript – Part 3 here and will start VBScript – Part 4 in the next post.
VBScript Series:
VBScript for Automation (QTP/UFT) Testing – Part 1
VBScript for Automation (QTP/UFT) Testing – Part 2
VBScript for Automation (QTP/UFT) Testing – Part 3
VBScript for Automation (QTP/UFT) Testing – Part 4
VBScript for Automation (QTP/UFT) Testing – Part 5