0

My requirement is to make use of user defined java libraries in robot framework using RED eclipse editor. When trying to specify library in the robot framework, the system errors as no such library available(shown underline in red for library name). Please correct my mistakes done. I have followed the below steps,

  1. Updated Eclipse with RED Editor(Eclipse Neon (v 4.6), RED - Robot Editor v0.7.5)
  2. Created a class file just as Project in the same eclipse. (Package name: org.robot.KCCKeywords and Class Name: LogonToKCC)
  3. Converted the class file into the type '.JAR' and stored it in jython folder(C:\jython2.7.0\Lib\site-packages\KCCLibraries)
  4. Integrated RED with Maven plugin using launch4j-3.8-win32(using https://github.com/nokia/RED/blob/9d62dccce18ee7f3051162d05bf3d027e33dccef/red_help/user_guide/maven.html.md)
  5. Integrated RED with Robot framework and Jython. (using https://github.com/nokia/RED/blob/9d62dccce18ee7f3051162d05bf3d027e33dccef/red_help/user_guide/maven.html.md)
  6. CLASS PATH updated for below jars,

    a) jython.jar b) robotframework-3.0.2.jar c) myOwnJavaLibrary.jar ( The jar that i created in step 3) d) jdk and jre path

  7. Verified the same class paths in red.xml too.
  8. Created RED Project and started initializing key words as below,

    a) Library Selenium2Library

    b) Library org.robot.KCCKeywords.LogonToKCC

Here is where the system couldn't read my own library. I also referred to below blogs and adjusted my steps accordingly. But didn't help me. Referring to multiple blogs and stacks also confusing me. Finally I'm here.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Roja
  • 293
  • 1
  • 5
  • 21

2 Answers2

6

Using the codecentric blog: Robot Framework Tutorial – Writing Keyword Libraries in Java as a base with some specific steps for RED in stead of RIDE. This walkthrough will allow you to setup Jython, create a simple library in Java and run it from Robot script.

After the installation of Eclipse (NEON) and RED Feature in Eclipse create a new Java project in Eclipse. With that done continue to create a new Java class with the following content.

package org.robot.sample.keywords;

import java.util.Stack;

/**
 * This is an example for a Keyword Library for the Robot Framework.
 * @author thomas.jaspers
 */
public class SampleKeywordLibrary {

    /** This means the same instance of this class is used throughout
     *  the lifecycle of a Robot Framework test execution.
     */
    public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";    

    //</editor-fold>
    /** The Functionality to be tested */
    private Stack<String> testStack;

    /**
     * Keyword-method to create an empty stack.
     */
    public void createAnEmptyStack() {
        testStack = new Stack<String>();
    }


    /**
     * Keyword-method to add an element to the stack.
     * @param element The element
     */
    public void addAnElement(String element) {
        testStack.push(element);
    }

    /**
     * Keyword-method to remove the last element from the stack.
     */
    public void removeLastElement() {
        testStack.pop();
    }

    /**
     * Keyword-method to search for an element position.
     * @param element The element
     * @param pos The expected position
     */
    public void elementShouldBeAtPosition(String element, int pos) 
            throws Exception {
        if (testStack.search(element) != pos) {
            throw new Exception("Wrong position: " + testStack.search(element));
        }
    }

    /**
     * Keyword-method to check the last element in the stack.
     * @param result Expected resulting element
     */
    public void theLastElementShouldBe(String result) throws Exception {
        String element = testStack.pop();
        if (!result.equals(element)) {
            throw new Exception("Wrong element: " + element);
        }
    }
}

Please ensure that you have Jython installed using the Windows Installer. In my example Jython is installed in c:\Jython. As with the regular Python Interpreter Robot Framework still needs to be installed. Assuming that your machine has access to the internet, in the command line go to c:\Jython\bin\ and run the command pip install robotframework. This will install Robot Framework in the Jython environment.

Now create a new Robot Framework project in Eclipse. Please make sure that you have Window > Perspective > Open Perspective > Robot or Other > Robot.

enter image description here

In the project the default Robot Framework is one based on Python and we need to configure the Jython Interpreter. In Eclipse go to Window > Preferences and then select Robot Framework > Installed Frameworks from the tree-menu. Click on Add and point to c:\Jython\bin\. Click on OK.

enter image description here

