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 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.
1 2 3 4 5 6 7 8 9 10 11 |
iNumber = 10 counter = 0 Do While counter < iNumber 'The do loop will exit when the value of counter becomes 9 counter = counter + 1 Loop MsgBox counter |
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
Item | Description |
---|---|
counter | The variable is used as a counter for the number of iterations of the For Loop. |
start | The initial value of the counter. |
end | The last number of the counter at which the Loop should stop |
step | The 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. |
statement | A 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.
1 2 3 4 5 6 7 8 9 10 |
iNumber = 10 iTemp = 0 For counter = 1 To iNumber iTemp = iTemp + 1 Next MsgBox iTemp 'Will display 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
Item | Description |
---|---|
item | It is a variable that represents the element in the array. |
array | It is the name of the array. |
statement | A 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.
1 2 3 4 5 6 7 |
arrFruit = Array("Apple", "Orange", "Mango", "Banna") For Each fruit In arrFruit MsgBox fruit ' Displays current item of the array Next |
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
Item | Description |
---|---|
condition | A condition to be fulfilled to continue the loop. |
statement | A 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.
1 2 3 4 5 6 7 8 9 |
iCounter = 0 'initliaze the icounter variable to 0 While iCounter < 10 ' While Loop continues until value of iCounter is < 10 iCounter = iCounter + 2 'The value of iCounter is incremented by 2 on each iteration Wend MsgBox "The updated value of iCounter is: " & iCounter |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
iNumber = 10 counter = 0 Do While counter < iNumber 'The do loop will exit when the value of counter becomes 9 counter = counter + 1 If counter = 5 Then Exit Do End If Loop MsgBox counter ' Now the message box will display 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.
1 2 3 4 5 6 7 8 9 10 11 12 |
iNumber = 10 counter = 0 ' Do not run this code. It is an infinite loop ' Value of counter variable is not being increased inside the loop Do While counter < iNumber a = 1 Loop MsgBox "The value of Counter is: " & counter 'This statement will never execute |
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.
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.
1 2 3 4 5 6 7 8 9 |
iNumber = 10 For counter = 0 To iNumber a = 1 Next MsgBox "The value of Counter is: " & counter |
But here is a catch.What if someone resets the value of counter inside the For Loop.It will become an infinite loop.
1 2 3 4 5 6 7 8 9 |
iNumber = 10 For counter = 0 To iNumber a = 1 counter = 1 'counter is reset to 1 on each iteration Next MsgBox "The value of Counter is: " & counter |
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
- Import Excel File into Datatable in UFT
- How to Record a Script in Chrome Browser in UFT
- How to Use WaitProperty | Dynamic Wait in UFT
- How To Get Tooltip Text In UFT
- Understanding Object Spy in UFT
- UFT Reporter Object Methods that You Don’t Know
- Most Useful VBScript Functions That You Must Know
- Read and Update XML File in UFT | VBA