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

vbscript - part 2

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:

Example:

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:

Example:

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

IF – ELSEIF STATEMENT: (More than two conditional)

SYNTAX:

Example:

SELECT CASE STATEMENT:

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

SYNTAX:

EXAMPLE:

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:

Example:

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:

Example:

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:

Example:

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:

Example:

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”

FOR LOOP:

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

INCREMENTAL:
SYNTAX:

[step 1] is not mandatory for one step increment

DECREMENTAL:
SYNTAX:

[step -1] is mandatory here

Example:

Example:

Example:

How to Exit a “FOR” Loop

Use the keyword EXIT to come out of the loop:

Syntax:

Example:

FOR EACH STATEMENT:

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

SYNTAX:

Example:

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

VBScript-Part2

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

1 thought on “VBScript for Automation (QTP/UFT) Testing – Part 2”

  1. Valuble information. ….

Leave a Comment

Share via
Copy link
Powered by Social Snap