Loops in Robot Framework
| Introduction | |
| FOR | |
| Iteration over list | |
| Exit Loop | |
| WHILE | |
| Related Articles |
Introduction
In this article, you can explore the different ways to use loops in RobotFramework.
FOR
Standard loop for iterating over integers
*** Settings *** Documentation An example of the for loop *** Test Cases *** Numeric For FOR ${i} IN RANGE 51 Log To Console ${i} END
robot loop.robot
============================================================================== Loop :: An example of the for loop ============================================================================== Numeric For .0 1 2 … 49 50 Numeric For | PASS | ------------------------------------------------------------------------------ Iloop :: An example of the for loop | PASS | 1 task, 1 passed, 0 failed ============================================================================== Output: /home/andrei/src/tests/robot/src/tests/output.xml Log: /home/andrei/src/tests/robot/src/tests/log.html Report: /home/andrei/src/tests/robot/src/tests/report.html
Using IN RANGE you can specify the start and end value of the interval
FOR ${i} IN RANGE 50 59
Iteration over List
Let's look at an example of sorting through bicycle brands from list : Stels , Forward , Author , Trek
*** Settings *** Documentation Regular loop over list *** Variables *** @{BIKES}= Stels Forward Author Trek *** Tasks *** Break out of the for loop on condition FOR ${bike} IN @{BIKES} Log To Console \n${bike} END
robot loop.robot
============================================================================== Loop :: Regular loop over list. ============================================================================== Regular loop over list Stels Forward Author Trek ------------------------------------------------------------------------------ Loop :: Regular loop over list | PASS | 1 task, 1 passed, 0 failed ============================================================================== Output: /home/andrei/src/tests/robot/src/tests/output.xml Log: /home/andrei/src/tests/robot/src/tests/log.html Report: /home/andrei/src/tests/robot/src/tests/report.html
Exiting Loop
Let's consider the same example, but with an additional condition for exiting the loop.
*** Settings *** Documentation An example breaking out of the for loop based on some condition. *** Variables *** @{BIKES}= Stels Forward Author Trek *** Tasks *** Break out of the for loop on condition FOR ${bike} IN @{BIKES} Exit For Loop If $bike == 'Author' Log To Console \n${bike} END
robot loop.robot
============================================================================== Loop :: An example breaking out of the for loop based on some condition. ============================================================================== Break out of the for loop on condition Stels Forward Break out of the for loop on condition | PASS | ------------------------------------------------------------------------------ Loop :: An example breaking out of the for loop based on some cond... | PASS | 1 task, 1 passed, 0 failed ============================================================================== Output: /home/andrei/src/tests/robot/src/tests/output.xml Log: /home/andrei/src/tests/robot/src/tests/log.html Report: /home/andrei/src/tests/robot/src/tests/report.html
WHILE
Starting with the fifth version of the Robot Framework, the WHILE loop is available out of the box
*** Settings *** Documentation An example of while loop *** Tasks *** WHILE: zero to fifty ${x}= Set Variable ${0} WHILE ${x} < 51 Log To Console ${x} ${x}= Evaluate ${x} + 1 END
robot loop.robot
============================================================================== Iloop :: An example of while loop ============================================================================== WHILE: zero to fifty .0 1 2 … 49 50 WHILE: zero to fifty | PASS | ------------------------------------------------------------------------------ Iloop :: An example of while loop | PASS | 1 task, 1 passed, 0 failed ============================================================================== Output: /home/andrei/src/tests/robot/src/tests/output.xml Log: /home/andrei/src/tests/robot/src/tests/log.html Report: /home/andrei/src/tests/robot/src/tests/report.html
Example with string concatenation
*** Settings *** Documentation An example of while loop *** Tasks *** WHILE: zero to fifty ${x}= Set Variable ${0} WHILE ${x} < 51 Log To Console ${x} ${name}= Catenate name${x} Log To Console ${name} ${x}= Evaluate ${x} + 1 END
Loop with condition inside
Inside the loop, you can use
conditional operators
For example, let's highlight even numbers
*** Settings *** Documentation Searching for even values *** Tasks *** Look for even ${i}= Set Variable 0 FOR ${i} IN RANGE 7 Log To Console ${i} IF ${i % 2} == 0 Log To Console "i is even" Sleep 1 END END
robot ex.robot
============================================================================== Ex :: Searching for even values ============================================================================== Look for even .0 "i is even" 1 2 "i is even" 3 4 "i is even" 5 6 "i is even" Look for even | PASS | ------------------------------------------------------------------------------ Ex :: Searching for even values | PASS | 1 task, 1 passed, 0 failed ============================================================================== Output: /home/andrei/robot/output.xml Log: /home/andrei/robot/log.html Report: /home/andrei/robot/report.html