Automation of REST API in Robot Framework

The rest API's can be mainly automated by using the following libraries such as:

  • Request Library
  • HTTP Library

We will be covering the HTTP library since it has a number of keywords. There are some prerequisites to before installing the HTTP library, if you install the HTTP library directly, you won't have the following things, so it is better to install each one by using the pip command.

InstallWebTestby using the below command in the command prompt

pip install WebTest

successfully-installed-webtest

Install Webob by using the below command

pip install WebTest

Install Jsonpointer by using the following command

pip install Jsonpointer

successfully-installed-jsonpointer

Install beautifulsoup by using the following command

pip install beautifulsoup

successfully-installed-beautiful-soup

Install HTTP Library by using the below command

pip install robotframework-httplibrary

successfully-installed-http-library

Automation Testing Basics

HTTP Library Keywords in Robot Framework

The complete documentation of HTTP Library is available at http://peritus.github.io/robotframework-httplibrary/HttpLibrary.html, Let us discuss some of the frequently used HTTP libraries as follow:

Go to Ride editor and create a new test suite as RestAPITestCases

new-test-case-as-rest-api

Create a new test case as Get_Request

get-request-test-case

Import the HttpLibrary.HTTP into the new test suite.

importing-http-library

We have successfully imported HTTP library, our first keyword is to Create HTTP Context . It sets the HTTP host to use for future requests. You must call this before issuing any HTTP requests. The host is the name of the host, optionally with port (e.g. 'google.com' or 'localhost:5984') scheme the protocol scheme to use. Valid values are http, https.

Go back to the test case and access the keyword HTTPLibrary.Create http Context and the parameter(https://dummy.restapiexample.com/) as shown below.

get-request-test-case-with-httplibrary-keywordac

Select the test case and run. The Log report is as shown below:

log-report-of-create-http-context

The next keyword is Get Issues an HTTP GET request. Go to the test case and access the keyword HttpLibrary.HTTP.GET and the corresponding parameters as shown below.

get-keyword-with-get-request-test-case

The Response Status Code Should Equal keyword fails if the response status code of the previous request was not the specified one.

test-case-with-response-should-equal-keyword

The Response Status Code Should Not Equal Fails if the response status code of the previous request is equal to the one specified.

test-case-with-response-should-not-be-equal

The Get Response Status keyword Returns the response status line (e.g. 200 OK or 404 Not found)

This is not the response status code (e.g. 200), but the entire first line of the response. To check for status code, use the following:

${status}= Get Response Status

Should Start With 200 ${status}

test-case-with-get-response-status

And the Log report is:

log-report-of-get-response-status-robot-framework

The Log Response Status keyword Logs the response status line Specify log_level (default: "INFO") to set the log level.

log-response-status-keyword

And the log report is:

log-report-of-log-response-stattus-robot-framework

The Post request Issues an HTTP POST request. URL is the URL relative to the server root. Create a new test case PostRestAPI and set the HTTP to create context and then access the post keyword as shown below.

post-rest-api-test-case-robot-framework

Select the test case and run, the log report is as shown below:

post-request-api-log-report-robot-framework

To verify whether the Nextgen120employee name has been generated or not, go to postman app and send the request, you can see the employee name as shown below

post-man-created-nextgenx120-robot-framework

The Get Response Body keyword get the response body, go back to the Get_Rquest test case and access the keyword as shown below.

get-response-body-test-case

As we can see in the below image the response is very limited and hence to overcome this problem we can use the keyword Show response body in Browser

log-report-of-get-response-body

The show response body in browser will open the last request in our default browser as shown below.

show-in-brwoser-response

The Response should contain keyword Fails if the response body does not contain should_contain. The id 1 has existed and hence the below test case has passed.

response-should-contain-keyword

If you don't want to run any keyword, just comment as shown above, so that the keyword won't execute

The Log Response Header Logs the response headers, line by line. Specify log_level (default: INFO) to set the log level.

log-response-header

And the log report is:

log-response-header-report-robot-framework

The Response Should have Header keyword Fails if the response does not have a header named header_name.

response-should-have-header

For more HTTP keywords, navigate to http://peritus.github.io/robotframework-httplibrary/HttpLibrary.html.

The Put keyword Issues an HTTP PUT request. Create a new test case as PutRestAPI and then access the keywords as shown below. Here I am updating the salary of the employee with id 54193

put-test-case

If you check the employee id in the postman you can see that the salary has been updated

postman-has-updated-salary-for-54193-robot-framework

The Delete Request keyword Issues an HTTP DELETE request, Go to ride editor and create a new test case and access the keywords and in this test case, I am going to delete the employee id 54193 as shown below.

delete-test-case

Select the test case and run, the given id has been deleted. To verify whether the given id has been deleted or not, go to the postman and send the request and then search for the id.

deleted-item-in-the-postman-robot-framework

If you observe the above image, when we search for the id54193 in the postman application, it says no result found and hence the id has been deleted successfully.

The Set JSON Value Set the target node of the JSON document json_string specified by JSON Pointer json_pointer to json_value. Go back to the ride editor and create a new test case JsonstringExample and access the keywords as shown below.

json-string-example-test-case

The log report is as shown below:

log-report-of-set-request-body

The name, age, and salary have been updated. To verify go to the postman and send a request you will find the employee details with name Nextgen111 and age is 11 and the salary is 1231.

json-string-postman



Basics of CSS Selector

End to End Automation of Rest API in Robot Framework

Test Case Name Test Steps Description Expected Result
Test Case_end-to-end Step1 Send Get request with the below URL-dummy.restapiexample.com/api/v1/employees The should send back a response containing a record of all the employees
Step2

Issue the Command to Get Response Status code

The Response Status Code should be 200
Step3 Get the Response Header Verify the Response contains a header connection, and the value is keep-alive
Step4 Log the Response Body The Response Body will Be Logged into the console
Step5 Set Request Body to add an employee with name - "NextGenE2E", age - "25", and salary - "12345" "The below JSON will be formed- {

""name"": ""NextGenE2E"",

""salary"": ""12345"",

""age"": ""25"",

}"

