【发布时间】:2021-09-28 23:07:20
【问题描述】:
我在机器人框架问题中有一个比较 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}
【问题讨论】:
-
到目前为止,您为实现目标做了哪些尝试?
-
你绝对可以在机器人框架中做到这一点,我认为 mutch 更容易的是用 python 编写代码,然后在 rf 中使用它。
-
由于格式不正确,您在此处附加的脚本将无法运行 - 我想您已经很清楚了,但想指出。我从您的脚本中读到的是,您已经将所有数据放在单独的列表中,但无法创建有效的字典?当前的输出是什么,为什么对您不可用?
-
@Morkkis 我有我需要的所有数据(列表),但我不知道如何比较两个列表然后选择相同的对象并存储到另一个列表。之后,我想使用比较结果列表来检查它是否存在于 json_C
标签: robotframework