Conditional Statements VBScript | Automation Testing QTP/UFT

Conditional Statements VBScript | Automation Testing QTP/UFT

Conditional Statements VBScript:

Control statements are subdivided in to two types.

  1. Conditional Control Statements
  2. Looping Control Statements

Here we are going to see Conditional Statements VBScript. In the next post we see about Looping Statements.

CONDITIONAL STATEMENTS VBScript:

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

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

ghtechproject.net