VBScript Loops: Do Loop, For Loop, For Each, and While Loop

In this article, I will show you how to use different types of Loops in UFT using VBScripting to iterate certain statements a specific number of times in order to optimize the script rather than copy-pasting the same statement many times.

VBScript loops in uft

VBScript Loops in UFT with Example

Do…Loop statement

The Do…Loop statement instructs UFT One to iterate a statement or series of statements while a condition is true or until a condition becomes true. The following is the syntax of Do…Loop :

Do [ {while} {until} condition ]
     statement 1
     …………….
    statement n
Loop

Eample

In the following example, I have taken two variables iNumber and counter. The  iNumber variable is initialized to 10 and the counter variable is initialized to 0. The value of the counter variable increments by 1 on each iteration and the do loop will continue to iterate until its value is less than 10. When the value of the counter becomes 10 the while condition becomes false and the do loop will exit.

For…Next Statement

A For…Next loop instructs UFT One to iterate one or more statements a specified number of times. The following is the syntax of For..Next loop.

For counter = start to end [Step step]
statement…
Next

ItemDescription
counterThe variable is used as a counter for the number of iterations of the For Loop.
startThe initial value of the counter.
endThe last number of the counter at which the Loop should stop
stepThe number to increment at the end of each loop.
Default = 1.
Optional.

This parameter should only be used if you want to increase the value of counter more by more than one (say 2 or 3, etc) on each iteration.
statementA statement, or series of statements, to be performed during the loop.

The following example shows how to use a For loop.The variable iNumber is initialized to 10 and iTemp is initialized to 0.The counter variable is set to 1 and the loop will continue to iterate until the value of the counter variable becomes equal to iNumber that is 10. The value of iTemp variable increments by 1 on each iteration. When the For loop terminates the value of iTemp is displayed in the message box and it will display value 10.

For…Each Statement

A For…Each loop works only with an array or an object collection. It instructs UFT One to perform one or more statements for each element in an array or an object collection. The syntax is as follows:

For Each item In array
statement
Next

ItemDescription
itemIt is a variable that represents the element in the array.
arrayIt is the name of the array.
statementA statement, or series of statements, to be performed during the loop.

Example

The following example uses a For…Each loop to display each item of the array one by one.

While…Wend Statement

A While…Wend statement instructs UFT One to iterate a statement or series of statements while a condition is true. The following is the syntax:

While condition
statement
Wend

ItemDescription
conditionA condition to be fulfilled to continue the loop.
statementA statement or series of statements to be executed during the loop.

Example

In the following example, the while loop will iterate only 5 times as the value of iCounter increments by 2 on each iteration.

How to Exit a Loop When a Specific Condition is Met

We’ve seen how to use the aforementioned loops in the UFT. When we use a loop to iterate a few statements in an automation script, we may need to exit or break the loop when a specific condition is met.This reduces execution time and improves script optimization.

Exit a Do Loop When a Certain Condition is Met

We can use Exit Do statement inside a loop to check when the required condition is met to exit from the loop.

Example: In the following example, the Do Loop will exit when the value of counter variable becomes 5.

Exit a For Loop When a Certain Condition is Met

We can use Exit For statement inside an IF statement to exit the loop when a certain condition is met.

Exit a While Loop When a Certain Condition is Met

There is no statement to exit a While loop like Exit For or Exit Do.Microsoft recommends us to use the Do loops as they are more structured, so we should prefer Do Loop over While Loop.

Infinite Loop

If you forget to increment the value of the counter when writing a loop, the loop will become infinite and never end.The following is an example of an infinite loop using Do Loop.

In this example, the counter is set to 0 but its value never incremented. Therefore the condition will never be met – the counter will always be less than 10 (iNumber).If you accidentally write an infinite loop in your automation script and run it the script will keep executing forever.So,I would recommend you to always run the piece of code in debug mode whenever you write a loop to ensure every thing is working fine.If the loop goes into the infinite mode you may effortlessly terminate the script with the aid of using the stop button.

How to Debug a Test in UFT

Advantages of Using For Loop

In the following example, the same Do Loop has been written in the form of For Loop. However, in this case, the loop will not become endless as the counter increases automatically. So it can be concluded that for loop is safer for counting as it automatically updates the counter in a loop.

But here is a catch.What if someone resets the value of counter inside the For Loop.It will become an infinite loop.

Conclusion

In this article, we have learned four different types of VBScript loops in UFT that can be used for looping certain statements in the Test Script.We have also learned how to exit a loop on a specific condition.If you have any queries please mention them in the comment box. If you find this article helpful please don’t forget to share it.

Recommended Posts

Leave a Reply