Robot Framework - Keyword Tutorial

Table of Contents

Introduction and Purpose

The main idea of this project is to have easy and out-of-the-box working examples on how to work with the Robot Framework. And here especially with its most central feature, namely, Keywords.

Therefore the tutorial is splitted into really small sections and is always only using those Keywords that are part of the Robot Framework out-of-the-box. Thus no additional installation is required beside having a running Robot Framework installation and forking/cloning this repository to try around with the examples. I hope this tutorial can help on the one hand side to get a basic understanding how Testsuites are written with the Robot Framework using Keywords. Furthermore this might serve as a quick reference for more experienced users to just quickly take a look on how a certain feature is used (or a certain syntax).

Note: All examples are shortly explained in the following and it is shown how to start the example either using robot or the Java installation of the Robot Framework. It is required that robot can be found from the PATH on your machine for this to work and the path to the Robot-JAR will most likely differ ;-). It is expected that the execution is always triggered from the directory containing the Testsuite-files.

top

Installation and Update

The most convenient way to install the Robot Framework is using the Python package manager "pip".

To install the Robot Framework, RIDE and the Selenium2 Test Library just execute:

pip install robotframework

pip install robotframework-selenium2library

pip install robotframework-ride

Using "pip list" it is possible to get information on all installed packages.

With the following commands it is possible to trigger an update installation.

pip install robotframework --upgrade
pip install robotframework-selenium2library
 --upgrade
pip install robotframework-ride --upgrade

top

sample-0-trivial

This contains the most basic example on how to define a testcase using keywords (here only the Log keyword is used that prints to the generated log-file). Note: There must be at least two spaces to separate arguments from keywords and arguments from each other. Furthermore there must be always an indention of two spaces in the beginning of each line below a testcase.

Test execution | VIEW FILE
robot --outputdir ./report sample-0-trivial.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-0-trivial.txt


The next example shows how to import a Library (in this case the String Library). It also shows a simple for-loop. Please note that the "" are required in the lines belonging to the loop.

Test execution | VIEW FILE
robot --outputdir ./report sample-1-trivial-extended.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-0-trivial.txt

top

sample-1-own-keyword

One of the key features of the Robot Framework is the possibility to combine existing Keywords into new Keywords within some text files. This first example shows the most simple way of defining a customized keyword within the Testsuite-file.

Test execution | VIEW FILE
robot --outputdir ./report sample-0-own-keyword.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-0-own-keyword.txt


The second example shows the usage of an array as a parameter for a customized Keyword.

Test execution | VIEW FILE
robot --outputdir ./report sample-1-own-keyword.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-1-own-keyword.txt


In this example the customized Keyword is moved to an external Resource-file that is then used from within the Testsuite.

Test execution | VIEW FILE | VIEW RESOURCE FILE
robot --outputdir ./report sample-2-own-keyword-resource.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-2-own-keyword-resource.txt

top

sample-2-remote-keywords

Using a remote library can be extremely helpful as it allows writing Keyword-Libraries in any programming language as long as it offers a XML-RPC interface. The example make use of a very simple example of such a remote library that has been written in Java.

To make this example usable out of the box the corresponding JAR-File (implementing the remote library) is part of the example directory and can be started right away.

Test preparation
Start the Remote Keyword Server by issuing (Note: The sample server is using port 8270):
java -jar server/sample-remote-library-1.0-server.jar

Test execution | VIEW FILE
robot --outputdir ./report sample-0-remote.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-0-remote.txt

top

sample-3-variables

Variables are offering a lot of possibilities when writing tests with the Robot Framework. The following sample shows a most basic example on how to use variables.

Test execution | VIEW FILE
robot --outputdir ./report sample-0-variables.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-0-variables.txt


This example gives an example on how global variable values can be temporarily overwritten in the scope of a testcase. Note that this does not permanently change the value of the globally defined variable.

Test execution | VIEW FILE
robot --outputdir ./report sample-1-variables-scope.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-1-variables-scope.txt


This example shows the usage of variable files. This is especially useful to define certain values (like URLs or database connections) differently for different test environments. The variable files are then passed in via a command line option.

Test execution | VIEW FILE | VIEW LOCAL VARIABALE FILE | VIEW TEST VARIABLE FILE
robot --outputdir ./report --variablefile ./variable-file-env-local.py sample-2-variables-file.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report --variablefile ./variable-file-env-local.py sample-2-variables-file.txt

Note: Change the variable-file passed in and take a look at the resulting log-file.

top

sample-4-tagging

Tagging is a useful feature to have some kind of test coverage in the generated report. Just tag each testcase with the proper - uhm - tags. Note: Make sure you are using unique tag-names for the same features.

Test execution | VIEW FILE
robot --outputdir ./report sample-0-tagging.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-0-tagging.txt

top

sample-5-setup-teardown

Setup- and Teardown-functions (Keywords) are very helpful to separate preparation tasks from the real test execution. Furthermore it allows to perform global steps for a testsuite once (in the beginning and end). This is shown in the following example.

