当使用 json 库并且列表与 unicode 字符混合时,列表输出无法识别机器人框架中的自定义库 [英] List output are not getting recognize for custom library in robotframework when json library is used and list is mix with unicode character
问题描述
这里三个文件 list.robot ,code.json 是输入文件,库文件 compareLib.py当我运行这个程序时,它总是返回 False ,我期待 true 因为列表中存在 30所有文件都在同一个文件夹中
Here three file list.robot , code.json is input file , Library file compareLib.py when i run this program, it always return False , i am expecting true since 30 is present in list all file are present in same folder
*** Settings ***
Library SudsLibrary
Library JSONLibrary
Library OperatingSystem
Library compareLib.py
*** Test Cases ***
test json data1
# load file in json object
${json_obj}= Get file code.json
${obj}= evaluate json.loads('''${json_obj}''') json
log ${obj}
${value} = Get Value From Json ${obj} $..code_id
#variable ${value} return list [20,30,40] from code.json file
log to console ${value}
${compare} set variable 30
${contain} = contain_number ${value} ${compare}
log to console ${contain}
示例 code.json 文件从数组 '[' 开始,然后是三个块 {},然后是数组结束块 ']'
sample code.json file start from array '[' then three block {} then array closing block ']'
lib 文件
这里 arg1 是列表 [20,30,40] 和 arg2 是 30 ,我期待 True 但它返回 false
here arg1 is list [20,30,40] and arg2 is 30 , i am expecting True but it returns false
def contain_number(arg1,arg2):
if arg2 in arg1:
return True
else:
return False
推荐答案
问题是当你将 ${compare} 变量从 robotsframework 传递给 python 函数时,它变成了 unicode 字符串
The problem was when you pass the ${compare} variable from robotframework to python function , it was going as unicode string
所以comaprison是这样的([30, 40, 30], u'30')
so the comaprison was some thing like this ([30, 40, 30], u'30')
u'30' 不会出现在 ${Value} 的列表中.
u'30' was not going to be found in the list that is ${Value}.
所以我已经将机器人框架传递的字符串转换为 Python 文件中的整数,现在应该可以使用了
so i have converted the string passed from robotframework to integer in your Python file and that should work now
def contain_number(arg1,arg2):
arg2=int(arg2)
if arg2 in arg1:
return True
else:
return arg1,arg2,False
这篇关于当使用 json 库并且列表与 unicode 字符混合时,列表输出无法识别机器人框架中的自定义库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!