Automated testing with RobotFramework — installing in Windows and running our first test

Adrian Voicu
6 min readJul 10, 2020

--

Let us start by explaining the title. By automated testing we must understand exactly what the naming implies. Not as in manual testing, automated testing means that there is no need for human intervention once the test has been created. And when we talk about a “test” we are referring to a series of steps that the machine (the software) has to take, imitating a human action, in order to find out if the final result was the desired one.

This kind of tests can help with regression testing. This means that, every test that was previously done can be run again after new code was implemented, in order to see if the new code is affecting other areas in our program — website in our case.

RobotFramework, as the name implies, is a framework, having a pre-established way of working. What this means is that, if you give it a script having the necessary structure, it will run it and return the results, without having to write a lot of code.

For testing websites, RobotFramework uses Selenium Library, which is a well known library (if not the only one) for testing websites. This library uses browser drivers in order to access directly the browsers.

We will start by installing Python. We will use Python 3. The installment is done by simply running the executable that cand be downloaded from the python website: https://www.python.org/downloads/

After installing, make sure you have Python added to the system variables. So add it to the Path. Now, if everything went fine, you should be able to open a command prompt and run the following line:

python --version

As a result, you should receive the Python version, which in my case is 3.6.4.

Python version returned…

Now that we have Python language installed and the executable is in our system paths, we can install RobotFramework by using the Python installer, “pip”. But before that, let us make sure we have the latest version of “pip” by running the following in the command prompt:

python -m pip install — upgrade pip

Now, after we’ve upgraded “pip”, we will continue with installing RobotFramework:

pip install robotframework

Once installed, we can check if everything went ok by running two commands:

robot — version
rebot — version

We must agree that RobotFramework is not only for testing websites using browsers. For testing websites, RobotFramework needs to use Selenium Library (https://www.selenium.dev/). So, let’s go ahead and install it (https://github.com/robotframework/seleniumlibrary).

The install si done by running the following line:

pip install — upgrade robotframework-seleniumlibrary

Now, once we have Selenium installed, we must offer the library the needed tools in order to test websites, that means the drivers used by browsers to navigate websites and test their components.

We can see the drivers on the Selenium website: https://www.selenium.dev/selenium/docs/api/py/index.html#drivers.

These drivers must be downloaded into an easily accessible directory (C:\webdrivers), and then the directory must be added to the system variables (PATH), the same way that we’ve added the python executable.

Another solution to download all drivers, which I am not really recommending, would be using a Python library that deals with downloading the drivers. The library is named Webdrivermanager and you can find out more about it in here: https://pypi.org/project/webdrivermanager/

So, if you didn’t download the needed drivers you can write the following lines at the command prompt:

pip install webdrivermanager
webdrivermanager chrome firefox edge — linkpath C:/webdrivers

Now that everything is installed, it’s time to do our first tests. Just to check that everything it’s in its place, let us create a directory named “tests”. Now we open a terminal window inside this directory and write:

robot tests

If everything is in place, we should receive an error message:

Error from Selenium. No tests to be run.

Now, let’s create our first test. For starters, we will test that a certain page has a title tag (“<title></title>”). So, let’s create a file named homepage.robot inside our “tests” directory.

We can edit the file using any editor, but I would recommend something like Visual Studio considering the fact that it has plugins that help us with creating the tests (like Robot Framework Intellisense plugin).

A .robot file is generally divided into sections. The first section is the “Settings” one:

*** Settings ***
Documentation Our first test that uses SeleniumLibrary
Library SeleniumLibrary

In the .robot file we can define variables. And what better place than at the start of the file? So let us add the “Variables” section:

*** Variables ***
${URL} https://avenir.ro
${BROWSER} Chrome

Now that we’ve defined the variables, it’s time for our first test:

*** Test Cases ***
Valid Page
Open Browser At Homepage

Our first test will be named “Valid Page”, and the command will be to “Open Browser At Homepage”. This command is in fact a keyword that actually means executing a few Selenium Library commands. Now, that we have talked about “keywords”, let us define them. We do this inside a “Keywords” section:

*** Keywords ***
Open Browser At Homepage
Open Browser ${URL} ${BROWSER}
Title Should Be Boohoohoo

What we’ve done here is define the keyword “Open Browser At Homepage”. As you can see we are using tabs in order to separate blocks of code.

Please, note that in our definition we are using two important terms: “Open Browser” and “Title Should Be”. The two terms are part of along list of terms supported by the Selenium Library. You can see the whole list in the documentation: https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html

Also, it is worth mentioning that the terms accept parameters. The parameters are separated from terms using two white spaces. If we want to use white spaces that we don’t want to be interpreted by the library, we can use “\s” as replacement for space.

Sow what did we write? We asked Selenium Library to open the Chrome browser and the url mentioned in the ${URL} variable, and see if the page title is “Boohoohoo”.

Let’s see the whole file again:

*** Settings ***
Documentation Our first test that uses SeleniumLibrary
Library SeleniumLibrary

*** Variables ***
${URL} https://avenir.ro
${BROWSER} Chrome

*** Test Cases ***
Valid Page
Open Browser At Homepage

*** Keywords ***
Open Browser At Homepage
Open Browser ${URL} ${BROWSER}
Title Should Be Boohoohoo

Now, once we’ve saved the file, we can execute all the tests in the “tests” directory. We do this by opening the command prompt in the parent of the “tests” directory and running:

robot tests

Mentioning a directory as parameter of “robot” command tells the RobotFramework to execute all test files inside that particular directory. Ofcourse we can execute a specific test by mentioning the file directly.

Once we execute the command, RobotFramework, using SeleniumLibrary tells the Chrome browser to open at that particular url. After opening the browser, and once the test is finished, we should see in the command prompt something similar to the following:

Test results returned from RobotFramework

Also we can see that, at the end of the test, the results of the test were saved in a file named report.html, which allows us to easily see all the tests that were ran.

The only problem now is the closing of the browser. For this kind of actions we can use the “Test Teardown” in the “Settings” section:

*** Settings ***
Documentation Our first test that uses SeleniumLibrary
Library SeleniumLibrary
Test Teardown Close Browser

Save it and… that’s it. Now you know how to run your first automation test using RobotsFramework, SeleniumLibrary and Python.

Also read

https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html
https://github.com/robotframework/HowToWriteGoodTestCases/blob/master/HowToWriteGoodTestCases.rst

--

--