How do I use Loops in ScenarioBuilder?

Add loops to your scripts to complete repetitive steps a set number for times. Loop actions can be used alone, or in combination with other script actions, such as components. This article reviews various scenarios for using loops in your scripts.

Scenario 1 - Group Actions

Sometimes when you are creating scripts it isn’t readily apparent that loops should be used for repetitive tasks. But that’s OK because ScenarioBuilder has us covered with the Group actions function.

Here is a script that was created to add a new patient to the application.

Script without loop

Looking it over, you can see there are some repetitive function key steps where tab is pressed several times in a row. We can manually add a loop step before the Function Keys steps and configure it to loop through and press the tab key 3 times. However, ScenarioBuilder can do this for us automatically. All we need to do is click Group Actions found in the Edit section of the ribbon.

The application will look for these repetitive steps and automatically convert them to loop statements.

Script with loop

Now we see that the three steps that pressed the tab key have been converted to a loop action. The loop action has a single step of pressing the tab key, but the loop is executed 3 times.

Scenario 2 - Basic Steps to Add a Loop

To add a loop to your script, drag Loop from Control Actions. Add the steps as children of the loop. All children steps will be executed the number of specified iterations. For the Loop step, set the Iterations property for the number of times the script should execute the child steps.

Scenario 3 – Looping Using a Variable

By default, when you add the Loop action it defaults to 5 iterations. You can change the value to a different number assuming you know how many times you want the loop to iterate. Sometimes, however, the number of times something needs to loop can vary. In that case, we can use a variable to set the number of iterations.

The variable might represent a random number, a value from a csv file, or some other calculated variable. To use a variable instead of a number you can enter it into the Iterations property directly (i.e. %numits%) or right-click to access a shortcut menu that provides access to the variables that exist for the script.

Scenario 4 – Looking for Text in a Long Drop-Down List

In my application I have a drop-down list that contains many items. To view all the items, the user must scroll through the list. I need to emulate that behavior in my script. The number of items that can show up in the list varies, so I can’t be sure how many times I might need to scroll down to find the item I want to select. To handle this, I’ll add a Loop statement that looks for the text and if it doesn’t find it, it scrolls down. To begin, my scripts would look like this:

Script 1

If the script stayed like this, here is what would happen:

It would open the drop-down list, look for the text and click on it if it finds it, then click the mouse in the scroll bar to scroll down. It would then repeat looking for the text and scrolling down 4 more times whether it found the text in an iteration or not.

If it finds the text in the first loop, then it doesn’t need to do any more loops. Let’s edit the script so if it finds the text it stops looping. We’ll do this by adding an Exit step from Control Actions. The script now looks like this:

Script 2

Now if it finds the text it will drop out of the loop and continue with any remaining steps after the loop. We still have a possibility of it looping too many times. We don’t know how many times we need to scroll to reach the end of the drop-down list since it can vary. We need to set our iterations high enough to cover all possibilities, but we then need a way to exit out of the loop when it reaches the end of the drop-down list entries. This can be done by looking for the scroll box to be at the bottom of the scroll bar and it if it, then we exit. However, if it reaches the end of the drop-down and doesn’t find the text, then it needs to stop executing the script and report a failure. The script now looks like this:

Script 3

Now our loop will look for the text. If it finds it, then it will exit the loop and continue to the next step in the script outside of the loop. If it doesn’t find it, then it will look to see if the scroll box is at the bottom of the scroll bar. It if it is, then it will exit the scenario with a failure since it didn't find the text anywhere in the drop-down list. If it hasn’t reached the bottom of the scroll bar yet, then it will click in the scroll bar to scroll down and loop from the top again.