keepcalculator

06 Numbers and Powers

For Loops in Robot Framework: A Step-by-Step Guide

Card No. 06·49693 · Updated · Standard formula, shown below

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 FOR syntax).
  • 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

  1. Understand the FOR loop syntax. In Robot Framework, a FOR loop starts with FOR, followed by a variable name (or multiple names), the word IN, and the iterable (e.g., a list, a range, or a string). The loop body is indented, and the loop ends with END. For example:
        FOR    ${item}    IN    a    b    c
            Log    ${item}
        END
        
  2. 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}
            END
        

    Run the test and observe the log to see each fruit printed.

  3. 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}
            END
        

    Note the use of @{FRUITS} to expand the list.

  4. Use a range of numbers. To loop a specific number of times, use the IN RANGE syntax. The range can be from 0 to a number, or with start, stop, and step:
        FOR    ${i}    IN RANGE    5
            Log    ${i}
        END
        

    This logs numbers 0, 1, 2, 3, 4. For a custom start and step, use IN RANGE 1 10 2 which logs 1, 3, 5, 7, 9.

  5. Iterate over a string. You can loop over each character of a string by using IN with the string variable:
        ${text}    Set Variable    Hello
        FOR    ${char}    IN    ${text}
            Log    ${char}
        END
        

    This logs H, e, l, l, o.

  6. 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}
        END
        

    This logs ‘a and 1’ and ‘b and 2’.

  7. Control loop execution with EXIT FOR and CONTINUE. Use EXIT FOR to break out of the loop early, and CONTINUE to 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}
        END
        

    This logs 0,1,2,4,5,6 and then exits at 7.

  8. Nested FOR loops. You can place a FOR loop inside another FOR loop. Ensure each inner loop has its own END and proper indentation:
        FOR    ${i}    IN RANGE    3
            FOR    ${j}    IN RANGE    2
                Log    ${i}-${j}
            END
        END
        

    This logs combinations 0-0, 0-1, 1-0, 1-1, 2-0, 2-1.

  9. Use FOR with IN ENUMERATE to 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}
        END
        

    This logs 0: Alice, 1: Bob, 2: Charlie.

  10. Run a keyword multiple times with Repeat Keyword. If you just need to repeat a single keyword a fixed number of times, you can use Repeat Keyword instead of a FOR loop:
        Repeat Keyword    5    Log    Hello
        

    This logs ‘Hello’ five times.

Common Mistakes

  • Forgetting the END keyword. Every FOR loop must end with END. 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 RANGE and IN. IN RANGE is for numeric ranges, while IN is for iterating over items. Using IN RANGE with a list will not work as expected.
  • Using old :FOR syntax. In Robot Framework 4.0 and later, the old :FOR syntax is deprecated. Use the modern FOR syntax 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 ENUMERATE when 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 IF conditions 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 :FOR syntax.
  • 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 WHILE loop 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 the EXIT FOR is 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

The whole Numbers and Powers drawer →