RemoteServer – How to use Java Keyword in Robotframework?

I’m trying to implement JavaLibCore to use Java method in robotframework. I followed this tutorial : https://blog.codecentric.de/en/2016/01/robot-framework-tutorial-2016-remote-server-keywords-in-java/

RemoteServer class used is the following one : https://github.com/robotframework/jrobotremoteserver/blob/master/src/main/java/org/robotframework/remoteserver/RemoteServer.java

Could you please help me to find a way to fix below error? Thank you a lot.

Java

Keyword class

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

@RobotKeywords
public class MyKeyword {

    @RobotKeyword("Print Message")
    @ArgumentNames({"message"})
    public void printMessage(String message) {
        System.out.println("My message is : " + message);
    }

}

Main class

import org.robotframework.javalib.library.AnnotationLibrary;
import org.robotframework.remoteserver.RemoteServer;

public class KeywordRemoteLibrary extends AnnotationLibrary {

    public static void main(String[] args) {
        RemoteServer.configureLogging();
        RemoteServer server = new RemoteServer("localhost", 8271);
        server.putLibrary("/keywords", KeywordRemoteLibrary.class);
        try {
            server.start();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

}

Console

17:11:26.290 [main] INFO  org.eclipse.jetty.util.log - Logging initialized @1006ms to org.robotframework.remoteserver.logging.Jetty2Log4J
17:11:26.361 [main] INFO  org.robotframework.remoteserver.RemoteServer - Mapped path /keywords to library java.lang.Class.
17:11:26.362 [main] INFO  org.robotframework.remoteserver.RemoteServer - Robot Framework remote server starting
17:11:26.364 [main] INFO  org.eclipse.jetty.server.Server - jetty-9.4.25.v20191220; built: 2019-12-20T17:00:00.294Z; git: a9729c7e7f33a459d2616a8f9e9ba8a90f432e95; jvm 11.0.2+9
17:11:26.435 [main] INFO  org.eclipse.jetty.server.handler.ContextHandler - Started [email protected]{/,null,AVAILABLE}
17:11:26.460 [main] INFO  org.eclipse.jetty.server.AbstractConnector - Started [email protected]{HTTP/1.1,[http/1.1]}{127.0.0.1:8271}
17:11:26.461 [main] INFO  org.eclipse.jetty.server.Server - Started @1178ms
17:11:26.461 [main] INFO  org.robotframework.remoteserver.RemoteServer - Robot Framework remote server started on port 0.

Robotframework code

*** Settings ***
Library    Remote    http://localhost:8271

*** Test Cases ***
First Test Case
    Print Message    test

However I’m facing an error on robotframework script:

Unknow ‘Remote’ library under ‘http://localhost:8271‘ location. Unable to connect.

Answer

Please find my solution below:

public class KeywordRemoteLibrary extends AnnotationLibrary {

static List<String> includePatterns = new ArrayList<String>() {{
    add("keywords/*.class");
}};

public KeywordRemoteLibrary() {
    super(includePatterns);
}

public static void main(String[] args) {
    RemoteServer server = new RemoteServer("127.0.0.1", 8270);
    server.putLibrary("/RPC2", new KeywordRemoteLibrary());
    try {
        server.start();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *