Basic Keyword Development
Getting started
We’ll be using Java as our default language and platform and Maven as our dependency management and build tool in this series of examples, but the same can be achieved in other languages supported by step (JS, .Net, etc) as long as the dependencies are correctly downloaded and linked.
This page helps you to start with developing a simple keyword. You should check following pages for details about the Keyword API itself.
Full examples are available on github.
Prerequisites
step Keyword development requires at least Java 8 SDK installed on your machine. Depending on your platform, they are various ways to install it :
-
Windows refer to the official website and follow the instructions : Oracle Java download
-
Debian / Ubuntu execute the following command as root : apt-get install default-jdk
-
Mac OS refer to the official website and follow the instructions : Oracle Java Max OS installation
Setting up Maven
Dependencies
We have made a public maven artefact available on our nexus for the developer’s convenience:
<dependency>
<groupId>ch.exense.step</groupId>
<artifactId>step-api-keyword</artifactId>
<version>1.1.3</version>
</dependency>
Maven Repository
Exense’s public repository (http://nexus.exense.ch/nexus/content/repositories/releases) is deprecated. As of version 3.6.1 (and upwards), you should now be able to pull our dependencies straight out of the public sonatype repository.
We recommend also to setup the Maven compiler plugin to use Java 8 :
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.step</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>scripts-keyword-demo</artifactId>
<dependencies>
<dependency>
<groupId>ch.exense.step</groupId>
<artifactId>step-api-keyword</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Basic example
The following examples apply to all languages supported by step (i.e, Java, .NET, JS/nodeJS, etc). Syntax may vary due to the language’s own specifics but the objects exposed to the developer won’t.
For Java, which will be our default platform for examples in the documentation, make sure your Keyword class extends step’s class step.handlers.javahandler.AbstractKeyword and that your dependencies have been set up.
Code
In the following example we will create a simple keyword waiting for a period passed into the Input and adding to the Output a boolean with a value of true. Here the code :
package com.example.step;
import step.handlers.javahandler.AbstractKeyword;
import step.handlers.javahandler.Keyword;
public class KeywordDemo extends AbstractKeyword {
// Annotation tells step that this function can be used as a Keyword
@Keyword()
public void Sleep() throws NumberFormatException, InterruptedException {
// Waiting for the period passed in Input
Thread.sleep(Long.parseLong(input.getString("sleep")));
// Add a boolean object to the Output
output.add("success", true);
}
}
Testing the keyword locally
Here’s an example of how you can run your keyword locally as a JUnit test:
@Test
public void KeywordTest() throws Exception{
ExecutionContext ctx = KeywordRunner.getExecutionContext(properties, this.getClass());
Output<JsonObject> output = ctx.run("Sleep", "{\"sleep\":\"100\"}");
System.out.println(output.getPayload());
}
2019-01-11 15:47:07,481 DEBUG [main] s.h.j.KeywordHandler [KeywordHandler.java:91] Invoking method Sleep from class com.example.step.KeywordDemo loaded by sun.misc.Launcher$AppClassLoader@b4c966a
{"success":true}
Packaging and deployment
You can now package your class into a single Jar file using Maven. For this you can use the following command after changing directory to the one containing you Java class (note that you can also use your IDE in order to generate the Jar file):
mvn clean install
You can now register the keyword into step. For this, go to the Keyword tab, click on New Keyword and fill properly the required fields :
Note: jar files can also be dragged and dropped into the input box or uploaded programmatically via REST or our Remote Java Client
You can now test your keyword by clicking on the run button and fill the Input value properly :