【问题标题】:How to capture the values from Get Response Body - Robot framework如何从 Get Response Body - Robot 框架中获取值
【发布时间】:2019-01-31 16:18:41
【问题描述】:

响应正文的输出

{"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

除了捕获单个 id 之外,是否可以将它们放入 for 循环中?

:FOR    ${i}  IN          ${ids}
\    ${the_id=       Get Json Value    ${resp.content}    /data/${i}/id ?

我试过了,但失败了。

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

谢谢。

【问题讨论】:

    标签: json python-2.7 robotframework


    【解决方案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
    ==============================================================================
    

    【讨论】:

    • @Kootstra,这行得通!!!。太感谢了。能够从响应中提取数据/id 并与测试数据进行比较。需要进行一些微调,并将很快发布解决方案。再次感谢您
    • 如果这确实是您寻求的解决方案,请将此回复标记为答案,以便其他人一眼就知道。
    • //测试文件 ${data_file_string} 获取文件 .txt ${data_file_obj} Parse Json ${data_file_string} :FOR ${I} IN @{data_file_obj['data']} \追加到列表 ${list_1} ${i['ids']} 排序列表 ${list_1} ${resp} 获取请求 http / ${resp_data}= Parse Json ${resp.content} :FOR ${ i} IN @{resp_data['data']} \ 追加到列表 ${list_2} ${i['ids']} 排序列表 ${list_2} 列表应该相等 ${list_1} ${list_2}
    猜你喜欢
    • 2022-11-11
    • 2015-10-28
    • 2021-08-09
    • 1970-01-01
    • 2020-08-27
    • 2020-01-18
    • 2019-07-22
    • 2011-10-26
    • 1970-01-01
    相关资源
    最近更新 更多