4

I've got a problem with using a loop in another existing loop in RF That is just an example of what I need: I have a list of people (@{people}) and each of the people has a list of items.

I do the following:

:FOR | ${person} | IN @{people} 
\ | @{items}= | Create List | xpath=//div[@class='item'] 
\ | :FOR ${item} | IN @{items} 
\ | \ | ...

The problem is on the second :FOR I got an error 'keyword :FOR is not found'. Do I do something wrong? Or is there any other way to include a loop in another loop?

1

3 Answers 3

9

According to User Guide, having nested for loops is not supported directly, but it is possible to use a user keyword inside a for loop and have another for loop there

*** Keywords ***
Loop over people
    :FOR  ${person}  IN  @{people} 
    \  @{items}=  Create List  xpath=//div[@class='item'] 
    \  Loop over items  @{items}

Loop over items    
    [Arguments]  @{items}
    :FOR  ${item}  IN  @{items} 
    \  ...
Sign up to request clarification or add additional context in comments.

Comments

0

This question is also same as yours Nested loop in RobotFramework

sharing the same answer here as well.

Nested for loops

Having nested for loops is not supported directly, but it is possible to use a user keyword inside a for loop and have another for loop there.

*** Keywords ***
Handle Table
    [Arguments]    @{table}
    :FOR    ${row}    IN    @{table}
    \    Handle Row    @{row}

Handle Row
    [Arguments]    @{row}
    :FOR    ${cell}    IN    @{row}
    \    Handle Cell    ${cell}

Referenced from : http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#nested-for-loops

Comments

0

It's possible with a custom keyword like this: How can I implement Robot Framework-style variables in this nestable For Loop?.

Please note that this does not support While loops, For loops that are not "IN RANGE", or Robot Framework-style variable construction (hence the question), but I use it all the time for my tests to avoid having a second keyword for the inner for loop. It's a lot cleaner.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.