How to import java class, to Robot Framework like library

最后都变了- 提交于 2019-12-23 01:37:12

问题


I can't understand how to import .jar file, in Robot Framework.

Here is the code:

*** Settings ***
Library   MyLibrary

*** Test Cases ***
My Test
    Do Nothing
    Hello    world

and Java:

public class MyLibrary {

    public void hello(String name) {
        System.out.println("Hello, " + name + "!");
    }

    public void doNothing() {
    }

}

After Extracting in .jar, I put in C:\Python27\Lib\site-packages\MyLibrary and I created empty __init__.py file. After I execute my Robot file with: pybot TestJavaLibrary.robot I get this WARN:

[ WARN ] Imported library 'MyLibrary' contains no keywords.
==============================================================================
TestJavaLibrary
==============================================================================
My Test                                                               | FAIL |
No keyword with name 'Do Nothing' found.

How to use this jar, like external library?


回答1:


I have also worked on the same kind of project which required Java class to be called via robot framework. Here is a short example of importing a Java library:

File : myJavaLibrary.java

public class myJavaLibrary{

    public void my_java_add(int i, int j, int k) {
        if(i+j == k)
            return;
        System.out.println("Invalid Sum");
        assert false;
    }

}

Use command : javac myJavaLibrary.java

This will create a .class file in the same directory : myJavaLibrary.class

Import this in robot file :

File: test.robot.txt

*** Settings ***

Library       myJavaLibrary.java

*** Test Cases ***

User defined Java Test

    My Java Add     5   7   12

You may notice that library myJavaLibrary.java is added in settings section, since robot file is present in the same directory as .class file. You may add the absolute path for the same.

You may need to install jython for running the robot file.

Finally use the command:

jython -m robot test.robot.txt.

Final output can be seen in log.html file in the run folder

For JAR import:

Include the absolute path to .jar file in your environmental variable:

Variable Name : CLASSPATH

Variable Value: "Absolute path to directory containing Jar"\*;

In this case the process is same for running robot file, and there is no need to include any library.

Hope this works.

Reference: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html




回答2:


You have to use jython (jybot). There are other settings like JYTHONPATH.



来源:https://stackoverflow.com/questions/38525558/how-to-import-java-class-to-robot-framework-like-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!