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.