VBScript for Automation (QTP/UFT) Testing – Part 2

VBScript for Automation (QTP/UFT) Testing – Part 2

In VBScript – Part 2, let’s see the following topics:

Control Statements

  1. Conditional Statements
  2. Loop Statements

CONTROL STATEMENTS:

Statement: Each and every line in VBScript
1. Conditional control statements
2. Looping control statements

Firstly, let’s see conditional control statements

CONDITIONAL CONTROL STATEMENTS:

Used to execute single statements or set of statements based on conditions.

The following Conditional statements are available in VB Script:
IF STATEMENT: executes a set of statement when a condition is true
IF – ELSE STATEMENT: select one of two sets of statements to execute
IF – ELSEIF STATEMENT: select one of many sets of statements to execute
SELECT CASE STATEMENT: select one of many sets of statements to execute
Let’s see the syntax of each condition with an example below:

IF STATEMENT: (One conditional statement)

To execute only one statement when a condition is true:

SYNTAX:
1
2
3
IF THEN
set of statements
END IF
Example:
1
2
3
4
5
Dim a,b
a=8 : b=6
if a>b then
msgbox a is greater than b
End if

There is no “Else” in the above example. It performs only one action when a condition is true

IF – ELSE STATEMENT: (Two conditional statement)

SYNTAX:
1
2
3
4
5
IF THEN
set of statements
ELSE
set of statements
END IF
Example:
1
2
3
4
5
6
7
Dim a,b
a=3 : b=6
if a>b then
msgbox a is greater than b
Else
Msgbox Stay tuned to Software Testing Material
End if

It is to execute more than one statement when a condition is true.

IF – ELSEIF STATEMENT: (More than two conditional)

SYNTAX:
1
2
3
4
5
6
7
IF THEN
set of statements
ELSEIF Then
set of statements
ELSE
set of statements
END IF
Example:
1
2
3
4
5
6
7
8
9
Dim a,b
a=8 : b=6
if a>b then
msgbox a is greater than b
Elseif a=b then
Msgbox a is equals to b
Else
Msgbox a is not equals to b
End if

SELECT CASE STATEMENT:

It’s an alternative to IF-THEN-ELSE. It makes code more efficient and readable.

SYNTAX:
1
2
3
4
5
6
7
8
SELECT CASE EXPRESSION
CASE EXPRESSION1
set of statements
CASE EXPRESSION2
set of statements
CASE ELSE
set of statements
END SELECT
EXAMPLE:
1
2
3
4
5
6
7
8
9
10
11
InputValue = Inputbox(Enter the value: red or green or yellow)
Select case lcase(InputValue)
Case red
Msgbox stop
Case green
Msgbox go
Case yellow
Msgbox wait
Case else
Msgbox Invalid
End Select

NOTE:
In select case, the subdata type of “main expression” and “sub expression” should be same “same data type”
Data will always compare with equal relationship operator
Here the expressions are case sensitive. Eg: “Red” is not equal to “red”
In the above example, we used string function (lcase) to convert the input value to lowercase

LOOPING CONTROL STATEMENTS:

Looping control statements allow to run a group of statements repeatedly when the condition is true.
Following looping control statements are available in VBScript:
While…Wend statement – It executes only when a condition is true. Use the Do-Loop statement instead of this.
Do – Loop While statement – loops  while a condition is true
Do While – Loop statement – It executes one time even if the condition is False
Do – Loop Until statement – loops  until a condition is true
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

SIMPLE WHILE STATEMENT: (Pre-conditional looping statement)

It executes only when a condition is True.

SYNTAX:
1
2
3
WHILE
set of statements
WEND
Example:
1
2
3
4
5
6
Dim a
a=0
while a<=10
msgbox a
a=a+1
wend

DO-WHILE STATEMENT: (Post-conditional looping statement)

It gets execute at least one time even if the condition is False because the condition in the while statement gets checked at the end of the first iteration.

SYNTAX:
1
2
3
DO
set of statements
LOOP WHILE

Example:

1
2
3
4
5
6
7
Dim a
a=11
do
msgbox a
a=a+1
loop while a<=10
msgbox “end of script”

DO-WHILE STATEMENT: (PRE CONDITIONAL DO-WHILE)

It gets executed the set of statements only when the condition in the while statement holds true.

SYNTAX:
1
2
3
DO WHILE
set of statements
LOOP

Example:

1
2
3
4
5
Dim a
a=11
do while a<=10
msgbox a
loop

change the value of a and try how it works.

Incase if the program is keep on running. You could end the task by navigating the following path:
Task Manager – Processes – Apps – Microsoft Windows Based Script Host

DO-UNTIL STATEMENT:

It gets executed until the condition becomes true.

SYNTAX:
1
2
3
DO
set of statements
LOOP UNTIL
Example:
1
2
3
4
5
6
7
Dim a
a=0
do
msgbox a
a=a+1
loop until a>10
msgbox End of script

How to Exit a “DO” Loop
Use the keyword EXIT to come out of the loop:
In the above example, the code executes until the value of “a” is greater than “10”. Let’s see how to exit from the loop when the value of a reaches “5”

1
2
3
4
5
6
7
8
9
10
Dim a
a=0
do
msgbox a
a=a+1
if a = 5 then
exit do
end if
loop until a>10
msgbox “End of script”

FOR LOOP:

There are two types of for loops: 1. Incremental and 2. Decrement

INCREMENTAL:
SYNTAX:
1
2
3
For i=0 to n [step 1]
set of statements
next

[step 1] is not mandatory for one step increment

DECREMENTAL:
SYNTAX:
1
2
3
For i=n to 0 [step 1]
set of statements
next

[step -1] is mandatory here

Example:
1
2
3
4
5
6
Option explicit
Dim i
For i=0 to 10
Msgbox i
Next
Msgbox “end of script”
Example:
1
2
3
4
5
6
Option explicit
Dim i
For i=0 to 10 step 2
Msgbox i
Next
Msgbox “end of script”
Example:
1
2
3
4
5
6
Option explicit
Dim i
For i=10 to 0 step 1
Msgbox i
Next
Msgbox “end of script”

How to Exit a “FOR” Loop

Use the keyword EXIT to come out of the loop:

Syntax:
1
2
3
4
5
6
For i=0 to n [step 1]
set of statements
if i=5 Then
exit for
End if
next
Example:
1
2
3
4
5
6
7
8
9
option explicit
Dim i
For i=1 to 10 step 1
If i=5 Then
Exit for
End if
Msgbox i
Next
Msgbox end of script

FOR EACH STATEMENT:

repeats a block of code for each element of an array.

SYNTAX:
1
2
3
FOR EACH <> IN <>
set of statements
NEXT
Example:
1
2
3
4
5
6
7
Dim site(2)
site(0)=“Software”
site(1)=“Testing”
site(2)=“Material”
FOR EACH sitepart IN site
msgbox(sitepart)
Next

I would like to conclude VBScript – Part 2 here and will start  VBScript – Part 3  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

Back To Top