gpt4 book ai didi

javascript - Robotframework:如何评估从 Javascript 调用返回的 bool 值

转载 作者:行者123 更新时间:2023-11-29 16:52:22 29 4
gpt4 key购买 nike

我很困惑,因为 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 中将 bool 值转换为字符串:

${val} =    Execute Javascript    return sessionStorage.isMatching.toString()

但是我又遇到了一个NameError

我知道的唯一方法是这看起来很糟糕(因为我明确必须将 bool 值转换为 bool 值):

${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.

关于javascript - Robotframework:如何评估从 Javascript 调用返回的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35015039/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com