Robotframework:如何评估从 Javascript 调用返回的布尔值
Robotframework: How to evaluate boolean value returned from Javascript call
我很困惑这失败了 NameError
:
${val} = Execute Javascript return sessionStorage.isMatching
Should Be True ${val}
日志输出为:
INFO Executing JavaScript:
INFO ${val} = true
KEYWORD BuiltIn . Should Be True ${val}
TRACE Arguments: [ 'true' ]
FAIL Evaluating expression 'true' failed: NameError: name 'true' is not defined
这也不行:
Should Be True ${val} == true --> Evaluating expression 'true == true' failed: NameError: name 'true' is not defined
Should Be True ${val} == 'true' --> Evaluating expression 'true == 'true'' failed: NameError: name 'true' is not defined
Should Be True ${val} == "true" --> Evaluating expression 'true == "true"' failed: NameError: name 'true' is not defined
阅读文档后,我了解到底层 python 不会将 JS true
评估为 true
。然后我尝试在 JS 中将布尔值转换为字符串:
${val} = Execute Javascript return sessionStorage.isMatching.toString()
但我又得到了一个 NameError
。
我知道的唯一方法是这看起来很糟糕(因为我明确必须将布尔值转换为布尔值):
${val} = Execute Javascript return sessionStorage.isMatching
${bool} = Convert To Boolean ${val}
Should Be True ${bool}
有没有更好的方法,在 RF 或 JS 中对此进行评估?
版本:
Python 2.7.8
Robotframework 3.0
您从 javascript 得到的是文字字符串 'true'
。要在表达式中使用它,您必须引用它:
Should be true '${val}` == 'true'
您收到错误 NameError: name 'true' is not defined
的原因是机器人在计算表达式之前进行了字符串替换。
换句话说,机器人中的这个表达式:
should be true ${val} == 'true'
... 与您在 python:
中输入的相同
if true == 'true':
...
Python 看到 true
,尝试查找名为 true
的变量的值,但没有找到任何此类变量。因此,你得到 NameError
.
您可以在 BuiltIn library(强调我的)的文档中找到对此的描述:
When a variable is used in the expressing using the normal ${variable} syntax, its value is replaces before the expression is evaluated. This means that the value used in the expression will be the string representation of the variable value, not the variable value itself. This is not a problem with numbers and other objects that have a string representation that can be evaluated directly, but with other objects the behavior depends on the string representation. Most importantly, strings must always be quoted, and if they can contain newlines, they must be triple quoted.
我很困惑这失败了 NameError
:
${val} = Execute Javascript return sessionStorage.isMatching
Should Be True ${val}
日志输出为:
INFO Executing JavaScript:
INFO ${val} = true
KEYWORD BuiltIn . Should Be True ${val}
TRACE Arguments: [ 'true' ]
FAIL Evaluating expression 'true' failed: NameError: name 'true' is not defined
这也不行:
Should Be True ${val} == true --> Evaluating expression 'true == true' failed: NameError: name 'true' is not defined
Should Be True ${val} == 'true' --> Evaluating expression 'true == 'true'' failed: NameError: name 'true' is not defined
Should Be True ${val} == "true" --> Evaluating expression 'true == "true"' failed: NameError: name 'true' is not defined
阅读文档后,我了解到底层 python 不会将 JS true
评估为 true
。然后我尝试在 JS 中将布尔值转换为字符串:
${val} = Execute Javascript return sessionStorage.isMatching.toString()
但我又得到了一个 NameError
。
我知道的唯一方法是这看起来很糟糕(因为我明确必须将布尔值转换为布尔值):
${val} = Execute Javascript return sessionStorage.isMatching
${bool} = Convert To Boolean ${val}
Should Be True ${bool}
有没有更好的方法,在 RF 或 JS 中对此进行评估?
版本:
Python 2.7.8
Robotframework 3.0
您从 javascript 得到的是文字字符串 'true'
。要在表达式中使用它,您必须引用它:
Should be true '${val}` == 'true'
您收到错误 NameError: name 'true' is not defined
的原因是机器人在计算表达式之前进行了字符串替换。
换句话说,机器人中的这个表达式:
should be true ${val} == 'true'
... 与您在 python:
中输入的相同if true == 'true':
...
Python 看到 true
,尝试查找名为 true
的变量的值,但没有找到任何此类变量。因此,你得到 NameError
.
您可以在 BuiltIn library(强调我的)的文档中找到对此的描述:
When a variable is used in the expressing using the normal ${variable} syntax, its value is replaces before the expression is evaluated. This means that the value used in the expression will be the string representation of the variable value, not the variable value itself. This is not a problem with numbers and other objects that have a string representation that can be evaluated directly, but with other objects the behavior depends on the string representation. Most importantly, strings must always be quoted, and if they can contain newlines, they must be triple quoted.