【问题标题】:How to compare two values as strings using robot framework?如何使用机器人框架将两个值作为字符串进行比较?
【发布时间】:2021-11-18 18:23:44
【问题描述】:

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

${tab}= 获取文本 xpath=.//[@id='projectTable_info'] ${selected text}= Fetch From Right ${tab} of ${selected text}= Fetch 从右 ${tab} of ${sele}= 从左取 ${selected text} 条目 ${empno}= 获取表格单元格
xpath=.//
[@id='projectTable'] 3 6 获取价值 ${empno} ${only value}= 从右取值 ${empno} |应该是字符串 ${only value} ${sele} 转换为字符串 ${only value} 转换 字符串 ${sele} 应该等于 ${only value} ${sele}

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

【问题讨论】:

标签: robotframework


【解决方案1】:

您可以使用Should be equal as strings 而不是Should be equal,它会在进行比较之前将值转换为字符串。

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}

【讨论】:

  • "Should be equal as strings" 当值为 时可能无法解决问题; \n 但是,“应该等于整数”不会有这个副作用,推荐。 \n 我用它来等于 HTTP STATUS CODE。
  • @GaGa_Ek:正确。然而,这个问题是关于比较字符串,而不是整数。
【解决方案2】:

此脚本尝试将输入转换为浮点数并比较 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}

【讨论】:

    【解决方案3】:

    也许使用 evaluate 是最简单的,尤其是在需要 rc 时

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

    【讨论】:

      【解决方案4】:
      def compare(number1, relation, number2):
          if relation == "<":
              assert float(number1) < float(number2)
          if relation == ">":
              assert float(number1) > float(number2)
          if relation == "=>":
              assert float(number1) >= float(number2)
          if relation == "<=":
              assert float(number1) <= float(number2)
          if relation == "=":
              assert float(number1) == float(number2)
      

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2021-07-21
      • 2013-07-24
      • 2018-03-18
      • 2020-07-19
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多