Note: You are looking at a static snapshot of documentation related to Robot Framework automations. The most recent documentation is at https://robocorp.com/docs

While loops in Robot Framework

A common need in programming is repeating one or more operations until a condition is met. In Robot Framework and Python, this can be done with a while loop.

The native WHILE loop is supported starting from Robot Framework version 5.

The structure of the while loop

The WHILE construct starts with WHILE and ends with END. WHILE is followed by a Python statement. The loop is executed as long as the statement evaluates to boolean True. The body of the loop contains the lines to be executed for each loop iteration.

This is an example of a simple while loop in Robot Framework:

*** Tasks *** WHILE: A simple while loop WHILE True Log Executed until the default loop limit (10000) is hit. END

The while loop has a default limit of 10000 iterations to avoid accidental infinite loops. The above example throws an exception when it hits the default limit (10000).

This is effectively the same loop as described above:

*** Tasks *** WHILE: A simple while loop using the default loop limit WHILE True limit=10000 Log Executed until the default loop limit (10000) is hit. END

Here's an example where the loop limit exception is catched and handled:

*** Tasks *** WHILE: Loop while the default limit (10000) is hit TRY WHILE True Log Executed until the default loop limit (10000) is hit. END EXCEPT WHILE loop was aborted type=start Log The loop did not finish within the limit. END

You can override the loop limit using the limit argument:

*** Tasks *** WHILE: Loop while the given limit is hit TRY WHILE True limit=10 Log Executed until the given loop limit (10) is hit. END EXCEPT WHILE loop was aborted type=start Log The loop did not finish within the limit. END

If you really want to loop infinitely, you can achieve that by setting the limit argument to NONE.

*** Tasks *** WHILE: The infamous infinite loop WHILE True limit=NONE Log Oh no. This is an infinite loop! 😱 Log Do not try this at home. END

Wikipedia - Infinite loop: "In computer programming, an infinite loop (or endless loop) is a sequence of instructions that, as written, will continue endlessly, unless an external intervention occurs ("pull the plug"). It may be intentional."

Loop while condition evaluates to True

The following Robot Framework example demonstrates a while loop that executes as long as the value of the ${x} variable is less than three. The variable value is incremented at the end of each iteration:

*** Tasks *** WHILE: Loop while condition evaluates to True or the default loop limit is hit ${x}= Set Variable ${0} WHILE ${x} < 3 Log Executed as long as the condition is True. ${x}= Evaluate ${x} + 1 END

Skip a while loop iteration with CONTINUE

CONTINUE can be used to skip a loop iteration. The loop will continue with the next iteration:

*** Tasks *** WHILE: Skip a loop iteration with CONTINUE ${x}= Set Variable ${0} WHILE ${x} < 3 ${x}= Evaluate ${x} + 1 IF ${x} == 2 CONTINUE # Skip this iteration. Log x = ${x} # x = 1, x = 3 END

Exit while loop with BREAK

BREAK can be used to break out of the loop. It will effectively end the loop:

*** Tasks *** WHILE: Exit loop with BREAK WHILE True BREAK Log This will not be logged. END

Simulating a while loop

Robot Framework version 4 does not support while loops directly. However, there are ways to achieve very similar results.

Setting a for loop to repeat a high number of times

Using the FOR .. IN RANGE syntax, we can set a for loop to repeat for a very high number of times. Then, using the Exit For Loop If keyword, we can specify the condition that we want to "break" the execution.

In this simple example, our loop will continue until a random value equals to "5":

*** Settings *** Documentation Demonstrating a while-like loop. Library String *** Tasks *** For Test FOR ${i} IN RANGE 9999999 ${random_value_from_1_to_5}= Generate Random String 1 12345 ${random_value_is_5}= Evaluate ${random_value_from_1_to_5} == 5 Exit For Loop If ${random_value_is_5} END Log Exited the loop.

Using the native Python while syntax in your custom library

Another possibility, depending on your use case, could be to create a custom library using Python, and use the native Python while loop in you keywords there.

You can then use your keywords in your Robot Framework script, calling them with the Wait Until Keyword Succeeds keyword.

Last edit: May 5, 2022