Blog
Running Robot Framework's Remote Server as Java agent
- A main class that has a "premain" method similar to the regular "main" method.
- A manifest file in the jar with a Premain-Class entry.
- Start up the JVM with the -javaagent parameter.
Step 0: get the RemoteServer code
Our work is relatively easy because we are leveraging the RemoteServer that has been created by ombre42. It instantiates a Jetty server that implements the xml-rpc protocol that connects the Robot Framework client with remote servers. It also has a nice feature in that you can pass test library classes as parameters on the command line so you don't have to repackage the remote server when you change or add a test library. Ensure these dependencies are in your POM (changing version numbers as needed): [xml] <dependencies> <dependency> <groupId>com.github.ombre42</groupId> <artifactId>jrobotremoteserver</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> </dependencies> [/xml]Step 1: create the premain class
[java] package com.xebia.robotbackdoor; import java.lang.instrument.Instrumentation; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.robotframework.remoteserver.RemoteServer; public class RobotBackdoor { public static void premain(String args, Instrumentation inst) throws Exception { RemoteServer.main(args.split(" ")); } } [/java] As you can see we leverage the RemoteServer implementation by ombre42: all we do is to run this in the premain and pass the arguments on (see step 3 for the arguments to pass on the command line). However, if you want to code the settings yourself (and package the test libraries with the premain class), you can code it like this: [java] ...other imports... import com.xebia.mysystem.test.robotlibraries.MyTestLibrary; public class RobotBackdoor { public static void premain(String args, Instrumentation inst) throws Exception { RemoteServer.configureLogging(); Logger.getRootLogger().setLevel(Level.INFO); RemoteServer server = new RemoteServer(); server.putLibrary("/spam", new MyTestLibrary()); server.setPort(9999); server.start(); } } [/java] In case you're wondering (I did): the server will keep itself running and will not get garbage collected.Step 2: Add a Premain-Class entry in the jar manifest
An agent class is started similar to main classes (Main-Class entry): with a Premain-Class entry in the jar manifest file. In our example this one line does the magic:Premain-Class:com.xebia.robotbackdoor.RobotBackdoor
If you're using Maven you will have to manipulate the packaging to add the entry. This is done with the following additions to your POM:
[xml]
<plugins>
...other plugins...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifestEntries>
<Premain-Class>com.xebia.robotbackdoor.RobotBackdoor</Premain-Class>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
[/xml]
Extra: package as a fat jar
You may or may not need this, but we found it convenient to create a "fat jar" that has all the dependencies the RemoteServer needs. This is done with the following addition to your POM: Note we are using the shade plugin which correctly allows the other plugin to add the manifest entry. Another "fat jar" plugin that we tried just plonked in its own manifest without allowing other plugins to manipulate it. [xml] <plugins> ...other plugins... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <minimizeJar>false</minimizeJar> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> [/xml] The exclusions are there because many dependency jars have these security files in them and the JVM will complain about it otherwise.Step 3: Start up the JVM with the -javaagent parameter.
A Java agent gets loaded with the -javaagent parameter as follows (where you have packaged the above code into a "robotremoteagent.jar"):java -javaagent:/path/to/robotremoteagent.jar -jar MySystemUnderTest.jar
This works fine if you have the "hardcoded" version of the Premain class, but if you use the dynamic loading mechanism of RemoteServer you will need to pass in parameters, which are distinct from the regular parameters. This is done with by adding ="my parameter list" to the -javaagent parameter:
java -javaagent:/path/to/robotremoteagent.jar="--library com.xebia.mysystem.test.robotlibraries.MyTestLibrary:/spam --port 9999" -jar MySystemUnderTest.jar
You can add multiple --library entries.
Conclusion and downloads
Executing Robot Framework tests within the context of a running system can be achieved by loading the RemoteServer as a Java agent. You can find the pom.xml and the .java file here: robotremoteagent-code. For the truly lazy: here is the packaged jar file of the remote agent (up to date as of the writing of this post): robotremoteagent-1.0.jar.Written by
Our Ideas
Explore More Blogs
Contact













