(My) Notes about Robot Framework

QA Automation with Python

Victor Fernandez
10 min readJul 16, 2022
Photo by Jason Leung on Unsplash

❓The Problem

I need something which allows me to present a human-readable software test (Test cases) to another non-technical member of my team while maintaining the flexibility and possibilities to automate further with Continues delivery or continuous integration systems.

💡The Solution

I need a Software automation tool that I can use to abstract the complexity and present the test cases as a list of steps written in plain English. Additionally, this tool should allow further integration with CI tools such as Jenkins.

Example of a human-readable test case

🛠The Implementation

Robot Framework is an automation framework that allows the user to express test cases as a list of steps written in plain English but is robust enough that allows the implementation of more complex logic using python.

  1. Introduction
  2. What are keywords
  3. Section Description
  4. Structure of a Project
  5. Installation
  6. How to write a Test
  7. Potential initial errors and how to solve them.

Introduction

Robot Framework is open source automation framework. It is open and extensible. Robot Framework has an easy syntax, utilizing human-readable keywords. Its capabilities can be extended by libraries implemented with Python or Java.

What are keywords in Robot Framework?

The robot framework uses the concept of keywords, these are words or sentences that tell the platform to perform specific actions, for example Open browser is a keyword that tells Robot Framework to open a new browser.

Now if we pair this keyword with a value that must be separated from the keyword by at least 2 spaces, we are telling the framework to execute the keyword using the parameter passed.

Open browser     <https://www.google.come>

In this case, we are telling the framework to open a new browser and go to http://www.google.com

How many keywords?

The framework has some built-in keywords already available and can be used without any extra steps, however by using libraries we can have access to more specialized keywords. For example SeleniumLibrary allow users to interact with the web UI and provide several keywords that facilitate that interaction.

The documentation will be the first palace to go, although some IDE will provide some autocompletion in most cases search in the documentation will be more accurate.

Section Description.

The main file in the robot framework will be the test case, these files will contain the steps (keywords) the framework needs to automate or perform.

The instructions and almost everything in the framework will be stored in files with the extension .robot

Within the .robot the file we have sections, these sections are surrounded by the ***

The sections are:

  • *** Settings *** This section can contain documentation about the test suite, Libraries use during the test, Resources to be used, and the setup and teardown of the test and test suite.
  • *** Variables *** Any variable to be used in the script
  • *** Test Cases *** define the documentation for the test and the test steps.
  • *** Keywords ** this is optional, it can be used to abstract some complexity, in other words, can be a container that holds complex steps or keywords, example: click search button can be a keyword that contains steps like wait until the search button is visible, click in the search bar, clean the search bar, input new word to search. now in the *** Test Case *** section, we can use the keyword click search button making the test case more readable and hiding the complexity.

☝🏾 On Pycharm in the file .robot we don't need to input the *** we just need to put the beginning of the word and the autocomplete function will give us the option to add the section

*** Settings ***

The way to add elements to this section is by adding the keyword, for example, Library follow by two spaces and SeleniumLibrary this will load the library and the selenium keywords

Common keywords to use in this section are:

  • Libraries: Loads a built-in, third-party, or user-made library
  • Resources: loads resources, this resource can be common user-defined keywords that are common to several tests.

We can see this section as the space to import all the tools need it to work with the robot framework, and it is one of two required sections to create a test case.

*** Variable ***

Here we can store a variable to be used in the test case, there should be two spaces between the name of the variable and the value.

Definition of a variable
Example of variables

This area is useful to store locators when working with test automation of web UI elements.

*** Keywords ***

Here we can create subroutines or steps that can be called later using a specific keyword. It helps to keep the Test case body smaller. Additionally, I can provide the opportunity to abstract complexity.

  1. Without indentation, we can set the keyword. A keyword can be a word or a sentence.
  2. With Indentation we define the different steps, here we can use any keyword that we will normally use in the TestCase.
Example of a keyword

In the example above the keyword will be Search for keyword and what this keyword does is:

  1. Wait until the element Search input is visible.
  2. Input the text monster hunter: world in the search bar.
  3. Please enter to perform the search.

☝🏾 A resource file is another .robot file, these files are normally store in a folder called Resources and they are used to store keywords that are common in several test cases or suites.

Here is an example of keywords use to abstract complexity

Keywords use to abstract complexity
  1. Define mobile broswer is telling the framework to launch a browser with special conditions, this will be confusing for a non-technical person so we can abstract this complexity and in the test case we will have just Define mobile broswer
  2. Search for Keyword it will search for a special keyword and execute the search
  3. Scroll down It will use the tab key from the keyboard to move between elements, in this particular project the elements are stacked vertically so using the tab several times will end creating the appearance of scrolling down

*** Test Case ***

Here we provide the details from the test case, this will use keywords to define the steps. the main idea is to have a small number of steps with keywords that describe what is done not how is done.

