(My) Notes about Robot Framework
QA Automation with Python
❓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.
🛠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.
- Introduction
- What are keywords
- Section Description
- Structure of a Project
- Installation
- How to write a Test
- 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.
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.
- Without indentation, we can set the keyword. A keyword can be a word or a sentence.
- With Indentation we define the different steps, here we can use any keyword that we will normally use in the TestCase.
In the example above the keyword will be Search for keyword
and what this keyword does is:
- Wait until the element
Search input
is visible. - Input the text
monster hunter: world
in the search bar. - 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
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 justDefine mobile broswer
Search for Keyword
it will search for a special keyword and execute the searchScroll 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.
- 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. - 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.
Structure of the project
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:
Installation
Using PIP
- install Robot framework
pip3 install robotframework
(Optional) Install Selenium libraries for Web UI and mobile automation.
pip install robotframework-seleniumlibrary
(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.
Then we select the plugins section.
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:
- My plugin was version IntelliBot 0.10.143.381 (Pycharm plugin marketplace)
- On Intellibot GitHub the latest version is 0.13.191.8026
So, how to solve it
- I uninstall the old plugin.
- Download the release version on Github (https://github.com/lte2000/intellibot/releases/tag/v0.13.191.8026).
- 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:
- Open the URL on the browser
- 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.
- Create the
.robot
file with the following sessions.
- Settings
- Variables
- Test Case
2. Import the libraries and create the variable.
3. Name the test case.
4. Now below the test case name and indented create the test case steps.
How to run it
To run the scrips in Pycharm we can use the terminal.
In the most basic version we have:
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.
- Create a new folder, you can set any name, for this case
Results
- We add the flag
-d
to the command followed by the location of the folder.
and example:
How to run just one test case
To run one single .robot
file we just need to provide the direction to that file.
With this command we just the test case VideoSearch_Android.robot
will be executed.
Potential initial errors and how to solve them.
I ran into issues running this framework
- 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 putgeckodriver
andChromedriver
on 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
- 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
- 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.