Getting started with Robot Framework

End-to-end testing may seem like a daunting task, but it doesn’t have to be. There are tools out there that take a lot of the guesswork out of the process. Enter Robot Framework, a modular test automation framework that’s designed to streamline your end-to-end testing.

In this guide, we’ll show you how to set up the framework and write your first test.

What is Robot Framework?

Robot Framework is an end-to-end automation framework used for both robotic process automation and test automation. The framework enjoys widespread adoption, with many companies and testers using it in their software development.

Robot Framework is open-source, free to use, and entirely self-hosted.

The framework uses keyword-based syntax for test cases and can be extended with libraries implemented using Java or Python. Many libraries and tools have been developed separately and add to Robot Framework’s functionality. It’s even possible to use a Python API to create new keywords that help perform functions specific to your program.

Looking at an example in Robot Framework, here’s a test case named sample_test:

*** Settings ***
Library           SeleniumLibrary

*** Variables ***
${SITE URL}    https://www.iana.org/domains/reserved/
${BROWSER}        Firefox

*** Test Cases ***
Clicking Link Works
    Open Browser To Site URL
    Click Link With Text    XN--G6W251D
    Title Should Be     IANA — .測試 Domain Delegation Data
    [Teardown]  Close Browser

*** Keywords ***
Open Browser To Site URL
    Open Browser  ${SITE URL}
    Title Should Be     IANA — IANA-managed Reserved Domains

Click Link With Text
    [Arguments]     ${text}
    Click Link    ${text}

This example test opens the IANA reserved domains page, looks for a link that contains XN--G6W251D in its name, clicks it and then checks whether the page we end up is the one we intended.

We’ll take a look at this in more detail below.

Installing Robot Framework

Robot Framework is not difficult to install, but there are a few steps you’ll need to follow before you’re up and running.

  1. Install Python

Robot Framework is built using the Python language. As such, it should come as no surprise that you need to have Python installed on your device. Visit Python’s website, choose your operating system and download the software. If you happen to be passionate about a particular version of Python, rest assured that Robot Framework will support the latest versions of Python 3.

Once Python has finished downloading, be sure to install it before moving on to the next step. The default installation package comes with pip—the Python package manager and Python documentation. You can customise the installation, but keep in mind that you will need pip for Robot Framework to run.

Before going any further, check that both Python and pip are successfully installed. From a command prompt, run the following:

  • python --version
  • pip --version

On some systems like macOS, python and pip can be aliased to Python 2 executables, so to get the up-to-date Python 3 you might need to run python3 --``version and pip3 --``version respectively.

The command prompt will display the version of the software if successfully installed, and will give an error if they are not.

  1. Install virtualenv

Virtualenv is a tool used to create isolated Python environments. When used, the programme generates a folder containing all the tools a Python project would need.

It’s best to use pip to install virtualenv, just as we used pip to install Robot Framework. If you still have your command prompt open to the Python folder, type “pip install virtualenv”, and you’ll be on your way.

As always, running the following command will verify the install:

virtualenv --version
  1. Install Robot Framework

Now we’ve come to the moment of truth. We’ll need to install Robot Framework on our system.

To do so, open up a command prompt and navigate to your project folder. Once there, run the command pip install robotframework and your device will download and install the framework.

If everything goes according to plan, you’ll see the message “Successfully installed robotframework”. You can verify the installation by running the following command:

robot --version
  1. Find additional libraries

Since Robot Framework can be extended with libraries from Python or Java, this is a good place to grab any additional libraries that you may need. For instance, you can download the Python library from Mailosaur on GitHub.

Writing your first test with Robot Framework

To write our first test that uses a web browser, we’ll need to use pip to install the Selenium library in Robot Framework. This is done with pip install robotframework-seleniumlibrary.

For this example, we’ll use Firefox. To do so, we’ll need to install geckodriver directly from GitHub. You can also use your preferred package manager, which could be Chocolatey on Windows, Homebrew on Mac, or apt on Linux. Chrome users will need to obtain chromedriver instead.

All the keywords that we’ll use for our web tests are provided right in the Selenium Library.

In the example above, we were testing to see whether clicking the link XN--G6W251D on our site URL takes us to a site with the title “IANA — .測試 Domain Delegation Data” on IANA’s Reserved Domains website. The complete test case looks as follows:

*** project_folder/sample_test

*** Settings ***
Library           SeleniumLibrary

*** Variables ***
${SITE URL}    https://www.iana.org/domains/reserved/
${BROWSER}        Firefox

*** Test Cases ***
Clicking Link Works
    Open Browser To Site URL
    Click Link With Text    XN--G6W251D
    Title Should Be     IANA — .測試 Domain Delegation Data
    [Teardown]  Close Browser

*** Keywords ***
Open Browser To Site URL
    Open Browser  ${SITE URL}
    Title Should Be     IANA — IANA-managed Reserved Domains

Click Link With Text
    [Arguments]     ${text}
    Click Link    ${text}

Let’s break the test file down line-by-line. We first need to call upon Library SeleniumLibrary. This directs Robot Framework to use the Selenium library as we’ll be writing web tests. This library contains the keywords we need for the rest of the test:

*** Settings ***
Library           SeleniumLibrary

Next, our variables show us which website we’re testing, and which browser we’re using to go there.

*** Variables ***
${SITE URL}    https://www.iana.org/domains/reserved/
${BROWSER}        Firefox

In the “Test Cases” section, we define the test names for our test. Here it’s just one test: “Clicking Link Works”. We can actually give this test any name we want. Each of the lines inside the test case together describe what the case means.

*** Test Cases ***
Clicking Link Works
    Open Browser To Site URL
    Click Link With Text    XN--G6W251D
    Title Should Be     IANA — .測試 Domain Delegation Data
    [Teardown]  Close Browser

In this case, there are four steps:

  • Open Browser To Site URL is a step that we define below in the Keywords section;
  • Click Link With Text XN--G6W251D is also a step that we define;
  • Title Should Be is a built-in step in the Selenium library;
  • [Teardown] Close Browser is a built-in step from the same library.

Lastly, as mentioned, we use the Keywords section to create two of our own steps:

*** Keywords ***
Open Browser To Site URL
    Open Browser  ${SITE URL}
    Title Should Be     IANA — IANA-managed Reserved Domains

Click Link With Text
    [Arguments]     ${text}
    Click Link    ${text}

To run the test:

robot sample_test
==============================================================================
Sample Test
==============================================================================
Clicking Link Works                                                   | PASS |
------------------------------------------------------------------------------
Sample Test                                                           | PASS |
1 test, 1 passed, 0 failed
==============================================================================
Output:  /Users/r2d2/code/robot-framework-sample/output.xml
Log:     /Users/r2d2/code/robot-framework-sample/log.html
Report:  /Users/r2d2/code/robot-framework-sample/report.html

Robot Framework opens our Firefox browser, navigates to the IANA site, and runs the test. We reach a green results page, telling us that our sample test has been completed successfully. In the case of a failed test, we would see a not-so-nice red screen and would have to correct any errors.

Leverage Mailosaur for email and SMS testing in Robot Framework

Try Mailosaur’s cutting-edge API to leverage Robot Framework’s testing capabilities. Our extensive documentation and friendly support make it easy to get started.

Sign up for a free Mailosaur account today!

Continue reading

Best Android UI testing frameworks in 2021

This article covers the importance of UI test automation and provides recommendations on which frameworks to use to test your Android applications.

Read

Transitioning from Manual Testing to Automated QA

In this article, we examine why automated QA testing is essential to any test plan, and go through the steps to begin your journey with automated QA.

Read