How to use for loops in Robot Framework

When building Software Robots (and in programming in general), it is a common pattern to have to repeat the same action multiple times based on a list of elements. For example, given a list of email addresses, send a message to each of them. Or, do some processing for each line of an invoice, etc. This is called for looping. Let's have a look at what you can do with loops in Robot Framework!

Simple loops

Consider this example:

*** Settings ***
Documentation     An example about for loops.

*** Variables ***
@{ROBOTS}=        Bender    Johnny5    Terminator    Robocop

*** Tasks ***
Loop Over A List Of Items And Log Each Of Them
    FOR    ${robot}    IN    @{ROBOTS}
        Log    ${robot}
    END

Let's go over it line by line:

*** Variables ***
@{ROBOTS}=        Bender    Johnny5    Terminator    Robocop

Here we are creating a list variable, and storing our robot names in it.

Note that list variables use the @{} syntax. You can learn more about variable types on the Robot Framework official documentation.

FOR    ${robot}    IN    @{ROBOTS}

Here we are starting our loop. We will loop over our @{ROBOTS} list variable, and we are defining a ${robot} local variable, which will be assigned at each iteration.

    Log    ${robot}

This is the operation we want to execute at each iteration. In our case, we just want to log the variable's value, so we use the Log keyword. Note the indentation: this and any other keywords we want to execute inside our loop will need to be indented with at least two spaces.

END   ${robot}

To signal that we want to close our for loop, we add the END keyword.

Our code will now print the name of each of the robots in sequence:

Log result of for loop

Controlling the execution of a for loop manually

A for loop will execute the same operation for all the elements of a list. Sometimes you might instead want to skip some element or to halt the loop immediately based on some condition. Let's see how.

Skipping one element

If you want to log the name of each robot in our list, but not Terminator, you can use the Continue For Loop If keyword like this:

*** Settings ***
Documentation     An example executing a loop but skipping one element.

*** Variables ***
@{ROBOTS}=        Bender    Johnny5    Terminator    Robocop

*** Tasks ***
Execute a loop only three times
    FOR    ${robot}    IN    @{ROBOTS}
        Continue For Loop If    $robot == 'Terminator'
        Log    ${robot}
    END

  • Continue For Loop If $robot == 'Terminator': Using this keyword, we can skip the current execution of the loop and continue to the next one if the condition is met.

The Continue For Loop keyword is also available: It will skip the current iteration of the loop in any case. This is mostly meant to be used inside another keyword.

Breaking out of the loop

If you want to stop the execution of the loop based on some condition and have your program continue onwards, you can use the Exit For Loop If keyword:

*** Settings ***
Documentation     An example breaking out of the loop based on some condition.

*** Variables ***
@{ROBOTS}=        Bender    Johnny5    Terminator    Robocop

*** Tasks ***
Break out of the loop on condition
    FOR    ${robot}    IN    @{ROBOTS}
        Exit For Loop If    $robot == 'Johnny5'
        Log    ${robot}
    END
  • Exit For Loop If $robot == 'Johnny5': Using this keyword, we can stop the execution of the loop and have our program continue after it.

The Exit For Loop keyword is also available: It will stop the execution of the loop in any case. This is mostly meant to be used inside another keyword.

Executing a loop a maximum amount of times

Using the Exit For Loop If keyword, you can decide to set the maximum amount of times a for loop should be executed:

*** Settings ***
Documentation     An example executing a loop only two times.

*** Variables ***
@{ROBOTS}=        Bender    Johnny5    Terminator    Robocop

*** Tasks ***
Execute a loop only two times
    ${index}=    Set Variable    1
    FOR    ${robot}    IN    @{ROBOTS}
        Exit For Loop If    ${index} > 2
        Log    ${robot}
        ${index}=    Evaluate    ${index} + 1
    END
  • ${index}= Set Variable 1: We create a variable called ${index} that we set to the value of 1.
  • ${index}= Evaluate ${index} + 1: We increment the value of the index variable by one inside the loop, using the Evaluate keyword.
  • Exit For Loop If ${index} > 2: We use the Exit For Loop If keyword to interrupt the execution of the loop if our index variable is bigger than two, effectively limiting the execution of the loop to two times maximum.

You can also use a "For-in-range loop" if you simply want to execute an operation a set amount of times, without iterating over a list.

Nested loops

You cannot directly create nested loops, but you can achieve the same result by calling a keyword inside the outer loop that itself contains a loop, for example, if you have a table which contains rows which in turn contain cells:

*** Keywords ***
Handle Table
    [Arguments]    @{table}
    FOR    ${row}    IN    @{table}
        Handle Row    @{row}
    END

*** Keywords ***
Handle Row
    [Arguments]    @{row}
    FOR    ${cell}    IN    @{row}
        Handle Cell    ${cell}
    END

Other practical examples and courses

Further reading

In this article, we have seen the most common use cases for loops in Robot Framework. If you want to learn more, you can refer to the official Robot Framework User Guide.