Search code examples
robotframework

For loop over dictionary in Robot Framework


Is there a proper way to loop over a dictionary in RF? I used pythonic way, but failed:

:FOR  ${key}  ${value}  IN  &{dict}

output: Number of FOR loop values should be multiple of its variables. Got 2 variables but 1 value.

Same, when i pointed dictionary as scalar variable. I couldn't find an example in documentation though. Has anyone solved that?

P.S.

I am aware of workaround solution, that you use kw. Get Dictionary Keys and Get Dictionary Values, then, to update values you use Set To Dictionary ${key} ${new_value}, but this seems to be human unfriendly and uses several for loops iterations instead one.


Solution

  • Loop Through Dict
        &{mydict}    Create Dictionary    a=1    b=2
        :FOR    ${key}    IN    @{mydict.keys()}
        \    Log    ${mydict["${key}"]}
    
    Loop Through Dict And Multiplicate Values
        &{mydict}    Create Dictionary    a=1    b=2
        :FOR    ${key}    IN    @{mydict.keys()}
        \    ${new_value}    Evaluate    ${mydict["${key}"]}*2
        \    Set To Dictionary   ${mydict}    ${key}=${new_value}
        Log    ${mydict}