Preface

We all are familiar with a lot of open source tools out there in the market, that are useful on our way while testing the JSON APIs. viz-Postman, SoapUI, JMeter, Rest-Assured and many more when you search on Google for API Testing Tools”. One more to the list is the Robot test framework.

One more to the list is Robot framework.-As said in my earlier blogs (Robot framework – an unglorified hero part 1, and part 2) about the Robot framework being an unsung hero! In this blog am going to let you know about an outline on how to use Robo framework for API testing, this being a  tool that I chose and have been using it off late to test the APIs. For those who are using Robot framework for your UI tests already, now can use it for conducting your API tests too!

Robot Framework and API Testing

There are RequestsLibrary, JSON, HttpLibrary.HTTP libraries that are really simple to use. We need to import these libraries into our tests, under the Settings section. So enough with the setup, let’s make some requests.

HttpLibrary.HTTP: It is the Http library for Robot Framework that works with JSON and the JSON related keywords use JSON Pointer. If you want to test API this library is a recommendation.

RequestsLibrary: It is a Robot Framework test library that uses the Request HTTP client.

Let me start with an API that’s using the OAUTH2 authentication. The authentication for an endpoint under test is through OAUTH2. Therefore, when OAUTH2 authentication credentials being supplied, a token is generated. The consequent calls to the APÍ need to have the token included as a ‘bearer’ in the ‘authorization’ header of the HTTP requests. And this is how it’s done:

We need to create a session after assigning the authorization=${token}, much prior to getting a request.

In the above example, we load in the RequestsLibrary using the library keyword. As said earlier, to be able to distinguish HTTP requests to different hosts and for ease of creation of test cases, you need to create a Session.

We’ll create a session, through create session keyword, internally this will create a request.

The create session keyword accepts two arguments:

  1. a base URL of the server. [Select your method and URL in ${resp}].
  2. an alias name for the Robot Framework to identify the session and the headers.

After creating a session, we fetch this bearer token and then pass it on to the subsequent endpoints/calls. When we create a Session, we can send any of the following get, post, put, patch, options, delete, and head requests.

We use create dictionary to create data storage to keep value and assign content-type as your type.

Write up your keywords and verify the response code, status accordingly. Refer keywords from Robot Framework Keywords Library – official Robot framework website.

Once the tests are run, the test results will be available in the form of an HTML file. The response is an HTML file that is saved locally in a folder called results, that can be found in the root of our project and looks like this:

As seen above the tests were successful and that we got the response from the server of the client details.

Conclusion

Robot Framework is a quite helpful and simple tool for automated testing, that speeds up the process of testing. A vast number of libraries can enable the framework to test RESTful JSON APIs. It is very similar to other common open source API testing tools. However, if you already use Robot Framework for your every day automated testing why not just import a library and test your APIs with it? 😊

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*