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>
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);
}
}
A dynmic library just need to implement the RobotJavaLibrary
interface.
The interface requires to implement following methods:
getKeywordNames
: Provides the names of all implemented keywordsrunKeyword
: 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}" };
}
}
* 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!