比较 Robot 框架中的 json 数据
compare json data in Robot framework
我在 Robot 框架问题中比较 json 数据。
如果我有如下三个 json:
json_A:
{
"name":"XXX",
"Type": {
"SubType": {
"Properties": [
"Status",
"Model",
"State",
"Tag",
"Number"],
}
}
}
json_B:
{
"Status": OK,
"Model": YYY,
"Number": 0000,
"Task": XYZ
}
json_C:
{
"Status": OK,
"Model": YYY,
"Number": 0000
"State": ON
"Tag": 1234
}
这是我想做的:
result_A = compare Properties of json_A and Object of json_B and collect SAME object
if "ALL" object of the result_A exist in json_C
is true and print all object key and value
else
break
所以结果为真,会显示
{
“状态”:好的,
“型号”:YYY,
"数量": 0000
}
我知道这可能很复杂,但是机器人框架可以有相关的关键字来做到这一点吗?
谢谢!!
暂时附上我的作品:
*** Settings ***
Library JsonValidator
*** Keywords ***
Get_Response_1
[Arguments] ${ip}=${ip}
... ${username}=${USERNAME}
... ${password}=${PASSWORD}
${auth} = Create List ${username} ${password}
Create Session alias=test_session url=http://${ip}:8080/redfish/v1
... auth=${auth} verify=${False}
${response} = GET On Session test_session
... url=https://${ip}:8080/redfish/v1/XXXXXX/YYYYY/ZZZZZ
Status Should Be 200 ${response}
Should Be Equal As Strings OK ${response.reason}
[Return] ${response}
Get_Response_2
[Arguments] ${ip}=${ip}
... ${username}=${USERNAME}
... ${password}=${PASSWORD}
${auth} = Create List ${username} ${password}
Create Session alias=test_session url=http://${ip}:8080/redfish/v1
... auth=${auth} verify=${False}
${response} = GET On Session test_session
... url=https://${ip}:8080/redfish/v1/AAAA/BBBB/CCCC
Status Should Be 200 ${response}
Should Be Equal As Strings OK ${response.reason}
[Return] ${response}
Get_JSON_File_Data
[Documentation] Get_JSON_File_Data
${json}= Get File D:\xxx\yyy\zzz\json_A.json
${Properties}= get json value ${json} /Type/SubType/Properties
[Return] ${Properties}
Get_Properties_List
[Documentation] Get_Properties_List
[Arguments] ${GET_RESPONSE_DATA}
${reponse_content}= Parse Json ${GET_RESPONSE_DATA.content}
log ${reponse_content}
FOR ${properties} IN @{reponse_content}
${elements}= get elements ${reponse_content} ${properties}
${properties_list}= create dictionary ${properties} ${elements}
Log ${elements}
Log ${properties}
Log ${properties_list}
END
[Return] ${properties_list}
*** Test Cases ***
${Properties}= Get_JSON_File_Data
log ${Properties}
${GET_JSON_RESPONSE_1}= GET_Response_1
log ${GET_JSON_RESPONSE_1.content}
${properties_list}= Get_Properties_List ${GET_JSON_RESPONSE_1}
Log ${properties_list}
${GET_JSON_RESPONSE_2}= GET_Response_2
Log ${GET_JSON_RESPONSE_2.content}
${properties_list}= Get_Properties_List ${GET_JSON_RESPONSE_2}
JSON 作为词典
您可能想在 上查看答案 - 这不是您要找的东西,但具有基础知识。
不必做这么多额外的工作,您可以使用
从 JSON 创建字典
${json}= evaluate json.loads('''${json_string}''') json
之后你可以在 Robot Framework 中正常调用字典中的任何值
${status}= Get From Dictionary ${json} Status
# ${status} will now contain string OK
正在从 JSON 词典中获取项目
要解决您的问题,您需要为每个 JSON 文件创建一个字典,然后比较字典中的兼容键。当然 json_A
会 return 一个列表,因为 Properties
键是列表格式,但它不会改变这里的想法。我们将遍历 json_A 提供的列表,然后将列表条目与 json_B 和 json_C 中的键进行比较。您可以考虑关注
*** Test Cases ***
Compare JSON File Contents
# First get each JSON File in a dictionary
${file}= Get File D:\xxx\yyy\zzz\json_A.json
${json_A_full}= evaluate json.loads('''${file}''') json
${file}= Get File <path\to\json_B.json>
${json_B}= evaluate json.loads('''${file}''') json
# Because in JSON A the properties key contains a list instead of dictionary, we'll need to get the list as a dictionary
${json_A}= Get From Dictionary ${json_A_full} Properties
# Now we have two variables where
# ${json_A} == [ "Status", "Model", "State", "Tag", "Number"]
# ${json_B} == { "Status": OK, "Model": YYY, "Number": 0000, "Task": XYZ }
# Iterating through list ${json_A} let's us know if all keys are present in dictionary ${json_B}
# Let's create a temp dictionary where we collect the matching keys
&{returnDict}= Create Dictionary
FOR ${item} IN @{json_A}
${key_exist}= Run Keyword and Return Status Dictionary Should Contain Key ${json_B} ${item}
IF '${key_exist}'=='False'
Log Key ${item} not found from json_B!
ELSE
Log Key ${item} found in json_B with value ${json_B[${item}]}
# You can return the key from json_B by simply setting a new variable for it
Set to Dictionary ${returnDict} ${item}=${json_B[${item}]}
END
END
# The returnDict contents should now have each key and value that were matching between json_A and json_B
Log ${returnDict}
现在以上仅测试 json_B 与 json_A - 要将 json_C 添加到循环中,只需重复相关关键字即可。
另请注意,如果您希望在同一个 JSON 文件中验证某些嵌套字典,您可以简单地获取嵌套字典以分隔变量并在那里也使用相同的方法。
比较词典项目
在同一个 FOR 循环中比较两个具有相同键的字典是相当简单的。您只需采用单独的键列表或字典并遍历键并验证值是否匹配。对于这种情况,Robot 甚至有一个 keyword Dictionaries Should be Equal
,它将为您比较长度和 key:value 对。
如果存在非失败状态可能性,即字典中存在的键数不相等,请使用键数较少的字典进行迭代。如果现在已知,您需要在尝试访问之前验证密钥的存在,否则 Python 将抛出 KeyError
。对于这种情况,您可以将下面的脚本视为上面脚本的扩展 - 替换 FOR 循环。
# JSON Files are parsed to dictionaries and list
# @{json_A}, &{json_B}, &{json_C}
FOR ${key} IN @{json_A}
# Skip validation where either C or B files do not contain same key
${B_exist}= Run Keyword and Return Status Dictionary Should Contain Key ${json_B} ${item}
${C_exist}= Run Keyword and Return Status Dictionary Should Contain Key ${json_C} ${item}
Continue For Loop If '${B_exist}'=='False' or '${C_exist}'=='False'
# Simply validating the keys contain same value in both dictionaries
Should Be Equal ${json_B[${key}]} ${json_C[${key}]}
END
我在 Robot 框架问题中比较 json 数据。 如果我有如下三个 json:
json_A:
{
"name":"XXX",
"Type": {
"SubType": {
"Properties": [
"Status",
"Model",
"State",
"Tag",
"Number"],
}
}
}
json_B:
{
"Status": OK,
"Model": YYY,
"Number": 0000,
"Task": XYZ
}
json_C:
{
"Status": OK,
"Model": YYY,
"Number": 0000
"State": ON
"Tag": 1234
}
这是我想做的:
result_A = compare Properties of json_A and Object of json_B and collect SAME object
if "ALL" object of the result_A exist in json_C
is true and print all object key and value
else
break
所以结果为真,会显示
{
“状态”:好的,
“型号”:YYY,
"数量": 0000
}
我知道这可能很复杂,但是机器人框架可以有相关的关键字来做到这一点吗? 谢谢!!
暂时附上我的作品:
*** Settings ***
Library JsonValidator
*** Keywords ***
Get_Response_1
[Arguments] ${ip}=${ip}
... ${username}=${USERNAME}
... ${password}=${PASSWORD}
${auth} = Create List ${username} ${password}
Create Session alias=test_session url=http://${ip}:8080/redfish/v1
... auth=${auth} verify=${False}
${response} = GET On Session test_session
... url=https://${ip}:8080/redfish/v1/XXXXXX/YYYYY/ZZZZZ
Status Should Be 200 ${response}
Should Be Equal As Strings OK ${response.reason}
[Return] ${response}
Get_Response_2
[Arguments] ${ip}=${ip}
... ${username}=${USERNAME}
... ${password}=${PASSWORD}
${auth} = Create List ${username} ${password}
Create Session alias=test_session url=http://${ip}:8080/redfish/v1
... auth=${auth} verify=${False}
${response} = GET On Session test_session
... url=https://${ip}:8080/redfish/v1/AAAA/BBBB/CCCC
Status Should Be 200 ${response}
Should Be Equal As Strings OK ${response.reason}
[Return] ${response}
Get_JSON_File_Data
[Documentation] Get_JSON_File_Data
${json}= Get File D:\xxx\yyy\zzz\json_A.json
${Properties}= get json value ${json} /Type/SubType/Properties
[Return] ${Properties}
Get_Properties_List
[Documentation] Get_Properties_List
[Arguments] ${GET_RESPONSE_DATA}
${reponse_content}= Parse Json ${GET_RESPONSE_DATA.content}
log ${reponse_content}
FOR ${properties} IN @{reponse_content}
${elements}= get elements ${reponse_content} ${properties}
${properties_list}= create dictionary ${properties} ${elements}
Log ${elements}
Log ${properties}
Log ${properties_list}
END
[Return] ${properties_list}
*** Test Cases ***
${Properties}= Get_JSON_File_Data
log ${Properties}
${GET_JSON_RESPONSE_1}= GET_Response_1
log ${GET_JSON_RESPONSE_1.content}
${properties_list}= Get_Properties_List ${GET_JSON_RESPONSE_1}
Log ${properties_list}
${GET_JSON_RESPONSE_2}= GET_Response_2
Log ${GET_JSON_RESPONSE_2.content}
${properties_list}= Get_Properties_List ${GET_JSON_RESPONSE_2}
JSON 作为词典
您可能想在
${json}= evaluate json.loads('''${json_string}''') json
之后你可以在 Robot Framework 中正常调用字典中的任何值
${status}= Get From Dictionary ${json} Status
# ${status} will now contain string OK
正在从 JSON 词典中获取项目
要解决您的问题,您需要为每个 JSON 文件创建一个字典,然后比较字典中的兼容键。当然 json_A
会 return 一个列表,因为 Properties
键是列表格式,但它不会改变这里的想法。我们将遍历 json_A 提供的列表,然后将列表条目与 json_B 和 json_C 中的键进行比较。您可以考虑关注
*** Test Cases ***
Compare JSON File Contents
# First get each JSON File in a dictionary
${file}= Get File D:\xxx\yyy\zzz\json_A.json
${json_A_full}= evaluate json.loads('''${file}''') json
${file}= Get File <path\to\json_B.json>
${json_B}= evaluate json.loads('''${file}''') json
# Because in JSON A the properties key contains a list instead of dictionary, we'll need to get the list as a dictionary
${json_A}= Get From Dictionary ${json_A_full} Properties
# Now we have two variables where
# ${json_A} == [ "Status", "Model", "State", "Tag", "Number"]
# ${json_B} == { "Status": OK, "Model": YYY, "Number": 0000, "Task": XYZ }
# Iterating through list ${json_A} let's us know if all keys are present in dictionary ${json_B}
# Let's create a temp dictionary where we collect the matching keys
&{returnDict}= Create Dictionary
FOR ${item} IN @{json_A}
${key_exist}= Run Keyword and Return Status Dictionary Should Contain Key ${json_B} ${item}
IF '${key_exist}'=='False'
Log Key ${item} not found from json_B!
ELSE
Log Key ${item} found in json_B with value ${json_B[${item}]}
# You can return the key from json_B by simply setting a new variable for it
Set to Dictionary ${returnDict} ${item}=${json_B[${item}]}
END
END
# The returnDict contents should now have each key and value that were matching between json_A and json_B
Log ${returnDict}
现在以上仅测试 json_B 与 json_A - 要将 json_C 添加到循环中,只需重复相关关键字即可。
另请注意,如果您希望在同一个 JSON 文件中验证某些嵌套字典,您可以简单地获取嵌套字典以分隔变量并在那里也使用相同的方法。
比较词典项目
在同一个 FOR 循环中比较两个具有相同键的字典是相当简单的。您只需采用单独的键列表或字典并遍历键并验证值是否匹配。对于这种情况,Robot 甚至有一个 keyword Dictionaries Should be Equal
,它将为您比较长度和 key:value 对。
如果存在非失败状态可能性,即字典中存在的键数不相等,请使用键数较少的字典进行迭代。如果现在已知,您需要在尝试访问之前验证密钥的存在,否则 Python 将抛出 KeyError
。对于这种情况,您可以将下面的脚本视为上面脚本的扩展 - 替换 FOR 循环。
# JSON Files are parsed to dictionaries and list
# @{json_A}, &{json_B}, &{json_C}
FOR ${key} IN @{json_A}
# Skip validation where either C or B files do not contain same key
${B_exist}= Run Keyword and Return Status Dictionary Should Contain Key ${json_B} ${item}
${C_exist}= Run Keyword and Return Status Dictionary Should Contain Key ${json_C} ${item}
Continue For Loop If '${B_exist}'=='False' or '${C_exist}'=='False'
# Simply validating the keys contain same value in both dictionaries
Should Be Equal ${json_B[${key}]} ${json_C[${key}]}
END