Skip to content

Instantly share code, notes, and snippets.

@gclaussn
Last active May 15, 2023 19:29
Show Gist options
  • Save gclaussn/90aea886126857266176a7d9e78a16fa to your computer and use it in GitHub Desktop.
Save gclaussn/90aea886126857266176a7d9e78a16fa to your computer and use it in GitHub Desktop.
robotframework-java-library

Custom Java libraries for the Robot Framework can be written as:

  • Static keyword library
  • Dynamic keyword library

Create a Maven project with following dependencies:

<dependency>
  <groupId>org.robotframework</groupId>
  <artifactId>robotframework</artifactId>
  <version>3.0</version>
</dependency>
<dependency>
  <groupId>org.robotframework</groupId>
  <artifactId>javalib-core</artifactId>
  <version>1.2.1</version>
</dependency>

Static keyword library example

Create a class which extends AnnotationLibrary and defines the ROBOT_LIBRARY_SCOPE as one of:

  • TEST CASE
  • TEST SUITE
  • GLOBAL

Please see: Test Library Scope

Add the pattern, under which classes with @RobotKeyword annotated methods can be found.

Library:

package some;

import org.robotframework.javalib.library.AnnotationLibrary;

public class StaticLibrary extends AnnotationLibrary {

  public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";

  /** Path, under which the keyword implementing classes can be found. */
  private static final String KEYWORD_PATTERN = "some/keywords/**/*.class";

  public StaticLibrary() {
    addKeywordPattern(KEYWORD_PATTERN);
  }
}

Keywords:

package some.keywords;

import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywords;

@RobotKeywords
public class MessageKeywords {

  @RobotKeyword
  @ArgumentNames({ "message" })
  public void printMessage(String message) {
    System.out.println(message);
  }
}

Dynamic keyword library example

A dynmic library just need to implement the RobotJavaLibrary interface. The interface requires to implement following methods:

  • getKeywordNames: Provides the names of all implemented keywords
  • runKeyword: Runs the keyword with the given name and arguments

Please note: With a dynamic library, keywords with embedded arguments can be implemented!

package some;

import java.util.Arrays;

import org.robotframework.javalib.library.RobotJavaLibrary;

public class DynamicLibrary implements RobotJavaLibrary {

  @Override
  public Object runKeyword(String keywordName, Object[] args) {
    System.out.println(keywordName + ": " + Arrays.asList(args));
    return null;
  }

  @Override
  public String[] getKeywordNames() {
    return new String[] { "Dynamic message from ${x} to ${y}" };
  }
}

Keyword library usage

* Settings
Library  some.DynamicLibrary
Library  some.StaticLibrary

* Test Cases
Test: Dynamic message
  Dynamic message from a to b

Test: Print message
  Print message  Hello World!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment