While loops in Robot Framework
A common need in programming is repeating one or more operations until a condition is met. This is a while
loop.
Robot Framework, at least for now, 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.