首页 文章

对于Robot Framework中的字典循环

提问于
浏览
3

有没有一种正确的方法来循环RF中的字典?我使用了pythonic方式,但失败了:

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

output:FOR循环值的数量应该是其变量的倍数 . 得到2个变量,但有1个值 .

同样,当我将字典指向为标量变量时 . 我在文档中找不到一个例子 . 有人解决了吗?

附:

我知道解决方法,你使用kw . 然后,获取字典键和获取字典值,以更新您使用的值设置为字典$ $ ,但这似乎是人类不友好并使用几个for循环迭代而不是一个 .

4 回答

  • 2
    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}
    
  • 3

    迭代字典's keys, you don' t必须使用任何python方法,但insted使用Robotframework的 @ 修饰符进行列表扩展 . 例如:

    ${mydict}    Create Dictionary    a=1    b=2
    :FOR    ${key}    IN    @{mydict}
    \    Log     The current key is: ${key}
    # there are at least to ways to get the value for that key
    # "Extended variable syntax", e.g. direct access:
    \    Log     The value is: ${mydict['${key}']}
    # or using a keyword from the Collections library:
    \    ${value}=    Get From Dictionary    ${mydict}    ${key}
    \    Log     The value through Collections is: ${value}
    

    键上的循环直接工作,因为在python中,字典的 list() 演员实际上是其键的列表 . 示例代码:

    mydict = {'a': 1, 'b': 2}
    print(list(mydict))
    # the output is 
    # ['a', 'b']
    

    有一个python的dict方法 items() 迭代字典并返回一个key,value的元组 . 它可以用来快速地将它们两个放在远离计算机的Robotframework 's loop - but I'm中,并且无法看到它是否在它中工作(据说是的):

    ${mydict}    Create Dictionary    a=1    b=2
    :FOR    ${key}    ${value}    IN    @{mydict.items()}
    \    Log     The current key is: ${key}
    \    Log     The value is: ${value}
    
  • 1

    检查方案:

    :FOR    ${key}    IN    @{mydict}
    # works as @{mydict.keys()}
    
    :FOR    ${key}    ${value}    IN    @{mydict.items()}
    # doesn't work at all
    

    找到另一种解决方案:

    :FOR    ${key}     ${value}    IN ZIP    ${mydict.keys()}    ${mydict.values()}
    
    # And another less readable (can work without setters)
    :FOR    ${el}    IN    @{mydict.items()}   
    \    ${key}=    Set Variable    ${el[0]}
    \    ${value}=    Set Variable    ${el[1]}
    
  • 6

    另一种解决方法是在for循环期间使用关键字“Get From Dictionary” .

    Loop through and Log key and value in dict
        [Documentation]    Loops through each key and stores the key value 
        ...                in a variable that can be used during the for 
        ...                loop, as if you were iterating through python 
        ...                with "for key, value in dict.iteritems()".
        &{mydict}    Create Dictionary    a=1    b=2
        :FOR    ${key}    IN    @{mydict.keys()}
        \    ${value}=    Get From Dictionary    ${mydict}    ${key}
        \    Log    ${key}, ${value}
    

    参考:http://robotframework.org/robotframework/latest/libraries/Collections.html#Get%20From%20Dictionary

相关问题