如何使用机器人框架将两个值比较为字符串?

时间:2015-05-22 07:30:44

标签: robotframework

如何在对象不相等时匹配值,但它们是字符串。

  

$ {tab} =获取文字xpath =。// [@ id =' projectTable_info'] $ {已选中   text} =从$ {selected text} = Fetch的右边$ {tab}获取   来自$ {sele}的右$ {tab} =从左边获取$ {已选中   text} entries $ {empno} =获取表格单元格   xpath =。// [@ id =' projectTable'] 3 6获取价值$ {empno}   $ {only value} =从Right $ {empno} |获取应该是字符串   $ {only value} $ {sele}转换为字符串$ {only value}转换   To String $ {sele}应该等于$ {only value} $ {sele}

它在控制台中出错 如果将对象转换为字符串后对象不相等则会失败。 INFO参数类型是:
失败2!= 2

3 个答案:

答案 0 :(得分:5)

您可以使用Should be equal代替Should be equal as strings,在进行比较之前将值转换为字符串。

Should be equal as strings    ${only value}    ${sele}

您的代码似乎正在尝试手动将值转换为字符串,这也是一种合理的解决方案。不幸的是,Convert to string的文档有点模糊,导致您错误地使用它。关键字不会更改参数,会返回一个新字符串

如果您想手动转换变量,您需要这样做:

${sele}=          Convert to string    ${sele}
${only value}=    Convert to string    ${only value}
Should be equal   ${only value}    ${sele}

答案 1 :(得分:0)

此脚本尝试将输入转换为float并比较Python 2中的值:

def should_be_x_than (self, number1, relation, number2):
    '''
    This keyword makes relation between 2 numbers (it converts them to number)
    Accepted relations:
    < > <= => =
    '''
    if relation =="<":
        return float(number1) < float(number2)
    if relation ==">":
        return float(number1) > float(number2)
    if relation =="=>":
        return float(number1) >= float(number2)
    if relation =="<=":
        return float(number1) <= float(number2)
    if relation =="=":
        return float(number1) == float(number2)

之后我导入了库并将其用作关键字(Should Be X Than)。

示例

Should Be X Than  ${num1}  <  ${num2}

答案 2 :(得分:0)

也许使用评估是最简单的,特别是当rc需要

[Documentation]    = / compare two string \ =
${rc}=    evaluate    'name'=='theon'
Log To Console   \n${rc}