且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将java类导入到Robot框架之类的库中

更新时间:2023-01-15 18:10:01

我还从事过同样的项目,需要通过机器人框架调用Java类。以下是导入Java库的简短示例:



文件 myJavaLibrary.java

  public class myJavaLibrary {

public void my_java_add(int i,int j,int k) {
if(i + j == k)
return;
System.out.println(无效总和);
断言错误;
}

}

使用命令:javac myJavaLibrary.java



这将在同一目录中创建一个.class文件:myJavaLibrary.class



在机器人文件中导入:



文件:test.robot.txt

  ***设置*** 

库myJavaLibrary.java

***测试用例***

用户定义的Java测试

我的Java添加5 7 12

您可能会注意到myavavaLibrary.java库已添加到设置部分,因为机器人文件与.class文件存在于同一目录中。您可以为其添加绝对路径。



您可能需要安装jython来运行机器人文件。



最后使用命令:



jython -m robot test.robot.txt。



最终输出可以在运行文件夹中的 log.html 文件中看到



对于JAR导入:



环境变量中包含.jar文件的绝对路径:



变量名称:CLASSPATH



变量值:包含Jar的目录的绝对路径\ *;



在这种情况下,运行机器人文件的过程是相同的,并且不需要包含任何库。





参考: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html


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?

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