🔥 There are built-in libraries, third-party libraries, and customer libraries, the latter meaning that we can build our own libraries to expand the reach of the Robot framework.

  1. The first line non-indented will be the test name. we can have several tests in a single .robot file if their names are not indented and other subsequent steps are.
  2. The next line must be indented and will contain the steps for the test case, here we can use the keyword store in resources or defined on the section *** Keywords *** of the same file.
Example of a test case

Structure of the project

File structure

RobotFramework is flexible with the structure, but there is a suggested structure, below implementation of that suggested structure:

  • Libraries: Here we store any third-party libraries designed or used in the project.
  • Resources: This directory will contain the resources to be used in the test, this resource can be page objects describing the screen or target of the test, or extra functions that will help in the execution of the test.
  • Result: It will contain the results of the test, there are three files in the result folder log.html , output.xml , report.html
  • Tests: This directory is where we will have more flexibility, we can divide the test by the products, but the type of test (performance test, sanity test, functional test, etc) or we can divide it in a way specific to the team using the test

We can use a structure like <Tests>/<Website>/<type of test> to organize the different types of tests.

Results

Once we run the robot framework test we will have an output of three files:

  • report.html: Higher level test report. Here we will have a page with the results and some visual indication of the status of the test (failed or passed)
  • log.html: Detailed test execution log.
  • output.xml: Results in machine-readable XML format.

Report example:

Left a successful Test Case and to the Right a failed Test Case

Installation

Using PIP

  1. install Robot framework
pip3 install robotframework
installing Robot framework

(Optional) Install Selenium libraries for Web UI and mobile automation.

pip install robotframework-seleniumlibrary
installing selenium library for robot framework

(Optional but Recommended) Install the intellibot plugin

🔥 For this, we need to have Pycharm already installed.

We need to go to the marketplace.

One way to go is by clicking in the lower-left corner of the IDE where the version of python is displayed, and selecting interpreter settings.

Interpreter Settings

Then we select the plugins section.

Marketplace

now we can search for intellibot

After the installation, we need to restart the IDE.

Troubleshooting

🔥 I had some issues with the syntax highlighting when I launch the IDE, I got some reports of errors.

I found the following:

  1. My plugin was version IntelliBot 0.10.143.381 (Pycharm plugin marketplace)
  2. On Intellibot GitHub the latest version is 0.13.191.8026

So, how to solve it

  1. I uninstall the old plugin.
  2. Download the release version on Github (https://github.com/lte2000/intellibot/releases/tag/v0.13.191.8026).
  3. Install the new version

Now everything is working, no more error notifications and no more problems with the syntax highlight.

How to write a Test

An Example

As an example we have a basic test case:

☝🏾 Open a specific URL and later proceed to close it

so we can say the steps for the test case will be:

  1. Open the URL on the browser
  2. Close the browser

now what we need:

  • We are talking about web UI and navigation so we need a library for that, in this case, we will use the SeleniumLibrary
  • We will need 2 steps, one to open the browser and the other to close it.
  • (Optional) we can store the URL of the website in a variable.

The implementation of the test case.

  1. Create the .robot file with the following sessions.
  • Settings
  • Variables
  • Test Case
Section need it

2. Import the libraries and create the variable.

Adding libraries and variables

3. Name the test case.

adding a name to the test case

4. Now below the test case name and indented create the test case steps.

Adding steps [Teardown] is to ensure the browser is closed and resources release

How to run it

To run the scrips in Pycharm we can use the terminal.

In the most basic version we have:

Basic run command

This will run all the .robot files in that folder and output the results at the root of the folder.

Set a result folder

The best way to organize the results is to send them to a specific folder and the robot framework allows us to do just that.

  1. Create a new folder, you can set any name, for this case Results
  2. We add the flag -d to the command followed by the location of the folder.
Run command with a specific destination.

and example:

For example, run the whole test suite and deliver results to result folder.
Run command sending results to result folder.

How to run just one test case

To run one single .robot file we just need to provide the direction to that file.

Running just one Test Case

With this command we just the test case VideoSearch_Android.robot will be executed.

Robot Framework running

Potential initial errors and how to solve them.

I ran into issues running this framework

  1. Error with the geckodriver andChromedriver I got a message which said they are not in the PATH, I tried to add them to the PATH, but it didn’t work. So the next option however not optimal is to put geckodriver andChromedriveron the python script folder, in the virtual environment directory.

2. SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in the default location, no ‘moz:firefoxOptions.binary’ for this error, I just need to install firefox.

Final Thoughts

  1. Robot Framework is easy to understand and fast to deploy, there are some issues with the initial setup but nothing that can be solved with a few minutes of research
  2. Because of the abstraction of complexity, it can be perfect for teams with mixed levels of skills, those members with a high level of skills can create the libraries and keywords and those with a beginning level of skills can use those keywords to automate the test cases.

Victor Fernandez

--

--

Victor Fernandez

I’m Victor, I’m a Field Application Engineer for a CCTV manufacturer. I love Raspberry Pi, Python, and Microcontrollers and I write about my personal projects.