How to verify the JSON result using the Robot Framework RESTinstance library.

4,231 views
Skip to first unread message

Nagaraj Hebbar

unread,
Apr 26, 2019, 2:36:15 AM4/26/19
to robotframework-users
How to verify the JSON result using the Robot Framework RESTinstance library. I have API response body that have attached through the mail how to verify the result of the response body using RESTinstance library can anyone help regarding this.
APITEST.zip

诸玉锋

unread,
May 15, 2019, 6:33:54 AM5/15/19
to robotframework-users
Hi

Let's say, we have response body stored in ${response} already as string or uni-code, and you can do following

${json_dict}    Evaluate    json.loads(${response})   modules=json

After that, a dict will be stored in ${json_dict}

Regards



在 2019年4月26日星期五 UTC+8下午2:36:15,Nagaraj Hebbar写道:

Ananda Kumar

unread,
May 15, 2019, 1:19:18 PM5/15/19
to robotframework-users
Let's say, we have response body stored in ${response} already as string or uni-code, and you can do following

${json_dict}    Evaluate    json.loads(${response})   modules=json

After that, a dict will be stored in ${json_dict}- from here how to validate for particular parameters.
 you  are loading entire response using json.loads(${response})

can you explain ,  if i want evaluate few result from response.

regards.

Ananda

诸玉锋

unread,
May 15, 2019, 10:59:03 PM5/15/19
to robotframe...@googlegroups.com
Hi
Here are the JSON contents from your log file
{ "totalCount": 2, "successCount": 2, "errorCount": 0, "records": [ { "failed": false, "identity": { "documentNo": "NPR45787", "idType": 1, "verified": true, "ifscCode": "", "nameAsPerDoc": "S Raj Kumar", "expiryDate": "2021-01-01", "verifiedDate": "2019-02-01", "employee": 1, "id": 1183 }, "error": null }, { "failed": false, "identity": { "documentNo": "********5413", "idType": 2, "verified": true, "ifscCode": "", "nameAsPerDoc": "D Mohan Rao", "expiryDate": "2022-02-02", "verifiedDate": "2019-02-02", "employee": 5, "id": 1184 }, "error": null } ] }

After the entire content was loaded to a dict, e.g ${json_dict}, we can access all attributes

For example, if you don't expect any error count, you can use following:

Should Not Be Equal as Integer    0    ${json_dict['errorCount']}

If you want to access particular attributes of a single record, you can simply use following as an example

${emplyees}    Set Variable   ${json_dict['records'][0]['identity']['employee']}

Anyway, ${json_dict} is a regular dict which allows to access it with standard RF method


在 2019年5月16日星期四 UTC+8上午1:19:18,Ananda Kumar写道:

Tatu Aalto

unread,
May 16, 2019, 2:47:18 AM5/16/19
to zhuy...@gmail.com, robotframework-users
Ugh

Because RESTinstance does not expose directly the returned message (although it can logged to console or a file) and therefore all the earlier responses are leading you to wrong direction. Instead RESTinstance library stores the last response inside of the library and RESTinstance library provides keywords which can be used to verify that the content is correct in the response. There are two different and in their own way clever way to verify the content: 1) Create schema 2) Validate specific content in the response. I have not used the first option, but if I would do more rest in testing in Robot Framework, I would definitely look in to that direction too. But now for later option (all the example are based on the quick start section [1].)

Lets assume that test looks like this: 
*** Settings ***
Library         REST    https://jsonplaceholder.typicode.com

*** Test Cases ***
GET an existing user
    GET         /users/1                                   # Creates a new response instance in the library
    Object      response body                              # Verifies that there is something in the response body, example that it is not missing or it is not empty.
    Integer     response body id          1                # Verifies that response body contains key "id" and it has integer value 1
    String      response body name        Leanne Graham    # Verifies that response body contains key "name" and it has string "Leanne Graham"

As can be see from from the test, RESTinstance provides keywords and syntax which can be used to travel in the json tree and validate keys and optionally values. The more validation you want to do, the more keywords[2] you use from the RESTinstance library. Please read the quick start[1] section and keyword documentation[2] for further examples to see what kind of different possibilities the RESTinstance library offers.


On Thu, 16 May 2019, 05:59 诸玉锋, <zhuy...@gmail.com> wrote:
Hi
Here are the JSON content from your log file
{ "totalCount": 2, "successCount": 2, "errorCount": 0, "records": [ { "failed": false, "identity": { "documentNo": "NPR45787", "idType": 1, "verified": true, "ifscCode": "", "nameAsPerDoc": "S Raj Kumar", "expiryDate": "2021-01-01", "verifiedDate": "2019-02-01", "employee": 1, "id": 1183 }, "error": null }, { "failed": false, "identity": { "documentNo": "********5413", "idType": 2, "verified": true, "ifscCode": "", "nameAsPerDoc": "D Mohan Rao", "expiryDate": "2022-02-02", "verifiedDate": "2019-02-02", "employee": 5, "id": 1184 }, "error": null } ] }

After the entire content was loaded to a dict, e.g ${json_dict}, we can access all attributes

For example, if you don't expect any error count, you can use following:

Should Not Be Equal as Integer    0    ${json_dict['errorCount']}

If you want to access particular attributes of a single record, you can simply use this as an example

${emplyees}    Set Variable   ${json_dict['records'][0]['identity']['employee']}

Anyway, ${json_dict} is a regular dict which allows to access it with standard RF method

在 2019年5月16日星期四 UTC+8上午1:19:18,Ananda Kumar写道:

Let's say, we have response body stored in ${response} already as string or uni-code, and you can do following

${json_dict}    Evaluate    json.loads(${response})   modules=json

After that, a dict will be stored in ${json_dict}- from here how to validate for particular parameters.
 you  are loading entire response using json.loads(${response})

can you explain ,  if i want evaluate few result from response.

regards.

Ananda


On Wednesday, May 15, 2019 at 4:03:54 PM UTC+5:30, 诸玉锋 wrote:
Hi

Let's say, we have response body stored in ${response} already as string or uni-code, and you can do following

${json_dict}    Evaluate    json.loads(${response})   modules=json

After that, a dict will be stored in ${json_dict}

Regards



在 2019年4月26日星期五 UTC+8下午2:36:15,Nagaraj Hebbar写道:
How to verify the JSON result using the Robot Framework RESTinstance library. I have API response body that have attached through the mail how to verify the result of the response body using RESTinstance library can anyone help regarding this.

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/robotframework-users/f3bfa85d-3fec-490d-ba42-f3c8361d7a96%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages