Robot Framework — From Zero to Hero — Guide for API Testing
What is Robot Framework?
Robot Framework is an open-source, generic, keyword-driven test automation framework for acceptance testing, test-driven development, and robotic process automation (RPA). It was developed at Nokia and was open-sourced in 2008. It is written in Python but can also be used with Jython (Java) and IronPython (.NET).
PS: Only Python will be covered in this tutorial.
Robot Framework allows testers and developers to create and execute automated tests in a simple, readable, and maintainable way. It uses a tabular test data syntax for defining test cases and test suites, and allows for easy integration with other tools and libraries.
Robot Framework also provides a rich set of built-in libraries for tasks such as interacting with the file system, web browsers, databases, and operating system, as well as allowing for the creation of custom libraries. Its flexibility and extensibility make it a popular choice for test automation in many industries.
Getting started
Getting started with Robot Framework is straightforward. Open up your favorite IDE and create a virtual environment, then head to the terminal section.
Once the environment is set, you should see the terminal commands as the below image.
Now, it is time for us to pip some stuff. Call the below command to install Robot Framework to your Virtual Environment.
pip install robotframework
The first part of the series will be dealing with API testing so we will make use of the requests library. The requests is a python library written by kennethreitz and maintained here.
Luckily, it is not required for us to write our own wrapper functions around this library. robotframework-requests 0.9.4 handles this requirement for us. In order to get this library, we will call our second pip command.
pip install robotframework-requests
Finally, we will install the JSON library since we are aimed to write some tests for APIs. JSON library will help us to fetch the data from response.
pip install robotframework-jsonlibrary
Now, we are ready to start construction of our test framework. The first thing to do is creating the core directories. Create the below directories inside root folder.
- library — holds the custom keywords and libraries
- resources — holds the common keywords for different test suites
- results — holds the test reports and related xmls
- tests — holds our beloved test suites
Our first test suite
Go into tests directory, create a new file as testSuite1.robot. When you are done with this file creation, PyCharm will show you a banner which says Plugins supporting *.robot files found. Click on Install plugins and install below add-ons for easier maintenance of robot files.
Restart the IDE to activate the add-ons. Open up the testSuite1.robot and add the below content. We will be going over the code together.
*** Settings ***
Documentation
... Hello!
... This is an example test suite for testing the requests library keywords along with built-in library keywords
Suite Setup Setup suite
Suite Teardown Teardown suite
Library DateTime
Library RequestsLibrary
Library JSONLibrary
Library ../library/ip_detector.py
Test Timeout 2 minutes
*** Variables ***
${LOCATEAPI} https://ipinfo.io
${CONVERTYODA} https://api.funtranslations.com
*** Test Cases ***
Check geolocation of connection
Locate my ip
Convert message to yoda version
Convert to yoda speech ${location}
*** Keywords ***
Setup suite
Log datetime information
Log Starting the test
Teardown suite
Log datetime information
Log Test execution completed
Log datetime information
${date}= Get Current Date
log ${date}
Locate my ip
${IP}= detect my public ip
Create Session mysession ${LOCATEAPI} verify=true
${response}= GET On Session mysession /${IP}/geo
Status Should Be 200 ${response}
Run keyword if "${response}" != "${EMPTY}" Check Geolocation ${response.json()}
Check Geolocation
[Arguments] ${json}
${location} Get Value From Json ${json} $.city fail_on_empty=${True}
${coordinates}= Get Value From Json ${json} $.loc fail_on_empty=${True}
Log ${coordinates}
Set Suite Variable ${location}
Convert to Yoda Speech
[Arguments] ${location}
${body}= Create Dictionary text=Hello Padawan, you are connecting from ${location[0]}
Create Session yodasession ${CONVERTYODA}
${response}= POST On Session yodasession /translate/yoda.json? data=${body}
${Yodish} Get Value From Json ${response.json()} $.contents.translated fail_on_empty=${True}
${Normal} Get Value From Json ${response.json()} $.contents.text fail_on_empty=${True}
Should Not Be Equal As Strings ${Normal} ${Yodish}
Navigate to your library directory and add the below custom keyword as well.
from requests import get
def detect_my_public_ip():
ip = get('https://api.ipify.org').content.decode('utf8')
return ip
The most important trick of Robot Framework is minding the gap.
- Deniz Sivas
Before going over the code, I would like to underline one point. In RF, there are two separate usage of space character. If space is hit once, it means you are writing a keyword. The whole sentence with single spaces will be interpreted as a keyword. If space is used consecutively twice or more, it means you are defining a variable to a keyword.
*** Keywords ***
Setup suite
Log datetime information
Log Starting the test
In this example code block, “log datetime information” is a keyword. In the second line, log is the keyword and “Starting the test” is the input text to that keyword. Mind the gap!
The *** <txt> *** syntax marks the sections of the test suite. The explanation of each section is below.
*** Settings *** — For defining resources, configurations and variables.
*** Variables *** — For defining suite variables.
*** Test Cases *** — For writing our test cases.
*** Keywords *** — For defining custom keywords.
*** Settings ***
Documentation is self explanatory, it is being used for comments, explanations about the tests, requirement information etc.
Suite Setup/Teardown are two keywords which are desired to be run at the start of the suite and at the end of the suite respectively.
Library keyword is used to define the Built-in libraries that are to being used for the suite. In our example we are using DateTime library and JSON library.
*** Variables ***
The syntax to define a variable in RF is ${str}. So in our code, we defined two sets of APIs. One belongs to HTTP GET requests and the other one belongs to HTTP POST requests. Observe that only the endpoints are defined here. The parameters will be provided in keywords.
*** Test Cases ***
Test cases are named in the first line and then the keywords associated with them are defined after a <tab>.
Good Practice: Try to stick with a decent abstraction level. Avoid too much detail in your test cases.
*** Keywords ***
This is the most challenging section of our test suite. All keywords that are aimed to be used need to be defined in this section.
Let’s break the most complicated ones down together to understand it better. Starting with Locate my ip, this keyword has 5 steps. The first step runs the custom keyword we defined as “detect my public ip” by placing the ip_detector.py under library directory. Observe that the name of the script has nothing to do with the keyword’s name. Also observe that when we defined the function we used underscores but the keyword was called without those. This is because Robot Framework converts underscores to spaces automatically when the keyword is called.
The second step creates a session to the URI we defined in ${LOCATEAPI} variable. The third step requests HTTP GET method to the session we created. The second argument for this call is the endpoint, that’s why /{$IP}/geo was added. Fourth step checks if the request was successfull or not and the last step runs another keyword named “Check Geolocation” if the ${response} of the GET request is not empty. The ${response.json()} is used to convert the response to JSON format for Check Geolocation usage.
Check Geolocation keyword gets the argument {json} and fetchs the $.city value from ${response.json()}. Likewise, it reads and fetches the coordinate of the city where the user connects. The last section defines the location as a suite variable which enables us to use the same variable for another keyword. In our case, Convert to Yoda Speech.
The Convert to Yoda Speech keyword gets the location argument, creates a dictionary by using the location information and a generic greeting sentence. Creates a session to a POST API defined with {CONVERTYODA}. Like previous GET call, this time we call it with POST method and a JSON is provided with the data=${body}. The API gets this text information and is expected to return a new sentence which is in Master Yoda style. As a final check, we read the Yodish version of the sentence and the normal version of the sentence and compare them. If everything goes well, they should not be identical.
For the keywords usage, it is not possible to explain them here so I will share the respective documentation.
Robot Framework BuiltIn Library
Robot Framework DateTime Library
Robot Framework Requests Library
Robot Framework JSON Library
It is time to run and observe the results by yourself.
To run the test suite, go to your terminal and run the below command.
robot --outputdir ../results --timestampoutputs --name "Robot Tutorial Report" testSuite1.robot
In this call, — outputdir defines the desired directory for the reports to be placed. — timestampoutputs writes the current datetime to the name of the report and — name renames the report to the input. Once the testruns are finished, log.html, report.html and output.xml files should be visible under results directory.
In order to observe the results, just open the html files.
Self Study Assignment:
I wrote some sections with “bad practice”. Read the best practices link that I shared below and rewrite the test cases, keywords and test steps as mentioned there.
Further Reading:
How to write good test cases
Robot Framework Best Practices
Robot Framework QuickStart Guide