Automate Test API with Robot Framework.

KOPKUN SAEYANG
Arcadia Software Development
3 min readDec 11, 2018

What is Robot Framework??

Robot Framework is a test automation framework that is a open source and application independent. There are so many library for using to test, but for this section we will focus on API automation test.

What do you need to have before doing API automate test?

  1. Python 2.7.14
  2. Setup Tool
  3. Pip
  4. Sublime

Here is the links to guide you to install.

  1. For robot framework (https://medium.com/@betiranasak/การติดตั้ง-robot-framework-ride-สำหรับ-window-65fc8448b834)
  2. For Sublime (http://www.howtoautomate.in.th/how-to-config-sublime-for-testing-robotframework/)

After you have done all installation, Let start for simple API automation testing.

Robot have 5 main parts. There are Settings, Variables, Test Cases, and Keyword.

  1. Settings : This part is about the Library that you use.
  2. Variable : This part is about the variable that you use to implement.
  3. Test Cases : This part is the test case, you can test your case here by calling function from keyword.
  4. Keyword : This part is like a function or method and will be used in Test Case.

The script below is the example of using automation test my login function.

There are two Libraries that mostly use to test API. You can download and install it in case you don’t have these two Libraries. If you don’t use these two libraries you might not be able to test API, not so sure may be there are other libs for testing API too.

Library          HttpLibrary.HTTP
Library RequestsLibrary

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

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

For Keywords part which is the function to describe all your test logic.This code below is about creating the Json object that use in request body.

    \     ${json_string}=    catenate
\ ... {
\ ... "LoginId": ${username},
\ ... "Password": ${password},
\ ... }

Next is header, its like header in Postman. Use Create Dictionary to create data storage to keep value, and assign content-type as your type. Is this example I use application/json as a sample below.

For the URL, select your method and url in ${resp}. This variable will store the response value. You have to select which method you will use (Post, Get, etc.) and put your body json and header in this.

\   &{header}=    Create Dictionary    Content-Type=application/json
\ CreateSession tmd ${url}
\ ${resp}= Post Request tmd api/Login/Login
\ ... data=${json_string} headers=${header}

Then write a keyword to verify the response with code status. In this example I verify it with code status 200.

Response Status should be Success 200
[Arguments] ${status}
Should Be Equal As Strings ${status} 200

How to Execute.

After done scripting, go to command prompt and use this command to execute your script. The first path is for the log, it will auto generate that file “log” which about the log and you can see the result on it. The second path is the path of your script.

-d D:\AutoTestAPT\log D:\AutoTestAPI\apiTestExample.robot

After finish execution,and if it executed success, you will get the result show “PASS”.

Result : So you can view report and log. If there are any errors it will show on this file and show you the cause and the green icon will be red color.

--

--