python - 使用字典、RobotFramework 时 Evaluate 和 SetVariable 之间的区别

标签 python dictionary robotframework

我正在 Robot Framework 中使用字典,并尝试在给定键时获取值。当我使用“SetVariable”命令时它似乎起作用,但当我使用“Evaluate”命令时它不起作用。 我还使用 Python 2.7 和 RIDE 来使用 Robot 框架。我在互联网上搜索了答案,但没有任何运气

** Variables **

&{Units_Lookup}    0=1    1=2    2=3    3=4    4=5    5=0    b=2

** Test Cases **

Experiment
    ${Counter}    Set Variable    1
    ${Value}    Set Variable    &{Units_Lookup}[${Counter}]
    Log To Console    \r${Value}
    ${Counter}    Evaluate    0+1
    ${Value}    Set Variable    &{Units_Lookup}[${Counter}]
    Log To Console    \r${Value}

最佳答案

了解变量类型的差异非常重要。在 Python 中,字符串和整数是有区别的。在您的字典示例中,键是字符串,评估的结果是整数。在下面的示例中将所有整数转换为其真实类型:

** Variables **

&{Units_Lookup}    ${0}=1    ${1}=2    ${2}=3    ${3}=4    ${4}=5    ${5}=0    b=2

** Test Cases **

Experiment
    ${Counter}    Set Variable    ${1}
    ${Value}    Set Variable    &{Units_Lookup}[${Counter}]
    Log To Console    \r${Value}
    ${Counter}    Evaluate    0+1
    ${Value}    Set Variable    &{Units_Lookup}[${Counter}]
    Log To Console    \r${Value}

如果您使用现有代码,则更改最后一个 Set Variable 语句以将 ${counter} 转换为 ${Counter.__str__() } 也将用于转换 Evaluate 的结果。

** Variables **

&{Units_Lookup}    0=1    1=2    2=3    3=4    4=5    5=0    b=2

** Test Cases **

Experiment
    ${Counter}    Set Variable    1
    ${Value}    Set Variable    &{Units_Lookup}[${Counter}]
    Log To Console    \r${Value}
    ${Counter}    Evaluate    0+1
    ${Value}    Set Variable    &{Units_Lookup}[${Counter.__str__()}]
    Log To Console    \r${Value} 

关于python - 使用字典、RobotFramework 时 Evaluate 和 SetVariable 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53594817/

相关文章:

python - pandas 中是否有一个函数可以在数据帧行中查找某些数据的 "gaps"?

python - 如何在python中传递包含单个字符串的列表?

python - 计算字符串中的多个字母组

arrays - 如何使用 Swift 将字典添加到数组?

c++ - 查找和排序列表

python - 如何将对象传递给机器人框架中的关键字?

python - 如何在 Python 中使用卡尔曼滤波器获取位置数据?

ios UITextChecker 无法将单词添加到字典中

python - 是否可以在不使用 jenkins 上的 "PyAutoGUI"库的情况下使用键盘操作?

robotframework - 机器人框架 : run setup for an entire test suite