"scores": [{"id": 1,"value": "100"},{"id": 2,"value": "77"},
"id": 3,"value": "96"}
]
... I get the whole array printed out ...
| A Simple Example of "Get Json Value" and "Should Contain" Keywords |
| | ${scores}= | Get Json Value | ${testscores} | /scores |
| | Should Contain | ${scores} | {"id": 2, "value": "77"} |
| Does Id 2 Have A Score Of 77? |
| | ${parsed}= | Parse JSON | ${testscores} | # get the data structure of the json object
| | ${length}= | get length | ${parsed["scores"]} | # this will be the duration of our for loop
| | :FOR | ${score_pair} | In Range | 0 | ${length} | # looping through the values
| | | ${id_value_pair}= | Get JSON Value | ${testscores} | /scores/${score_pair} | # Gets the embedded array
| | | ${status}= | Run Keyword And Return Status | Should Be Equal As Strings | ${id_value_pair} | {"id": 2, "value": "77"} | # Asks if we Pass or Fail
| | | Exit For Loop IF | ${status} | # exits as soon as we find our desired result
| | Should Be Equal As Strings | ${id_value_pair} | {"id": 2, "value": "77"} | | # asserts that we found the desired result
| *** Settings *** |
| Documentation | Three examples of how one might assert that an array of objects within |
| ... | a larger JSON object contains a certain combination of values. |
| ... | Requires HTTPLibrary which you can install with pip install --upgrade robotframework-httplibrary |
| Library | HttpLibrary.HTTP |
| Force Tags | temp |
| *** Variables *** |
| ${testscores} | {"scores": [{"id": 1, "value": "100"}, {"id": 2, "value": "77"}, {"id": 3, "value": "96"} ] } |
| *** Test Cases *** |
| Bare Bones Solution |
| | Should Contain | ${testscores} | {"id": 2, "value": "77"} |
| A Simple Example of "Get Json Value" and "Should Contain" Keywords |
| | ${scores}= | get json value | ${testscores} | /scores |
| | Should Contain | ${scores} | {"id": 2, "value": "77"} |
| Does Id 2 Have A Score Of 77? |
| | ${parsed}= | Parse JSON | ${testscores} | # get the data structure of the json object
| | ${length}= | get length | ${parsed["scores"]} | # this will be the duration of our for loop
| | :FOR | ${score_pair} | In Range | 0 | ${length} | # looping through the values
| | | ${id_value_pair}= | Get JSON Value | ${testscores} | /scores/${score_pair} | # Gets the embedded array
| | | ${status}= | Run Keyword And Return Status | Should Be Equal As Strings | ${id_value_pair} | {"id": 2, "value": "77"} | # Asks if we Pass or Fail
| | | Exit For Loop IF | ${status} | # exits as soon as we find our desired result
| | Should Be Equal As Strings | ${id_value_pair} | {"id": 2, "value": "77"} | | # asserts that we found the desired result
| Here Is The Same Operation Executed As A Keyword With Args |
| | Does The Json Array Contain Our Desired Packet? | ${testscores} | scores | {"id": 2, "value": "77"} |
| *** Keywords *** |
| Does The Json Array Contain Our Desired Packet? |
| | [Documentation] | Find a given JSON packet in an array of packets embedded within a JSON object |
| | [Arguments] | ${our_json} | ${json_pointer} | ${expected_json_packet} |
| | ${parsed}= | Parse JSON | ${our_json} |
| | ${length}= | get length | ${parsed["${json_pointer}"]} |
| | :FOR | ${our_pair} | In Range | 0 | ${length} |
| | | ${id_value_pair}= | Get JSON Value | ${our_json} | /${json_pointer}/${our_pair} |
| | | ${status}= | Run Keyword And Return Status | Should Be Equal As Strings | ${id_value_pair} | ${expected_json_packet} |
| | | Exit For Loop IF | ${status}
| | Should Be Equal As Strings | ${id_value_pair} | {"id": 2, "value": "77"} |
| ${scores}= | GetJsonValue | ${response.json()} | /scores