Beliebte Suchanfragen

Cloud Native



Agile Methoden



Robot Framework Tutorial – Writing Keyword Libraries in Java

10.6.2012 | 7 minutes of reading time

Robot Framework Tutorial

Part I: Robot Framework Tutorial – Overview
Part II: Robot Framework Tutorial – A complete example
Part III: Robot Framework IDE
Part IV: How to Structure a Scalable And Maintainable Acceptance Test Suite
Part V: Robot Framework Tutorial – Writing Keyword Libraries in Java
Part VI: Robot Framework Tutorial – Loops, Conditional Execution and more
Part VII: Robot Framework – Testing Windows Applications
Appendix A: Robot Framework – Compact Sheet

The new Robot Framework Tutorial 2016 series

In this series so far we have seen some high-level overview , and an example on how to start and implement a Robot Framework project from scratch beside some other topics. So now is time to write our first custom Keyword Library and we will do it in Java. Why in Java? The reason is simple: I do not know Python at all :-). But luckily Java is a quite popular language, thus this seems to be a viable choice anyway. Of course writing keywords in Java has some impact on the usage of the Robot Framework as explainded in the overview . It means Jython has to be used if any Test Library is written in Java, which might not always be desirable. Therefore we will cover the usage of the Robot Framework Remote Library in an follow-up article to this one as well. But first things first, let’s start with the pure Java Keyword Library …

Let’s start …

First look at a "complete" Test Library written in Java

And we are finished :-). Ok, just kidding, but in deed is the above screenshot showing the complete Java code required for a working – but obviously incomplete – Keyword Library. To make things easy in this example we are testing the Java implementation of the Stack-Interface. Of course this does not make too much sense, but it helps focusing on what is needed to implement the library and not that much on some complicated test functionality. In addition this solution can then be nicely compared to the corresponding example used in this JBehave-Tutorial .

Writing a Keyword Library in Java just means to write one class in Java. All public methods of that class can then be used as keywords in the Robot Framework after importing the Library. To be able to use a Keyword Library written in Java in your Robot Tests it is easiest to create a JAR out of it.

Basically there is only one robot-framework-specific definition in this class, which is ROBOT_LIBRARY_SCOPE. For this just take a look at the below excerpt from the Robot Framework documentation:

Test libraries can control when new libraries are created with a class attribute ROBOT_LIBRARY_SCOPE . This attribute must be a string and it can have the following three values:

  • TEST CASE: A new instance is created for every test case. A possible suite setup and suite teardown share yet another instance. This is the default.
  • TEST SUITE: A new instance is created for every test suite. The lowest-level test suites, created from test case files and containing test cases, have instances of their own, and higher-level suites all get their own instances for their possible setups and teardowns.
  • GLOBAL: Only one instance is created during the whole test execution and it is shared by all test cases and test suites. Libraries created from modules are always global.

Of course in this one class implementing the keywords any others Java classes can be used as long as all needed libraries are bundled to the final JAR. One good example here could be testing for example some services of your application directly (e.g. performing Web-Service calls, etc.).

Development Environment & Project Sources

As can be already seen from the above screenshot I am using NetBeans for implementing the Test Library. That is just for the fun of it, as we are using Eclipse in almost all our projects. For implementing the Test Library that does not matter at all, as long as you are able to produce the JAR-file in the end. Just use your preferred development environment.

The sources of this example (including the NetBeans project files) are stored to GitHub as part of the Robot Framework Sample Project that has already been used here . Beside the Java-Sources and Project-Files the repository also contains an example how to take the Test Library into use in some Testsuite. All this will be shown in the course of this article.

Implementation and Usage

As already stated above, the implementation of the Test Library as such is not very complicated, especially as we have used a very trivial example here. It could of course get more complicated, but that has then nothing to do with this being a Test Library – as the approach always stay the same – but with a more complicated system to test.

The following shows the complete Java code required for the Test Library. It is compiled to a JAR-file and can thus be imported to any Testsuite or Resource File for usage.

1package org.robot.sample.keywords;
3import java.util.Stack;
6 * This is an example for a Keyword Library for the Robot Framework.
7 * @author thomas.jaspers
8 */
9public class SampleKeywordLibrary {
11    /** This means the same instance of this class is used throughout
12     *  the lifecycle of a Robot Framework test execution.
13     */
14    public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";    
16    //</editor-fold>
17    /** The Functionality to be tested */
18    private Stack<String> testStack;
20    /**
21     * Keyword-method to create an empty stack.
22     */
23    public void createAnEmptyStack() {
24        testStack = new Stack<String>();
25    }
28    /**
29     * Keyword-method to add an element to the stack.
30     * @param element The element
31     */
32    public void addAnElement(String element) {
33        testStack.push(element);
34    }
36    /**
37     * Keyword-method to remove the last element from the stack.
38     */
39    public void removeLastElement() {
40        testStack.pop();
41    }
43    /**
44     * Keyword-method to search for an element position.
45     * @param element The element
46     * @param pos The expected position
47     */
48    public void elementShouldBeAtPosition(String element, int pos) 
49            throws Exception {
50        if ( != pos) {
51            throw new Exception("Wrong position: " +;
52        }
53    }
55    /**
56     * Keyword-method to check the last element in the stack.
57     * @param result Expected resulting element
58     */
59    public void theLastElementShouldBe(String result) throws Exception {
60        String element = testStack.pop();
61        if (!result.equals(element)) {
62            throw new Exception("Wrong element: " + element);
63        }
64    }

So let’s see how we can import this library now into an example testsuite. Please note that we are using the very directory structure that was presented already in some previous article on a Robot Framework Example . It is shown here again for your quick reference.

Directory Structure

Thus our JAR-file belongs to the execution/lib-folder and our sample testsuite to the implementation/testsuites-folder.

Please note that there are already additional files located in those folder in the GitHub-project. But this is perfectly fine and the different examples can co-exist there without any problems.

Once we have stored the SampleLibrary.jar to the execution/lib-folder we can start implementing a sample testsuite in the implementation/testsuites-folder. Let’s call this one SampleLibraryTestsuite.html and do not confuse this one with the SampleTestsuite.html from the previous example ;-).

Importing our own SampleLibrary is not much different from importing any other keyword library. But as with every keyword library written in Java we have to give the full qualified package name of the corresponding Java Class in the import. Thus in RIDE this looks then as follows:

Importing the Sample Java Library

The rest of the implementation is then absolutely straight forward. The keywords can be used as any other keywords as well. The nice thing is that you do not have to use the precise method names, but you can vary upper- and lowercase and add spaces for better readability. Another screenshot illustrates this:

Basic Stack Test

Basically that is pretty much it. Of course we could very well use another startup-script to be able to easily start the tests. This one can be found from the example under execution/local/scripts as start_SampleLibrary_TestSuite.bat. It has the following content:

del ..\..\..\output\*.xml /Q
del ..\..\..\output\*.html /Q
del ..\..\..\output\*.png /Q

set CLASSPATH="..\..\lib\SampleLibrary.jar;%CLASSPATH%"

jybot --outputdir ..\..\..\output ..\..\..\implementation\testsuites\SampleLibraryTestsuite.html

The most important thing is for sure to extend the Java Classpath properly. To keep our example independent from where it is located (as folders might differ for different team members) we are using a relative path to locate the SampleLibrary.jar. As for sure we are now using at least one Java Library in our tests we have to use jybot (and thus Jython) to start the tests.

That is it! Your first keyword library is probably waiting already around the next corner :-).

In the next post in this series we will then see how to extend this Library to make use of the Robot Framework Remote Server Interface .

share post




More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.


Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.