Open Red.XML from the Robot Project and go to the general tab. This is where the project Interpreter is set. If it is set to Python (like the example below) then click on use local settings for this project and check the Jython interpreter. Save the settings to the file (CTRL-S).

enter image description here

With Robot Framework project setup it is time to export the Java class to a Jar file. Right click the class file and click export. Then choose JAR file and click next. Click on Browse and set the location and file name of the JAR file. In this case I picked ExampleLibrary.jar and the folder of my Robot Project. Press Finish to complete the export.

Go Back to Red.XML and click on Referenced Libraries then proceed to click on Add Java library, pick the exported Jar file (ExampleLibrary.jar) and press OK. This will proceed to load the jar and read the keywords from the Jar file. Save the file (CTRL-S). This should result to the below reference.

enter image description here

Now it's time to create a Robot file and use the library. In the referenced blog the following example script is given that uses the java functions/keywords.

*** Settings ***
Library    org.robot.sample.keywords.SampleKeywordLibrary

*** Test Cases ***
ExampleJava
    Create An Empty Stack
    Add An Element    Java
    Add An Element    C++
    Remove Last Element
    The Last Element Should Be    Java

With the already loaded library no red lines should appear, but otherwise right-click on the library and click quick-fixand autodiscover the library.

Then using the regular Eclipse/RED Run menu run the script. This will then run the script successfully and output the following:

