如何从Get Response Body中获取值-机器人框架

时间:2018-08-25 11:57:01

标签: json python-2.7 robotframework

**Output from Response Body**
{"data":[{"id”:122,"name”:”Test 1“,”description”:”TEST 1 Test 2 …..}]},{"id”:123,"name”:”DYNAMO”……}]},{"id”:126,”name”:”T DYNAMO”……

*** Keywords ***    
Capture The Data Ids 
@{ids}=     Create List    122  123  126  167  190   
    ${header}    Create Dictionary    Authoriztion...   
    ${resp}      Get Response    httpsbin    /data    

    ${t_ids}=       Get Json Value    ${resp.content}    /data/0/id

问题

我已经在测试案例中创建了上述ID的列表,我需要将创建的数据与响应正文中返回的ID进行比较。 t_ids返回122,当0替换为1时,返回123

是否可以将它们放入for循环中而不是捕获单个id?

:在$ {ids}中用于$ {i}         \ $ {the_id =获取Json值$ {resp.content} / data / $ {i} / id吗?

我尝试过但失败了。

将响应数据中的ID与创建的列表进行比较的可能解决方案是什么?

谢谢。

1 个答案:

答案 0 :(得分:1)

可以满足您的要求,但是最好知道您的变量包含哪种数据结构。在以下示例中,加载json文件将替换${resp.content}中收到的答案。据我所知,这是一个字符串,也是Get File返回的字符串。

该示例分为json文件和机械手文件。

so_json.json

{
    "data":[
        {
            "id":122,
            "name": "Test 1",
            "description": "TEST 1 Test 2"
        },
        {
            "id": 123,
            "name": "DYNAMO"
        },
        {
            "id": 126,
            "name": "T DYNAMO"
        }
        ]
 }

so_robot.robot

*** Settings ***
Library    HttpLibrary.HTTP
Library    OperatingSystem    
Library    Collections    


*** Test Cases ***
TC
    ${json_string}  Get File    so_json.json
    ${json_object}  Parse Json    ${json_string}
    :FOR  ${item}    IN    @{json_object['data']}
    \    Log To Console    ${item['id']}

依次给出以下结果:

==============================================================================
Robot - Example                                                             
==============================================================================
Robot - Example.SO JSON                                                     
==============================================================================
TC                                                                    122
123
126
| PASS |
------------------------------------------------------------------------------
Robot - Example.SO JSON                                               | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Robot - Example                                                       | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================