To use a list efficiently within a FOR
loop in Robot Framework, you can directly iterate over its elements or use the RANGE
keyword to access items by index. This allows for powerful automation tasks such as processing data, performing actions on multiple items, or validating sequences.
Understanding FOR
Loops with Lists in Robot Framework
Robot Framework provides a straightforward syntax for iterating over collections like lists. Whether you need to process each item individually or require its numerical position, FOR
loops are versatile tools.
1. Direct Iteration Over List Elements (Most Common)
The simplest and most Pythonic way to use a list in a FOR
loop is to iterate directly over its elements. This method is ideal when you only need the value of each item and not its index.
Syntax:
FOR ${item} IN @{your_list}
# Keywords to perform actions with ${item}
END
${item}
: A scalar variable that will hold the value of the current item during each iteration.IN
: The keyword separating the loop variable from the iterable.@{your_list}
: A list variable (identified by@
) that the loop will iterate through.
Example:
Let's say you have a list of web elements to click or a list of data to process.
***Settings***
Library Collections
***Test Cases***
Process My Shopping List
@{SHOPPING_ITEMS}= Create List Apples Bananas Milk Bread
Log To Console --- Starting to process shopping list ---
FOR ${item} IN @{SHOPPING_ITEMS}
Log To Console Purchasing: ${item}
# Imagine a keyword here like: Click Element id=${item}-button
END
Log To Console --- Finished shopping ---
In this example, the loop will execute four times, with ${item}
taking on the value "Apples", then "Bananas", then "Milk", and finally "Bread".
2. Iteration Using RANGE
(When Indices Are Needed)
Sometimes, you might need to access items by their numerical index or perform actions based on their position within the list. Robot Framework's RANGE
keyword, often used with the list's length, is perfect for this scenario. The RANGE
keyword generates a sequence of numbers from a starting point (defaulting to 0) up to, but not including, the end point.
Syntax:
FOR ${index} IN RANGE ${start} ${end} ${step}
# Keywords to perform actions using ${index} to get list item
END
${index}
: A scalar variable that will hold the current number generated byRANGE
.IN RANGE
: The keyword indicating that the loop will iterate through a range of numbers.${start}
,${end}
,${step}
: Optional arguments defining the range. For iterating through a list's indices, you'd typically use0
asstart
andGet Length
of the list asend
.
Example:
This approach is particularly useful if you need to perform an action using both the item's value and its index, or if you need to skip items based on their position.
***Settings***
Library Collections
***Test Cases***
Process Items By Index
@{PRODUCT_IDS}= Create List prod_001 prod_002 prod_003 prod_004
${list_length}= Get Length @{PRODUCT_IDS}
Log To Console --- Processing products by index ---
FOR ${index} IN RANGE ${list_length}
${product_id}= Get From List @{PRODUCT_IDS} ${index}
Log To Console Processing product at index ${index}: ${product_id}
# Imagine a keyword like: Verify Product Detail index=${index} id=${product_id}
END
Log To Console --- Finished processing by index ---
Here, the FOR
loop iterates through 0
, 1
, 2
, and 3
. In each iteration, ${index}
holds the current number, which is then used with Get From List
to retrieve the corresponding product ID from @{PRODUCT_IDS}
.
Choosing the Right Iteration Method
Method | Description | When to Use |
---|---|---|
Direct Iteration | Iterates directly over the elements of a list, assigning each element's value to a loop variable. | When you only need the value of each item. It's cleaner and more readable for most common scenarios. |
Iteration with RANGE |
Iterates through a sequence of numbers (indices). You then use these indices to retrieve elements from the list using keywords like Get From List . This is also how you can iterate if you only want to access specific items or skip items based on their index. |
When you need the numerical index of an item, want to skip items based on their position, or perform actions that require both value and index. |
Best Practices and Practical Insights
- List Creation: Use the
Create List
keyword from theCollections
library to define lists within your Robot Framework tests. - Variable Naming: Use descriptive names for your loop variables (e.g.,
${item}
,${product_name}
,${index}
) to improve readability. - Clarity over Complexity: For most tasks, direct iteration (
FOR ... IN ...
) is sufficient and more straightforward. Opt forIN RANGE
only when the index is genuinely required. - Logging: Use
Log To Console
orLog
to output information during loop execution, which is helpful for debugging and understanding the flow. - External Documentation: For more advanced list manipulations or other
FOR
loop variations, refer to the Robot Framework User Guide.
By effectively utilizing FOR
loops with lists, you can build robust and dynamic automation scripts that can handle varying data sets and complex workflows.