Using loops

Overview

The best tests are the ones that adapt to changes in your application. Your tests shouldn’t break because you’ve added a new feature to your app, nor should it fail to test a portion of your app because it never selects the newly added options in a dropdown. By setting up a set of steps that repeat based on real data in your application (such as the number of choices in a dropdown or count of a specific element on the page), you can more easily create complex tests that cover more of your application, are more robust, and are faster to create and maintain.

Use looping of steps within mabl to enable you to test more complex user journeys in your application - with no coding necessary.

Use cases

Test loops are particularly useful when:

  • Skipping six months ahead in a date-picker element
  • Asserting that all elements in a dropdown are shown
  • Cleaning up test data created earlier in the test or deployment
  • Browsing through a list of results until the correct item appears
  • Asserting that a constant element appears across multiple pages

Adding loops in the Trainer to existing flows

Loops can easily be added to any existing reusable flow just by editing it. Please note that the loop will only loop that instance of the flow, so if you use it again elsewhere you'll need to reconfigure looping. The same goes for removing looping.

592

On the next page, configure the number of times you'd like the flow to loop. Skip down to the "Specifying loops with a variable" section to learn more about using a variable loop amount.

Adding a new looping flow

Loops can be added in the mabl Trainer by following these steps:

  1. Click the "Add step" plus button in the toolbar at the bottom of the Trainer screen.
  2. Select the "Loops" step type. This will open the "Create flow" window.

📘

Have an existing set of steps you want to loop?

Hover over the step number and click to select multiple steps at once. Select a group of contiguous steps, e.g. 5, 6, 7, and 8, and click the "New flow" button that appears in the toolbar.

  1. Name your flow.
  2. Switch to the "Looping" tab or click "Runs 1 time" to start configuring looping.
592
  1. When configuring the loop, a specific number can be used to determine the loop iterations. A variable can also be selected by clicking the Loop using a variable option.
592
  1. Once the loop is configured, simply click "OK" to save your new looping flow.
  2. Any steps that should be repeated can be added between the Start flow and End flow steps.

🚧

Current limitations

  • The maximum number of loops allowed is 500.

Specifying loops with a variable

Sometimes the number of loops can only be determined dynamically. For example:

  • Check all the checkboxes but the number of checkboxes may be different each run
  • Click "refresh" until the expected data appears

These cases can be handled by creating a variable that stores the number of loops and selecting the "Loop using a variable" option.

592

When the number of loops is specified with a variable, the mabl runner checks to see if the number of loops executed has reached or exceeded the variable value prior to each iteration.

It is possible to extend or break out of looping by modifying the variable conditionally from inside the loop. In the example below, the loop count variable is initialized to 50. However, as soon as the number of checked checkboxes has reached 5, the variable is changed to 0, causing the loop to exit early. This is effectively a while loop.

837

Using the "run.loop_index" variable

A special read-only variable run.loop_index is automatically injected when looping. Its value is the the current loop number, which is a one-based ordinal index. This can be useful for targeting the nth instance of a type of elements in a CSS query or XPath. In the example below, we use the CSS query div[role='radiogroup'] label:nth-of-type({{@run.loop_index}}) input to select each of the radio buttons in order.

800

📘

Accessing run.loop_index in a JavaScript Step

If you want to access the run.loop_index value in a javascript step you can use mablInputs.variables.web.runtime.flow_run_ordinal_index

Replay control

When replaying loops within the mabl Trainer, mabl will try to resume playing the loop at the current index, even if the loop was stopped partway through. You could also select the loop to replay from using the loop dropdown.

600