程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了比较Robot框架中的json数据大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决比较Robot框架中的json数据?

开发过程中遇到比较Robot框架中的json数据的问题如何解决?下面主要结合日常开发的经验,给出你关于比较Robot框架中的json数据的解决方法建议,希望对你解决比较Robot框架中的json数据有所启发或帮助;

我在 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,"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 作为字典

您可能希望在 this discussion 上看到答案 - 这不是您要寻找的内容,但具有基础知识。 您可以使用

从JSON创建字典,而不必做这么多额外的工作
JPQL

此后,您可以在 Robot Framework 中正常调用字典中的任何值

nativeQuery

从 JSON 字典中获取项目

要解决您的问题,您需要为您拥有的每个 JSON 文件创建一个字典,然后比较这些字典的兼容键。当然 ${json}= evaluate json.loads('''${json_String}''') json 会返回一个列表,因为 ${status}= Get From Dictionary ${json} Status # ${status} will now contain String OK 键是列表格式,但它不会改变这里的想法。我们将遍历 json_A 提供的列表,然后将列表条目与 json_B 和 json_C 中的键进行比较。你可以虑以下

json_A

现在以上仅针对 json_A 测试 json_B - 要将 json_C 添加到循环中,只需重复相关关键字即可。

另请注意,如果您希望验证同一 JSON 文件中的某些嵌套字典,您可以简单地获取嵌套字典以分隔变量并在那里使用相同的方法。

比较字典项

在同一个 FOR 循环中比较两个具有相同键的字典是相当简单的。您只需获取单独的键列表或字典并遍历键并验证值是否匹配。对于这种情况,Robot 甚至有一个 keyword Properties 可以为您进行长度和键值对的比较。

如果存在非失败状态的可能性,即字典中存在的键数不相等,请使用具有较少键的字典进行迭代。如果现在知道了,您需要在尝试访问密钥之前验证它是否存在,否则 Python 将抛出 *** 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} 。对于这种情况,您可以将以下脚本视为上述脚本的扩展 - 替换 FOR 循环。

Dictionaries Should be Equal

大佬总结

以上是大佬教程为你收集整理的比较Robot框架中的json数据全部内容,希望文章能够帮你解决比较Robot框架中的json数据所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。