Robot Framework

The Robot Framework is a generic open-source test automation framework that allows users to easily create and execute automated tests. It provides a simple and easy-to-understand syntax for writing test cases. The framework is particularly well-suited for acceptance testing, behavior-driven development (BDD), and acceptance test-driven development (ATDD). The framework is written in Python and supports Python as its scripting languages for user-defined extensions.

This chapter is intended as a brief introduction only, and full documentation can be found at https://robotframework.org/.

Overview

Key features of the Robot Framework include:

  • Human-Readable Syntax: Robot Framework’s syntax is easy to read and understand. Test cases are written in a tabular format using plain text.

  • Keyword-Driven Approach: The framework supports a modular approach to test automation. Test cases are composed of reusable keywords that represent specific actions or verifications. Keywords can be written in Python, Java, or other languages, allowing for flexibility and customization.

  • Test data abstraction: The framework allows you to separate the test logic from the actual test data using data-driven testing. Test data can be stored in external files like spreadsheets or databases, enabling easier maintenance and reusability.

  • Built-In Libraries and Extensibility: Robot Framework can be extended through libraries and plugins, i.e., you can use custom libraries, keywords, and modules to suit your testing needs. A collection of standard libraries is available to cover basic functionality like file operations, network communication, web testing, database interactions, and more.

  • Reporting and Logging: Robot Framework provides detailed reports and logs after test execution, including information about the passed and failed test cases, making it easier to analyze the test results.

Robot Framework can be integrated with CI/CD tools like Jenkins, Travis CI, and others to automate the testing process as part of the software development pipeline.

Libraries

The Robot Framework comes with a set of predefined keywords that are available as part of its standard libraries providing basic functionalities that can be used in your test cases without having to implement yourself. Here are some of the commonly used standard libraries and their associated keywords:

  • The BuiltIn Library provides fundamental keywords that are essential for writing test cases, e.g.,

    • Log logs a message to the test output.

    • Should Be Equal ssserts that two values are equal.

    • Run Keyword If executes a keyword conditionally based on a given condition.

  • The SSHLibrary allows top open and close SSH connections and execute commands, e.g.,

    • Open Connection opens a SSH connection to a remote device.

    • Execute Command executes a command on the remote device.

  • The OperatingSystem library provides keywords for interacting with the operating system, e.g.,

    • File Should Exist checks if a file exists.

    • Run Command runs a command on the operating system’s shell.

  • The Collections library offers keywords for working with lists, dictionaries, and other collections, including

    • Create List creates a new list.

    • Get From List gets an item from a list using an index.

  • The String library provides keywords for string manipulation and verification, such as

    • Should Be Equal As Strings compares two strings for equality.

    • Should Contain checks if one string contains another.

These are just a few examples of the standard libraries and their keywords that are available with the Robot Framework. Depending on your testing needs, you can select and import the appropriate libraries into your test cases and make use of their predefined keywords to perform various actions and verifications. If the predefined keywords don’t meet your specific requirements, you can also create custom keywords using programming languages like Python or Java and integrate them into your test suite.

Your First Robot Test

In order to better understand how the Robot Framework work, let’s write a simple test ourselves. Robot Framework test files are written in plain-text and have a specific syntax that consists of sections, settings, keywords, and test case definitions. The syntax is designed to be easily readable and writable by both technical and non-technical users. Here’s an overview of the basic syntax elements you’ll find in a Robot Framework test file:

  • The Settings section is used to define various settings and configurations for your test suite or test case. Common settings include importing libraries, setting variables, and configuring test environment options.

  • In the Variables section, you can define variables that can be used throughout the test suite or test case. Variables are often used to store data that might change based on the test environment.

  • The Test Case section is where you define your test cases. Each test case consists of a name, a series of test steps (keywords), and optional documentation or metadata.

  • In the Keyword section, you can define custom keywords that encapsulate specific actions or verifications. These custom keywords can be reused across multiple test cases.

All data in a Robot Framework file, such as keywords and their arguments, are separated from each others with two or more spaces. An alternative is using the pipe separated format.
example.robot
*** Settings ***                                          (1)
Documentation    This is a simple robot example file.     (2)
Metadata         Version    1.0
Metadata         Author     John Doe
Library          SSHLibrary                               (3)

*** Variables ***                                         (1)
${user}          supervisor                               (4)
${password}      supervisor

*** Test Cases ***                                        (1)
Basic Test Verifying Server Is Linux                      (5)
    Connect to Server and Login    SN    supervisor    supervisor
    Verify Server is Linux
    Close Server Connection

*** Keywords ***                                          (1)
Connect to Server and Login                               (6)
    [Arguments]    ${host}    ${user}    ${password}
    SSHLibrary.Open Connection    ${host}
    SSHLibrary.Login              ${user}    ${password}

Close Server Connection
    SSHLIbrary.Close Connection

Verify Server is Linux
    ${stdout}=    SSHLibrary.Execute Command     uname
    Should Be Equal as Strings    ${stdout}    Linux
1 Each section begins with three asterisks.
2 The Documentation and Metadata definitions are additional information written to log files.
3 The Library keyword allows to include pre-defined libraries.
4 Variables can be used as scalars (${scalar}), lists (@{list}) or dictionaries (&{dict}).
5 Test cases are defined by their name and contain keywords.
6 User defined keywords are new high-level constructions made of existing keywords.

Finally, we can execute the test via the robot command:

student@tour:~$ robot example.robot
==============================================================================
Module :: This is a simple robot example file.
==============================================================================
Basic Test Verifying Server Is Linux                                  | PASS |
------------------------------------------------------------------------------
Module :: This is a simple robot example file.                        | PASS |
1 test, 1 passed, 0 failed
==============================================================================
Output:  /home/student/dev_robot/output.xml
Log:     /home/student/dev_robot/log.html
Report:  /home/student/dev_robot/report.html