Roja
Roja

Reputation: 293

It errors when specifying the user defined java library into RED robot framework eclipse editor

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.

Upvotes: 0

Views: 5506

Answers (2)

Roja
Roja

Reputation: 293

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.

Upvotes: 0

A. Kootstra
A. Kootstra

Reputation: 6981

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

Upvotes: 6

Related Questions