How to use Loops in a test?


A loop is a sequence of instructions that is continually repeated until a certain condition is reached. Some scenarios where loops are used:

  • Iterating over a list of items appearing in a drop down to validate each on

  • iterating over a set of rows in an excel sheet to fill data in a form


The action keywords for looping introduced by BQ Tool are :

1- for

2- endFor

3- breakFor

4- forEach

5- endForEach

6- breakForEach

7- updateLoopCounter



Note : The data to be entered while using the looping keywords is entered in the Free Form Data Grid or an excel sheet.

To enter data in the Free Form Data Grid, please select 'Free Form - Data' as the data type in the 'value' field for the setValue keyword.



1- for loop : While using the for loop, you just need to enter the starting and ending index and a variable name in which the counter value will be stored.

2- endFor : If you are using the for loop, you will need to end the iteration at some point. Action keyword endFor is used for ending the iteration.

The basic test script would appear as :

3- breakFor : breakFor keyword will be used to break the iteration when a certain condition is reached.

The basic test script would appear as :

4- forEach : This keyword is used to perform a specific action for the comma separated values entered in the comma separated field of forEach keyword.

5- endForEach : If you are using the forEach loop, you will need to end the iteration at some point. Action keyword endForEach is used for ending the iteration.

The basic test script would appear as :

6- breakForEach : breakForEach keyword will be used to break the iteration when a certain condition is reached.

The basic test script would appear as :

7- updateLoopCounter : This keyword is used to update the counter value in a loop.

The basic test script would appear as :

   
For ex., the data in the Free Form Data Grid is as follows :

The normal for loop should execute all the values one by one, if the starting index is 1 and ending index is 8.

Now, let's update the counter value by 2 in the test script.

The data for first row will be executed normally and the counter value will become 1.

Now updateLoopCounter keyword will be executed and the counter value will be (1 + 2) = 3

So, the data for row 3 will be executed and the counter value wil become 4.

The updateLoopCounter keyword will be executed again and the counter value will be (4 + 2) = 6

This time, the data for row 6 will be executed and the counter value will become 7 and the loop is continued so on.