Test execution | VIEW FILE
robot --outputdir ./report sample-0-suite.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-0-suite.txt


It is also possible to define Setup- and _Teardown-_functions that are executed bevore and after the execution of every Testcase of a Testsuite. Can be helpful if the same preparation/cleanup steps are required for each test.

Test execution | VIEW FILE
robot --outputdir ./report sample-1-test.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-1-test.txt


Finally some testcases might need specific preparation/cleanup functionality that is not part of the test itsself. This can be achieved as follows.

Test execution | VIEW FILE
robot --outputdir ./report sample-2-testcase.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-2-testcase.txt

top

sample-6-parameter

Sometimes it might make sense to have Keywords that can work with a lot of parameters. For example all the parameters that might be processed in a form of a web application. Using the "..."-syntax it is possible to split paramters to one Keyword to sevaral lines and thus increase readability.

Test execution | VIEW FILE
robot --outputdir ./report sample-0-parameters-divided.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-0-parameters-divided.txt

top

sample-7-conditional-execution

This example shows how to execute a Keyword set as a variable. This way it is for example possible to stear which keyword to use for certain actions by overwriting this variable using a variable file or command line option. (Might be useful if the same keyword cannot be used locally and in some test environment.)

Test execution | VIEW FILE
robot --outputdir ./report sample-0-keyword-in-variable.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-0-keyword-in-variable.txt

Test execution with different logging keyword robot --outputdir ./report --variable MY_LOGGING_KEYWORD:Log_Many -E space:_ sample-0-keyword-in-variable.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report --variable MY_LOGGING_KEYWORD:Log_Many -E space:_ sample-0-keyword-in-variable.txt


This example shows how to run a Keyword based on a condition (aka some value in some variable). Use with care as it might make tests hard to understand and troubleshoot if they are failing.

Test execution | VIEW FILE
robot --outputdir ./report sample-1-run-keyword-if.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-1-run-keyword-if.txt


The following shows how to run a Keyword and ignore any errors it produces. Might be useful when using temporary sometimes or maybe when addressing an external system during testing that provides a kind of "nice-to-have" feature.

Test execution | VIEW FILE
robot --outputdir ./report sample-2-ignore-error.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-2-ignore-error.txt

top

sample-8-database

Note: This example differs from the previous examples of this tutorial, as it requires a database to be setup and running. In this case PostgreSQL is used with an example that is described here: https://github.com/ThomasJaspers/db_liquibase_sample

The example can of course be easily changed to user another database and other tables by replacing the JDBC-driver and adepting the tests.

The documentation for the Database Library is part of this tutorial and can be found here: https://github.com/ThomasJaspers/robot-keyword-tutorial/blob/master/sample-8-database/doc/DatabaseLibrary_v20.html


This example shows the usage of the Java Database Library for the Robot Framework. Please note that execution via robot is not possible, but the next example shows the usage of the Database Library as a Remote Library. Then robot can be used again.

Test execution | VIEW FILE

java -cp '/usr/local/opt/robotframework/robotframework-2.9.2.jar:./lib/dblibrary-2.0.jar:./lib/postgresql-9.3-1102-jdbc41.jar' org.robotframework.RobotFramework --outputdir ./report sample-0-database.txt


To execute the example using the Remote Library support of the Database Library the server needs to be started first. Afterwards the tests can be executed.

Test execution | VIEW FILE

java -cp './server/dblibrary-2.0-server.jar:./lib/postgresql-9.3-1102-jdbc41.jar' org.robot.database.server.RemoteServer --port 8270

robot --outputdir ./report sample-1-database-remotelib.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-1-database-remotelib.txt

top

sample-9-file-processing

This example shows how to do some more complex file processing using the Robot Framework. It starts with a small example and the continues by extending this example. Please take also a look at the corresponding blog post for more information.

Test execution | VIEW FILE
robot --outputdir ./report sample-9-reading-text-file.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-9-reading-text-file.txt


Added first processing step to the example.

Test execution | VIEW FILE
robot --outputdir ./report sample-9-processing-text-file-1.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-9-processing-text-file-1.txt


Added second processing step to the example.

Test execution | VIEW FILE
robot --outputdir ./report sample-9-processing-text-file-1.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-9-processing-text-file-1.txt

top

sample-10-collections

This example shows some basic usage of the Robot Framework Collections-library Please take also a look at the corresponding blog post for more information.

Test execution | VIEW FILE
robot --outputdir ./report sample-0-simple-list.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-0-simple-list.txt


Some more advanced examples for working with Lists.

Test execution | VIEW FILE
robot --outputdir ./report sample-1-complex-list.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-1-complex-list.txt


Add some examples how to work with dictionaries.

Test execution | VIEW FILE
robot --outputdir ./report sample-2-dictionary.txt
java -jar /usr/local/opt/robotframework/robotframework-2.9.2.jar --outputdir ./report sample-2-dictionary.txt

top