Enhancing Keyword Tests using loops

Loops are used for the purpose of executing repetitive actions in a test. With the help of the operations related to the Statements group, we can add such actions to the Keyword Test.

In this recipe, we will take up an example of clicking on one and the same button for several consecutive times with the help of the For Loop.

Getting ready

First, let's record a simple script for further modification:

  1. Launch the Calculator Plus application: (C:Program Files (x86)Microsoft Calculator PlusCalcPlus.exe).
  2. Begin the recording (Test | Record | Record Keyword Test).
  3. Click on the 2 button in the calculator and stop the recording by clicking on the Stop button on the Recording toolbar.

How to do it...

For creation of the for loop, it is first necessary to create a counter-variable:

  1. Click on the Add Variable button on the Keyword Test toolbar:
    How to do it...
  2. In the open Add Variable to Keyword Test window, input the name of the variable (counter) and the type (Integer), and click on Finish.
  3. Now we will add the loop itself.
  4. In the list of Operations, opt for the Statements group and drag-and-drop the For Loop element onto the CalcPlus element:
    How to do it...
  5. In the For Loop window, input the loop parameters: Loop Variable by selecting the previously created variable (counter), Start Value (1), and End Value (5):
    How to do it...
  6. Click on the OK button.
  7. Now we need to place the ClickButton operation inside the loop.
  8. Select the CalcPlus element and press the following keys combination: Ctrl + right arrow key.
  9. In the result, the ClickButton operation will become a sibling element of the For Loop:
    How to do it...
  10. Now, if you would launch the test to be executed, the 2 button will be clicked down five times in a row.

How it works...

The For Loop in the Keyword Test is workable in the same manner as the for loop in any kind of programming. Specific actions, placed inside the loop, are executed for a pre-set amount of times. To assign a specific number of iterations, a loop variable is to be used (in our case, it is the counter variable). During each iteration the value of the variable is incremented by the value of Step (we have left it to be equal to 1 by default). When the value of the loop variable is greater than the End Value, the loop execution is terminated.

In a similar way the While Loop and If...Then condition can also be created.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.114.19