Sweety
Sweety

Reputation: 85

How to capture the values from Get Response Body - Robot framework

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

Problem

I have created a list of above ids in the test case and I need to compare the created data against the id returned in the response body. t_ids returns 122and when 0 is replaced by 1, returns 123

Rather than capturing individual id, is it possible to put them in for loop?

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

I tried this and failed.

What is the possible solution to compare the ids from the response data against the created list?

Thank you.

Upvotes: 2

Views: 18646

Answers (1)

A. Kootstra
A. Kootstra

Reputation: 6961

It is possible to what you want, but it is always good to know what kind of data structure your variable contains. In the below example loading a json file replaces the received answer in ${resp.content}. To my knowledge this is a string, which is also what Get File returns.

The example is split into the json file and the robot file.

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']}

Which in turn gives the following result:

==============================================================================
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
==============================================================================

Upvotes: 3

Related Questions