Search code examples
pythonjythonrobotframework

How to add a Java custom library to Robot Framework RIDE


I want to create and import a java custom library into RIDE.

Create the Java file based on the below link
(source for creation of test library : http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#creating-test-libraries)

Here below is the java Library Code:

public class MyLibrary{

    public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
    public static final String ROBOT_LIBRARY_VERSION = "1.0";
    public static final String ROBOT_LIBRARY_DOC_FORMAT = "HTML";

    private int counter = 0;

    public void count() {
        counter += 1;
        System.out.println(counter);
    }

    public void clearCounter() {
        counter = 0;
    }    
}

Created the JAR file and placed in the path D:\Python27\Lib\site-packages

Installed the Jython and set the environment vairables, JYTHON_HOME=D:\jython2.7.0; JYTHONPATH = D:\jython2.7.0\bin

Open RIDE and attached the library with the keyword Library MyLibrary

It displayed the text in red. (Meaning it is not a recognized library).


Solution

  • I tried compiling the java into class file and placing it under "~\jython2.7rc1\Lib\site-packages\MyLibrary.class"

    When I run the below test it worked perfectly for me:

    *** Settings ***
    Library     MyLibrary
    
    *** Test Cases ***
    sample
       count 
    

    Just in case you face issues with path of jython or Jybot, place jybot.bat in the path "C:\Python27\Scripts" folder and change the jybot.bat content to absolute value i.e

    @echo off 
    C:\jython2.7.0\bin\jython "C:\Python27\Lib\site-packages\robot\run.py" %*
    

    This will resolve the path issues if any!