VBScript for Automation (QTP/UFT) Testing – Part 3
VBScript for Automation (QTP/UFT) Testing – Part 3
by
In VBScript – Part 3, let’s see the following topics:
Procedures
Functions
Sub Routine
Arrays
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:
1
2
3
Functionfunctionname
‘set of statements
Endfunction
Example:
1
2
3
4
5
6
7
8
9
10
Dima,b
a=5
b=2
temp=fnaddnum(a,b)
msgbox temp
functionfnaddnum(x,y)
result=x+y
msgbox result
fnaddnum=result
endfunction
Sub Routine:
Returns no value
Syntax:
1
2
3
sub subroutinename
‘set of statements
Endsub
Example:
1
2
3
4
5
6
7
8
Dimc,d
c=6
d=8
call subaddnum(c,d)
sub subaddnum(x,y)
res=x+y
msgbox res
endsub
Example:
1
2
3
4
5
6
7
8
Dimc,d
c=6
d=8
subaddnumc,d
sub subaddnum(x,y)
res=x+y
msgbox res
endsub
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:
1
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:
1
Dim arrayvalue(3)
It stores 4 values. The array always starts from 0.
Assigning values to the array:
1
2
3
4
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:
1
2
3
Dim arrayvalue(3)
Msgbox lbound(arrayvalue)
Msgbox ubound(arrayvalue)
Size of the Array:
1
2
Dim arrayvalue(3)
Msgbox“The arraysize is”&Ubound(arrayvalue)+1
Another way to assigning Array:
Syntax:
1
array(arglist)
Example:
1
2
3
4
5
6
7
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:
1
2
3
4
5
Dim arrayvalue(1)
arrayvalue(0)=10
arrayvalue(1)=20
msgbox arrayvalue(0)
msgbox arrayvalue(1)
MULTI DIMENSION ARRAY:
1
2
3
4
5
6
7
8
9
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
1
2
3
4
5
6
7
8
9
10
11
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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