Step6 "Send a POST request to the following - dummy.restapiexample.com/api/v1/create and the payload - {

""name"": ""NextGenE2E"",

""salary"": ""12345"",

""age"": ""25"",

}"

The Record will be created
Step7 Get Response Status Code Response Status code Should be 200
Step8 Send a Get Request to Retrieve all employee records Verify the Response contains a record for employee - "NextGenE2E"
Step9 Send a PUT request to update the age for "NextGenE2E" to 30 The Record will be updated
Step10 Get Response Status code Response Status code Should be 200
Step11 Send GET request to retrieve all employee records Verify the age for employee "NextGenE2E" is 30 in the response
Step12 Send a DELETE request to delete the record for employee "NextGenE2E" The employee record will be deleted
Step13 Get Response Status Code Response Status Code should be 200

Go to the ride editor and create a new test case EndtoEndTC and then automate the test case for rest API

Below are the test case codes, create a respective test case and run

*** Settings ***
Library           HttpLibrary.HTTP

*** Test Cases ***
GetRequest
    HttpLibrary.HTTP.Create Http Context    dummy.restapiexample.com    http
    HttpLibrary.HTTP.GET    /api/v1/employees
    Response Status Code Should Equal    200
    Response Status Code Should Not Equal    401
    ${a1}    Get Response Status
    Log To Console    ${a1}
    Log Response Status
    ${a2}    HttpLibrary.HTTP.Get Response Body
    Log To Console    ${a2}
    Comment    Show Response Body In Browser
    Response Body Should Contain    9623
    Log Response Headers
    Response Should Have Header    connection
    Response Should Not Have Header    json
    Response Header Should Equal    connection    keep-alive
    Response Header Should Not Equal    connection    keep-dead

PostRestAPI
    HttpLibrary.HTTP.Create Http Context    dummy.restapiexample.com    http
    ${Body}    Catenate    {"salary":"1234","age":"12","name":"Nextgenx120"}
    log    ${Body}
    Set Request Body    ${Body}
    HttpLibrary.HTTP.POST    /api/v1/create

PUTRestAPI
    HttpLibrary.HTTP.Create Http Context    dummy.restapiexample.com    http
    ${Body}    Catenate    {"salary":"12345","age":"12","name":"Nextgenx120"}
    log    ${Body}
    Set Request Body    ${Body}
    PUT    /api/v1/update/22848

DeleteRestAPI
    HttpLibrary.HTTP.Create Http Context    dummy.restapiexample.com    http
    DELETE    /api/v1/delete/22848

JSONSTRINGExample
    HttpLibrary.HTTP.Create Http Context    dummy.restapiexample.com    http
    ${body}    Set Json Value    {"name":"name","age":"age","salary":"salary"}    /name    "Nextgen111"
    ${body}    Set Json Value    ${body}    /age    11
    ${body}    Set Json Value    ${body}    /salary    1231
    Log To Console    ${body}
    Set Request Body    ${body}
    POST    /api/v1/create

EndtoEndTC
    HttpLibrary.HTTP.Create Http Context    dummy.restapiexample.com    http
    HttpLibrary.HTTP.GET    /api/v1/employees
    ${body}    Get Response Body
    HttpLibrary.HTTP.Response Status Code Should Equal    200
    Log Response Headers
    Response Header Should Equal    connection    keep-alive
    ${Body}    Catenate    {"salary":"1234","age":"25","name":"NextgenE2E"}
    log    ${Body}
    Comment    Set Request Body    ${Body}
    Comment    HttpLibrary.HTTP.POST    /api/v1/create
    Log Response Status
    GET    /api/v1/employees
    ${BODY1}    Get Response Body
    Response Body Should Contain    NextgenE2E
    ${Body2}    Catenate    {"salary":"1234","age":"30","name":"NextgenE2E"}
    Set Request Body    ${Body2}
    HttpLibrary.HTTP.PUT    /api/v1/update/22852
    Log Response Status
    GET    /api/v1/employees
    Log Response Body
    Response Body Should Contain    "employee_age":"30"

Problem Faced:

When I run the end to end automation test case from 5th step, I am getting a cookie error.

HttpLibrary.HTTP.Create Http Context dummy.restapiexample.com http

HttpLibrary.HTTP.GET /api/v1/employees

${body} Get Response Body

HttpLibrary.HTTP.Response Status Code Should Equal 200

Log Response Headers

${Body} Catenate {"salary":"1234","age":"25","name":"NextgenE2E"}

Log ${Body}

Set Request Body ${Body}

HttpLibrary.HTTP.POST /api/v1/create

And the error is:

CookieError: Illegal key value: active_template::133674

Developer Tools in Robot Framework

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions