Trying to create a test case in robot framework .
We have couple of rest apis which returns us Json as result . To invoke such a case, we used the following code in rest2.py
def restJSON():
r = requests.get("http://httpbin.org/get")
# print "Code" , r.status_code
# print "Text " , r.text
return r.json()
We stored the json inside a file output. And we have written robot test case to evaluate json comparison as below
*** Settings ***
Library rest2.py
Library OperatingSystem
*** Test Cases ***
Example that calls a python keyword
${result}= restJSON
${json}= Get file output
Should be equal ${result} ${json}
But when we run the pybot test case we are getting error saying both json are not same.
--------------------------------
pybot testSuite.txt
--------------------------------
==============================================================================
testSuite
==============================================================================
Example that calls a python keyword | FAIL |
{u'origin': u'10.252.30.94, 69.241.25.16', u'headers': {u'Via': u'1.1 localhost (squid/3.1.14)', u'Accept-Encoding': u'gzip, deflate, compress', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.2.1 CPython/2.7.6 Linux/3.16.0-30-generic', u'Host': u'httpbin.org', u'Cache-Control': u'max-age=259200'}, u'args': {}, u'url': u'http://httpbin.org/get'} != {u'origin': u'10.252.30.94, 69.241.25.16', u'headers': {u'Via': u'1.1 localhost (squid/3.1.14)', u'Accept-Encoding': u'gzip, deflate, compress', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.2.1 CPython/2.7.6 Linux/3.16.0-30-generic', u'Host': u'httpbin.org', u'Cache-Control': u'max-age=259200'}, u'args': {}, u'url': u'http://httpbin.org/get'}
------------------------------------------------------------------------------
testSuite | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
The json file are same. But still its Failing test case saying both aren't same. Is this the correct way to compare , or do we have any other method to do it in robot framework.
Why not using the already existing libraries:
Example code:
*** Settings ***
Library RequestsLibrary
Library Collections
Library XML use_lxml=True
Force Tags REST
*** Variables ***
${SERVICE_ROOT} http://ip.jsontest.com/
${SERVICE_NAME} testing
*** Keywords ***
json_property_should_equal
[Arguments] ${json} ${property} ${value_expected}
${value_found} = Get From Dictionary ${json} ${property}
${error_message} = Catenate SEPARATOR= Expected value for property " ${property} " was " ${value_expected} " but found " ${value_found} "
Should Be Equal As Strings ${value_found} ${value_expected} ${error_message} values=false
*** Test Cases ***
Example REST JSON
Create session ${SERVICE_NAME} ${SERVICE_ROOT}
${headers}= Create Dictionary Content-Type=application/json Accept=application/json
${result}= Get Request ${SERVICE_NAME} ${SERVICE_ROOT} headers=${headers}
Should Be Equal ${result.status_code} ${200}
Log ${result.content}
${json}= To Json ${result.content}
${pp}= To Json ${result.content} pretty_print=True
Log ${pp}
json_property_should_equal ${json} ip [Your_IP]
Remember to install all needed libraries:
pip install robotframework-requests
pip install requests
What is the response when you do:
Should Be Equal As Strings ${result} ${json}
I find your method for achieving what you want a bit strange, if I were you I would be using the http-library or requests library
https://github.com/peritus/robotframework-httplibrary https://github.com/bulkan/robotframework-requests
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With