API Automation Testing

API Automation Testing using Robot Framework with Python

Robot Framework WIth Python

Introduction:

In API Automation using Robot Framework, we will be using various python libraries like Request Library, Json Library, Collections, etc. These are various tools that are used to run operations in web applications. 

Prerequisites: 

  • Install Robot Framework  
  • Install Request Library 
  • Add RobotFramework-Requests, RobotFramework-Json and RobotFramework-SeleniumLibrary in the project panel of your IDE. 

Installation:

  • Check for pip version  $ pip –version  >> pip 20.0.2  
  • Install RobotFramework  $ pip install robotframework 
  • Install the request Library using the following command: $ pip install requests 

Syntax:

${response}= get on session   SessionName    URL header=${header} 

${response}= post on session   SessionName URL header=${header} 

${response}= put on session   SessionName URL header=${header} 

${response}= delete on session SessionName URL header=${header} 

Note:

Prior to the most recent upgrade, we used “get request”, “post request”, etc.; but, because of the depreciation of earlier versions, we now use “get on session”, “post on session”, etc.

Create (POST): 

To Send a POST request in Python, we can use ‘post on session’ method. This method accepts multiple parameters like URL, data, Json, arguments and sends a post request to specified URL. 

To send a post request we would require two things: 

  1. URL for service endpoints. 
  2. Object which has the properties needed to be sent. 

Since passing data as a parameter to the post method is optional, we can say that it is similar to the get method. 

Robot Framework - Post

Read (GET):

In same way, we can also use get request by using ‘get on session’ method. Once the requests are made and the data is a small unit, then we validate the response by printing it. But if the data is in large quantity, we can verify by checking its status code, that is asserting and checking if the status code is 200 or not. 

Robot Framework- Get

  Update (PUT):

For update, instead of put request we use ‘put on session’ method. To use this method, we must use the full request body to make changes with respect to “name,” “gender”,” email,” and” status.” 

Here, In the below example, we are updating user/4153 that is been previously created. Once the request is sent the changes are made in the desired JSON body of data. 

Put

Delete:

Deleting can be done by just passing the id we want to delete. Here we do it by using ‘delete on session’ method. Thus, we can further verify with the respective status code which is 204. 

Robot Framework- Delete

Pros:

  • When using request library in Robot framework, API response and behavior is similar to as while using python. 
  • Scrapping data from a website is quite easy. 
  • Using Requests, we can harness response in various forms like text format, binary response, Json response, and raw response. 
  • Keyword driven approach of the Robot framework is easy to understand.

Cons:

  • Slow execution speed compared to other tools like Axios, webdriverIO etc. 
  • While using an access token, operating data with post and put request is comparatively difficult task as it may result in authentication or other types of errors.