如果字符串中存在双引号,如何比较Robot Framework中的两个变量

时间:2017-10-14 07:17:45

标签: robotframework

在我的测试案例中,我试图比较是否

  

'内容类型" application / pdf"不受支持'

是否存在。

这是我得到的错误:

  

评估表达式'"内容类型" application / pdf"不是   支持" !=""'失败:SyntaxError:语法无效(,行   1)

我的关键字表达式为:

Run Keyword If  "${failure_message}" != "${EMPTY}"   My Click Element  id=btn_import_cancel

2 个答案:

答案 0 :(得分:3)

Robot允许您通过省略大括号来使用表达式中没有引号的变量。

run keyword if  $failure_message != "" My Click Element  id=btn_import_cancel

有关详细信息,请参阅BuiltIn库文档中的Evaluating Expressions

答案 1 :(得分:2)

您可以将变量放在三重引号中 - 在python中称为字符串文字,它几乎可以包含所有字符而不会出现问题 - 双引号,\n等。
案例:

Run Keyword  If  """${failure_message}""" != "${EMPTY}"   My Click Element  id=btn_import_cancel 
# ${EMPTY} is a RF builtin variable, shortcut for an empty string - not needed here, but I guess it helps with the case's readability 

原始方法中发生的事情是RF取代了${failure_message}的值,因为它有双引号char,这有效地封闭了中间的封闭引号 - 这里:"Content-type "application
,将{application}从字符串中删除,并且#34;使其成为" python表达式的操作数。