The “continue for loop” statement is used in a loop, such as a for loop, to skip the current iteration and move on to the next one. When the “continue for loop” statement is encountered, the current iteration is skipped and the loop moves on to the next iteration, bypassing any remaining statements in the current iteration. This can be useful for skipping specific iterations or for controlling the flow of the loop based on certain conditions. The “continue for loop” statement should be placed within the loop’s body, and its use can greatly simplify the loop’s code by allowing certain iterations to be skipped based on specific conditions.
***settings***
Documentation Skips the current FOR loop iteration and continues from the next
*** Variables ***
*** Keywords ***
*** Test Cases ***
continue for loop
${list} = Create List 10 20 30
FOR ${I} IN @{list}
IF ${I} == ${20}
log to console ${i}
log to console Going for next iteration
continue for loop
log to console after continue for loop, this statement will not execute
END
END
tech-bloggers@tech-bloggers:~/robot_example$ robot continue_for_loop.robot
==============================================================================
Continue For Loop :: Skips the current FOR loop iteration and continues fro...
==============================================================================
continue for loop .20
Going for next iteration
continue for loop | PASS |
------------------------------------------------------------------------------
Continue For Loop :: Skips the current FOR loop iteration and cont... | PASS |
1 test, 1 passed, 0 failed
==============================================================================
Output: /home/tech-bloggers/robot_example/output.xml
Log: /home/tech-bloggers/robot_example/log.html
Report: /home/tech-bloggers/robot_example/report.html
In Robot Framework, the continue keyword can be used within a for loop to skip over a particular iteration of the loop and move on to the next iteration. This can be useful in cases where you need to skip over certain iterations based on a condition or if you need to continue the loop even if a particular iteration fails.
Here’s an example of how to use the continue keyword in a for loop:
*** Variables ***
@{list} item1 item2 item3 item4 item5
*** Test Cases ***
Example Test Case
: FOR ${item} IN @{list}
\ Run Keyword If '${item}' == 'item3' Continue For Loop
\ Log ${item}
In this example, we have a list of items and we’re using a for loop to iterate through each item in the list. We’re using the Run Keyword If keyword to check if the current item is equal to “item3”. If it is, we use the Continue For Loop keyword to skip over that iteration and move on to the next one. If the current item is not “item3”, we log the value of the item.
If we run this test case, the output will be:
Example Test Case
item1
item2
item4
item5
As you can see, the “item3” iteration was skipped over due to the Continue For Loop keyword.
Another example of using the continue keyword in a for loop is to continue the loop even if a particular iteration fails. Here’s an example:
*** Test Cases ***
Example Test Case
: FOR ${item} IN @{list}
\ Run Keyword And Continue On Failure ${item} Keyword
\ Log ${item}
In this example, we’re using the Run Keyword And Continue On Failure keywords to execute a keyword for each item in the list. If the keyword fails for a particular item, the loop will continue to the next item instead of stopping. We then log the value of the item.
If we run this test case, the output will be:
Example Test Case
item1
item2
item3
item4
item5
As you can see, even though the keyword failed for “item3”, the loop continued to the next item and executed the keyword for the remaining items.
In conclusion, the continue keyword in Robot Framework can be used within a for loop to skip over a particular iteration and move on to the next one. This can be useful in cases where you need to skip over certain iterations based on a condition or if you need to continue the loop even if a particular iteration fails.