This guide explains how to use FOR loops in Robot Framework to repeat actions over lists, ranges, or other iterable data. You’ll learn the syntax, how to iterate over different data types, and how to control loop execution. By the end, you’ll be able to write more efficient and maintainable test cases.
What You’ll Need
- A working installation of Robot Framework (version 4.0 or later for the modern
FORsyntax). - Basic familiarity with Robot Framework syntax, such as test cases, keywords, and variables.
- A text editor or IDE to write your test files.
Step-by-Step
- Understand the FOR loop syntax. In Robot Framework, a FOR loop starts with
FOR, followed by a variable name (or multiple names), the wordIN, and the iterable (e.g., a list, a range, or a string). The loop body is indented, and the loop ends withEND. For example:FOR ${item} IN a b c Log ${item} END - Create a simple test case with a FOR loop. Open a new Robot Framework test file and write a test case that uses a FOR loop to iterate over a list of values. For instance:
*** Test Cases *** Loop Over List FOR ${fruit} IN apple banana cherry Log ${fruit} ENDRun the test and observe the log to see each fruit printed.
- Iterate over a list variable. Define a list variable in the
*** Variables ***section and use it in the FOR loop:*** Variables *** @{FRUITS} apple banana cherry *** Test Cases *** Loop Over List Variable FOR ${fruit} IN @{FRUITS} Log ${fruit} ENDNote the use of
@{FRUITS}to expand the list. - Use a range of numbers. To loop a specific number of times, use the
IN RANGEsyntax. The range can be from 0 to a number, or with start, stop, and step:FOR ${i} IN RANGE 5 Log ${i} ENDThis logs numbers 0, 1, 2, 3, 4. For a custom start and step, use
IN RANGE 1 10 2which logs 1, 3, 5, 7, 9. - Iterate over a string. You can loop over each character of a string by using
INwith the string variable:${text} Set Variable Hello FOR ${char} IN ${text} Log ${char} ENDThis logs H, e, l, l, o.
- Use multiple loop variables. When iterating over a list of lists (or parallel lists), you can use multiple variables. For example, to iterate over a list of tuples:
@{PAIRS} (a, 1) (b, 2) FOR ${letter} ${number} IN @{PAIRS} Log ${letter} and ${number} ENDThis logs ‘a and 1’ and ‘b and 2’.
- Control loop execution with
EXIT FORandCONTINUE. UseEXIT FORto break out of the loop early, andCONTINUEto skip the rest of the current iteration. For example:FOR ${i} IN RANGE 10 IF ${i} == 3 CONTINUE IF ${i} == 7 EXIT FOR Log ${i} ENDThis logs 0,1,2,4,5,6 and then exits at 7.
- Nested FOR loops. You can place a FOR loop inside another FOR loop. Ensure each inner loop has its own
ENDand proper indentation:FOR ${i} IN RANGE 3 FOR ${j} IN RANGE 2 Log ${i}-${j} END ENDThis logs combinations 0-0, 0-1, 1-0, 1-1, 2-0, 2-1.
- Use
FORwithIN ENUMERATEto get index and value. This syntax is available in Robot Framework 4.0 and later:@{NAMES} Alice Bob Charlie FOR ${index} ${name} IN ENUMERATE @{NAMES} Log ${index}: ${name} ENDThis logs 0: Alice, 1: Bob, 2: Charlie.
- Run a keyword multiple times with
Repeat Keyword. If you just need to repeat a single keyword a fixed number of times, you can useRepeat Keywordinstead of a FOR loop:Repeat Keyword 5 Log HelloThis logs ‘Hello’ five times.
Common Mistakes
- Forgetting the
ENDkeyword. Every FOR loop must end withEND. Missing it causes a syntax error. - Incorrect indentation. The loop body must be indented (typically 4 spaces) relative to the FOR line. Inconsistent indentation leads to errors.
- Using
${list}instead of@{list}in the IN part. When iterating over a list variable, you must use the list variable syntax@{LIST}to expand it into items. Using${LIST}treats it as a single string. - Mixing up
IN RANGEandIN.IN RANGEis for numeric ranges, whileINis for iterating over items. UsingIN RANGEwith a list will not work as expected. - Using old
:FORsyntax. In Robot Framework 4.0 and later, the old:FORsyntax is deprecated. Use the modernFORsyntax without the colon.
Tips
- Use
@{EMPTY}for an empty list. If you need to loop over an empty list, use@{EMPTY}to avoid errors. - Consider performance. For large loops, avoid logging too much information, as it can slow down execution and clutter logs.
- Use
IN ENUMERATEwhen you need both index and value. It’s cleaner than manually maintaining an index variable. - Test your loop with a small dataset first. This helps catch errors early.
- Combine FOR loops with
IFconditions for complex logic. This allows you to filter or transform data within the loop.
Troubleshooting
- Syntax error: ‘FOR’ is not recognized. Ensure you are using Robot Framework 4.0 or later. If you have an older version, upgrade or use the deprecated
:FORsyntax. - Variable not found in loop. Check that the loop variable is correctly spelled and that you are using the correct syntax (e.g.,
${item}). Also ensure the variable is defined before the loop if it’s not a loop variable. - Loop runs infinitely. This usually happens if you have a
WHILEloop instead of a FOR loop, or if you modify the iterable inside the loop in an unexpected way. For FOR loops, the iteration count is fixed at the start. - Unexpected behavior with
EXIT FOR. Make sure theEXIT FORis inside the loop body and not in a separate keyword that is called from the loop. If it’s in a keyword, the keyword must return a value that triggers the exit. - List variable not expanded. When using a list variable in the IN part, always use
@{LIST}. If you use${LIST}, it will be treated as a single string.
Frequently Asked Questions
Can I use a FOR loop in a user-defined keyword?
Yes, you can use FOR loops inside keywords. Just place the loop in the keyword body, following the same syntax rules.
How do I loop over a dictionary?
You can use FOR ${key} ${value} IN &{DICT} to iterate over key-value pairs. Alternatively, use Get Dictionary Keys or Get Dictionary Values to get lists to iterate over.
What is the difference between FOR and WHILE?
FOR loops iterate over a known set of items or a range, while WHILE loops continue until a condition becomes false. Use FOR when you know the number of iterations in advance.
Can I break out of a nested loop?
Yes, you can use EXIT FOR in any loop, including nested ones. It will exit the innermost loop containing that statement.
How do I loop over a list of lists?
You can use multiple loop variables in the FOR statement, as shown in Step 6. Each inner list is unpacked into the variables.
Is there a way to loop with a step in IN RANGE?
Yes, IN RANGE accepts three arguments: start, stop, and step. For example, IN RANGE 1 10 2 iterates over 1, 3, 5, 7, 9.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Turn Off Backup Encryption in iTunes?
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It