This is mostly a follow-up of the article Robot Framework, REST and JSON. As this article is now 5 years old, situation has evolved a bit, and recently a new REST library for Robot Framework got some attention: RESTinstance. So let’s take a quick look at it.
Before testing this new lib, let’s rewind a bit. First we need a product to test via a REST API and we will choose one that returns a JSON. For example a GET
on http://echo.jsontest.com/framework/robot-framework/api/rest
should return:
{ "api": "rest", "framework": "robot-framework" } |
Here is how we checked the response of that endpoint in the previous article using requests library:
*** settings *** Library Collections Library requests *** test cases *** simpleRequest ${result} = get http://echo.jsontest.com/framework/robot-framework/api/rest Should Be Equal ${result.status_code} ${200} ${json} = Set Variable ${result.json()} ${framework} = Get From Dictionary ${json} framework Should Be Equal ${framework} robot-framework ${api} = Get From Dictionary ${json} api Should Be Equal ${api} rest |
And here is how it can be checked using RESTinstance library:
*** settings *** Library REST http://echo.jsontest.com *** test cases *** simpleRequest GET /framework/robot-framework/api/rest Object response body String response body api rest String response body framework robot-framework |
So when we do a GET, we create a new instance of an object that can then be used by the other keywords of the library. This object can be written on the console log using Output
keyword. In my case I get this output:
The current instance as JSON is: { "spec": {}, "request": { "body": null, "scheme": "http", "sslVerify": true, "headers": { "Content-Type": "application/json", "Accept": "application/json, */*", "User-Agent": "RESTinstance/1.0.0b35" }, "allowRedirects": true, "timestamp": { "utc": "2018-08-15T19:23:04.046254+00:00", "local": "2018-08-15T21:23:04.046254+02:00" }, "netloc": "echo.jsontest.com", "url": "http://echo.jsontest.com/framework/robot-framework/api/rest", "cert": null, "timeout": [ null, null ], "path": "/framework/robot-framework/api/rest", "query": {}, "proxies": {}, "method": "GET" }, "response": { "seconds": 0.173237, "status": 200, "body": { "framework": "robot-framework", "api": "rest" }, "headers": { "Content-Length": "56", "Server": "Google Frontend", "X-Cloud-Trace-Context": "8e86c2afa42b5380847247dbdc8a7e24", "Date": "Wed, 15 Aug 2018 19:23:03 GMT", "Access-Control-Allow-Origin": "*", "Content-Type": "application/json; charset=ISO-8859-1" } }, "schema": { "exampled": true, "version": "draft04", "request": { "body": { "type": "null" }, "query": { "type": "object" } }, "response": { "body": { "required": [ "api", "framework" ], "type": "object", "properties": { "framework": { "enum": [ "robot-framework" ], "type": "string", "example": "robot-framework" }, "api": { "enum": [ "rest" ], "type": "string", "example": "rest" } } } } } } |
So when we do String response body api rest
we are exploring this object and checking the content of [“response”][“body”][“api”] but as we can see there is much more that can be checked. This syntax was a bit unexpected for me but I guess one just has to get used to it.
The library is also quite powerful in terms of JSON schema checking (and generation). I invite you to check the README to explore it further.
Hello Laurent, I m trying a POST method, running into error due to the quotes on the body – Serialized Order Query: “{ completedOrders : { numberOfDays: 30 } }”
– error attempting ready order query com.peapod.util.RequestReader$RequestReadException: Unable to read instance of class class
Any suggestion to send the body without the quotes?
Hi,
please share more details about what/how you are trying to do. Show some Robot Framework code that you are using so that we can help.
Laurent
Hi,
i am tying to run below
POST Statement On User API
set headers for session
POST /api/v4.0/user/${userId}/orders/query { completedOrders : { numberOfDays: 30 } }
output
output response body
Integer response status 200
In server log i see the call as below when i run robot. I get a 400 bad request.
Serialized Order Query: “{ completedOrders : { numberOfDays: 30 } }”
Now the when i hit the api from browser in server logs i see as below without quotes on the body, which returns valid response.
Serialized Order Query: { completedOrders : { numberOfDays: 30 } }
i got this to work with POST /api/v4.0/user/${userId}/orders/query { “completedOrders” : { “numberOfDays”: 30 } }
Excellent! Thanks for letting me know.
Hi,
How could I get a value of response as string and compare it to a partial string.
I have found that
${resp_string}= String response body api
Should Contain ${resp_string} re
does not work. says ‘[“rest”]’ does not contain ‘re’
Looks like you receive an array instead of a string.
In that case you should try:
Should Contain ${resp_string}[0] re
Thanks! managed to do it this way:
Should Contain ${response[‘body’][‘api’]} re
hi,
im desesperate …you can help me
i was tried to post with the data below and the output is not correct json output i guess, how can make output without the character ‘u’
{u’username’: u’my name’, u’password’: u’my password’, u’grant_type’: u’password’, u’client_id’: u’Pular’}
The “u” is for Unicode in Python.
Check out some stack overflow threads to see how to “remove” it
Exemple: https://stackoverflow.com/questions/9773121/removing-u-in-list
Hi Laurent,
I’m trying to run get from REST API.
Here is the snippets of my code.
But its throws an error.
>>>>>>>>>>>>>>>>
ValueError: Could not parse ‘
Error 404 Not Found
HTTP ERROR 404
Problem accessing /http://10.211.15.228:7896/xxx Reason:
Not Found
‘ as JSON: No JSON object could be decoded
>>>>>>>>>
Code
*** Settings ***
Library OperatingSystem
Library Collections
Library json
Library String
Library HttpLibrary.HTTP
Library RequestsLibrary
Library JSONLibrary
Library DataDriver test.xlsx sheet_name=Sheet1
Suite Setup create the session
Test Template check_api
Suite Teardown close session
*** Variables ***
${base_url} http://10.211.15.228:7896
${result} Mount point does not exist.
${timeout} 1s
${invalid} Invalid Parameter
*** Test Cases ***
${TestCase}
*** Keywords ***
create the session
${user_pass}= create list admin admin
create session mysession ${base_url} auth=${user_pass}
check_api
[Arguments] ${url}
${headers}= create dictionary Content-Type=application/json
${res}= get request mysession ${url} headers=${headers}
sleep ${timeout}
${res1}= set variable ${res.status_code}
${res2}= set variable ${res.content}
${res3}= get json value ${res2} /errors/error/0/error-message
Run Keyword If ‘${res1}’ == ‘200’ check_200 ${res2}
… ELSE IF ‘${res1}’ == ‘404’ check_404 ${res2}
… ELSE IF ‘${res1}’ == ‘500’ check_500 ${res2}
… ELSE check_404! ${res1} ${res2}
check_200
[Arguments] ${res2}
log 200
log many ${res2}
check_404
[Arguments] ${res2}
${res3}= get json value ${res2} /errors/error/0/error-message
log 404
log json ${res2}
should not be equal ${res3} ${result}
check_500
[Arguments] ${res2}
${res3}= get json value ${res2} /errors/error/0/error-info
${res4}= Get Regexp Matches ${res3} message=(\\w+ \\w+) 1
${res5}= set variable ${res4[0]}
log 500
log json ${res2}
should be equal ${res5} ${invalid}
check_404!
[Arguments] ${res1} ${res2}
log ${res1}
log json ${res2}
should be equal ${res1} 200
close session
delete all sessions