Command: C:\jython2.7.0\bin\jython.exe -J-Dpython.path=C:\jython2.7.0\Lib\site-packages -J-cp .;C:\Eclipse\Workspace\ExamplJava\ExampleLibrary.jar -m robot.run --listener C:\Users\User\AppData\Local\Temp\RobotTempDir8926065561484828569\TestRunnerAgent.py:57292:False -s ExamplJava.ExampleJava C:\Eclipse\Workspace\ExamplJava
Suite Executor: Robot Framework 3.0.2 (Jython 2.7.0 on java1.8.0_60)
==============================================================================
ExamplJava                                                                    
==============================================================================
ExamplJava.ExampleJava                                                        
==============================================================================
ExampleJava                                                           | PASS |
------------------------------------------------------------------------------
ExamplJava.ExampleJava                                                | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
ExamplJava                                                            | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  C:\Eclipse\Workspace\ExamplJava\output.xml
Log:     C:\Eclipse\Workspace\ExamplJava\log.html
Report:  C:\Eclipse\Workspace\ExamplJava\report.html
A. Kootstra
  • 6,827
  • 3
  • 20
  • 43
  • Thanks a lot Kootstra! This worked. I'm able to successfully add the library now without any red line. The mistake i have done is added myOwnLibrary.jar into class path rather than putting it in "Add Java Library" option. I destroyed all my set up and just followed your steps which lead me to successful integration. Thanks! But unfortunately I'm unable to execute it. It errors as below, **Unknown option: J-Dpython.path=C:\jython2.7.0\Lib\site-packages usage: jython [option] ... [-c cmd | -m mod | file | -] [arg] ... Try `jython -h' for more information.** – Roja Feb 23 '17 at 05:23
  • I also verified with the environment variables for the specified path in the error. What else could be the reason? – Roja Feb 23 '17 at 05:48
  • The example works for a Jython installation. If you follow the steps, then you will be able to run your java code. However, if you use the Launch4j generated jython.exe, the error you received will be generated. This appears to be a RED specific problem as it it is generates the `Command: C:\Launch\jython.exe -J-cp .;C:\Eclipse\Jython\ExampleLibrary.jar ... Suite Executor: Robot Framework 3.0.2 (Jython 2.7.0 on java1.8.0_71) Unknown option: J-cp` I'll raise an issue with RED as the `-J-cp` argument isn't supported by Launch4J generated exe files. – A. Kootstra Feb 23 '17 at 06:41
  • I followed the above set up today for jython integration, earlier i integrated with lauch4j, It seems having these both set up interrupted it. Now I have totally wiped out the lauch4j and tried to go ahead as per the above. In the Eclipse editor now the Selenium2Library and it's keywords are not recognized. It shows with red line. Re-tried by copying the selenium2library from python folder and that didn't help me out. What should i do now to make it recognized? – Roja Feb 23 '17 at 11:22
  • In my example Jython is installed in `c:\Jython`. In the `bin` directory there is `pip`. Have you installed Selenium2Library using the Jython PIP? – A. Kootstra Feb 23 '17 at 21:22
  • Today RED team released a new release RED 0.7.5-Nightly for a selenium2library (amongst others) problem. Perhaps this is the problem you're facing? – A. Kootstra Feb 23 '17 at 21:24
  • I tried using PIP & easy_install, it ended up with an error as below, **C:\jython2.7.0\bin>pip install robotframework-selenium2library Traceback (most recent call last): File "C:\jython2.7.0\bin\robotframework-3.0.2.jar\Lib\runpy.py", line 161, in run_module_as_main File "C:\jython2.7.0\bin\robotframework-3.0.2.jar\Lib\runpy.py", line 72, in _run_code File "C:\jython2.7.0\bin\pip.exe__main_.py", line 5, in ImportError: No module named pip –** – Roja Feb 24 '17 at 04:39
  • I updated my eclipse with latest RED fix 0.7.5-Nightly for selenium2library. This particular fix is to load selenium2library automatically. But I still have the same issue. Kindly help me on this. – Roja Feb 24 '17 at 09:35
  • To me the above error doesn't make sense. When I ran `C:\jython2.7.0\bin>pip.exe install robotframework` it installls the Python version of Robot Framework in `C:\jython2.7.0\Lib\site-packages\robot`. So the Jar file is not needed, this is an alternative approach to the Maven jar file. The same goes for installing robotframework-selenium2library, this also installs the Python version in site-packages. You will then be able to run it as usual. – A. Kootstra Feb 24 '17 at 19:06
  • Understood now. I pasted the jar file manually before working on the above steps. I deleted it from the folder now and re-executed C:\jython2.7.0\bin>pip.exe install robotframework . I got a long message. I think the lack of command prompt knowledge bugs me all these. I attached the error separately. Kindly help me what it is telling me and what further steps i should do over that. – Roja Feb 25 '17 at 07:18
  • 1
    Finally I achieved. Its all because of your valuable response A. Kootstra. Thanks a lot. I uninstalled entire areas related to test automation with robot framework and followed steps using your conversations. It worked. A glimpse on what i did is added below. – Roja Feb 25 '17 at 12:48
  • @Roja can you have a look and consider removing those examples below that didn't work so that other who read this later on aren't confused by it? – A. Kootstra Feb 25 '17 at 15:54
  • Excellent response - thanks for the tutorial! I had one issue that had me banging my head against the wall. I figured it out and wanted to share in case anyone else was stuck in the same spot. I kept getting a context error (red lines) on `Library org.robot.sample.keywords.SampleKeywordLibrary`. The problem was that I had created the test suite as a .tsv and then copy-pasted the text from this post. I needed to fix up the text above with tabs - per the tsv standard. Once that was done it worked like a charm. Creating the test suite as a .robot file does not have the same issue. – MattPerry Sep 27 '18 at 17:18
0

I finally followed the below for a great journey with robot framework.

1   Installed Java, Eclipse, RED Eclipse plugin.
   a) Java(JDK 1.8.0/JRE 1.8.0)
   b) Eclipse Neon (v 4.6)
   c) RED - Robot Eclipse Editor v0.7.5.2(Eclipse Plugin)
2   Downloaded and Installed Python 2.7.12 using windows. A folder created automatically after installation in C:\python27
3   "Installed Robot Framework using pip command in Command Prompt.
     Command:     C:\python27\scripts>pip install robotframework"
4   Downloaded and installed Jython 2.7.0 using windows. A folder created automatically after installation in C:\jython2.7.0
5   "Installed Robot Framework using pip command in Command Prompt.
     Command:     C:\jython2.7.0\bin>pip install robotframework"
6   "Installed Selenium2Library using pip command in Command Prompt.
     Command:     C:\jython2.7.0\bin>pip install robotframework-selenium2library"
7   "Set the below,
    a) Goto Window-Preferences-Robot Framework-Installed Framework
    b) Map Robot framework with Jython Interpreter
    I used c:\jython2.7.0\bin"
8   Created JavaProject and export it into a jar file. Right click on the class name, click on export-Java-Jarfile. Select the path name where the new jar file to be put including the new file name.jar. Click Ok.
9   Open RED.xml Click Add Java Library and select the newly created jar file.
10  "Set up this before proceeding with robot framework 
    goto Windows - Perspective - Open Perspective-Other-Robot"
11  Create a robot suite, import library selenium2library & user defined library, Write Test cases.
Roja
  • 293
  • 1
  • 5
  • 21