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

前端 未结 2 1502
醉话见心
醉话见心 2021-01-26 00:42

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 error

2条回答
  •  灰色年华
    2021-01-26 01:07

    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";    
    
        //
        /** The Functionality to be tested */
        private Stack testStack;
    
        /**
         * Keyword-method to create an empty stack.
         */
        public void createAnEmptyStack() {
            testStack = new Stack();
        }
    
    
        /**
         * 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.

    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.

    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).

    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.

    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
    

提交回复
热议问题