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 for loops in Robot Framework!
In addition to having nice Robot Framework documentation, such as how to use if-else or conditional execution in Robot Framework we provide tools that make Robot Framework development easier! Check out our VS Code extensions, the RCC command-line tool for robot development, or Robocorp Lab - our robot building IDE.
Simple for 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 for 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 for 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:
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 for 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 for loop but skipping one element.
*** Variables ***
@{ROBOTS}= Bender Johnny5 Terminator Robocop
*** Tasks ***
Execute a for 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 for 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 for loop in any case. This is mostly meant to be used inside another keyword.
Breaking out of the for loop
If you want to stop the execution of the for 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 for loop based on some condition.
*** Variables ***
@{ROBOTS}= Bender Johnny5 Terminator Robocop
*** Tasks ***
Break out of the for 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 for loop and have our program continue after it.
The
Exit For Loop
keyword is also available: It will stop the execution of the for loop in any case. This is mostly meant to be used inside another keyword.
Executing a for 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 for loop only two times.
*** Variables ***
@{ROBOTS}= Bender Johnny5 Terminator Robocop
*** Tasks ***
Execute a for 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 of1
.${index}= Evaluate ${index} + 1
: We increment the value of the index variable by one inside the for loop, using theEvaluate
keyword.Exit For Loop If ${index} > 2
: We use theExit For Loop If
keyword to interrupt the execution of the for loop if our index variable is bigger than two, effectively limiting the execution of the for 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 for loops
You cannot directly create nested for loops, but you can achieve the same result by calling a keyword inside the outer for loop that itself contains a for 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
- In our Beginners' course we use looping to fill a form repeatedly using data from an Excel file.
- The RPA form challenge has a similar use case filling forms.
- The Web store order robot uses looping to place a list of orders in a webshop.
- The PDF printer robot creates PDF files starting from a list of attendees for an event using a for loop.
- The Web scraper robot iterates over the last tweets for a given user with a loop.
- Sometimes a while loop can be more suitable than a for loop.
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.