Rumen
Rumen

Reputation: 137

How to import java class, to Robot Framework like library

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?

Upvotes: 3

Views: 6573

Answers (5)

Fred Meng
Fred Meng

Reputation: 1

*************About

Library A

Library A.java

Library A.class

public class A{

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

    public void doNothing() {
    }

}



*** Settings ***   
Library    A
#or Libary    A.java    this way is also ok 
#or Libary    A.class   this way is also ok 
#Both of these need javac A.java ,you need hava a A.class 

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

*************About robot commont and jython run with a Java or class file

At the same directory,you need to complier A.java

javac A.java

A.java

A.class

JavaHello.robot

run command

robot --pythonpath=. JavaHello.robot
#or
jython -Dpython.path=. -m robot.run JavaHello.robot
#or
jython -J-cp . -m robot.run JavaHello.robot

*************About robot commont and jython run with a jar

jar-cvf my.jar A.class

get my.jar

you can run it Use this commond, notice it

cd D:\AllWorkspace\EWorkspace4Java\RobotDemo\
D:
jython -J-cp D:\AllWorkspace\lib\my.jar -m robot.run JavaHello.robot

jython -Dpython.path=D:\AllWorkspace\lib\my.jar -m robot.run JavaHello.robot

jython -Dpython.path=D:\AllWorkspace\lib\/*.jar -m robot.run JavaHello.robot

or

robot --pythonpath=D:\AllWorkspace\lib\my.jar JavaHello.robot
robot --pythonpath=D:\AllWorkspace\lib\/*.jar JavaHello.robot

if you config CLASSPATH=D:\AllWorkspace\lib\my.jar at window env "PATH"

robot JavaHello.robot
jython  -m robot.run JavaHello.robot

About JYTHONPATH PYTHONPAHT CLASSPATH

You can get more info and diffrences from robot framework github doc.

Ref:http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#pythonpath-jythonpath-and-ironpythonpath

Upvotes: 0

Fred Meng
Fred Meng

Reputation: 1

I figure it out!!

=============env================

**********Sys Window 10

First of all,you need install jdk,jython,robot framework

(PS:jython do not need python2.7 or other python sdk.)

**********For intall

jdk I use a zip.

jython I use a installer jar 2.7.2b3,double click and use install all model.

robot I use master download master code zip, then use "jython setup.py install"

I did not add "CLASSPATH" to Windows System ENV "PATH",I'll say reason later.

I just added D:\Tools\jython2.7\bin To Windows System ENV "PATH".

D:\Tools\jython2.7\bin 的目录
2020/03/01  01:29    <DIR>          .
2020/03/01  01:29    <DIR>          ..
2020/03/01  01:29               417 chardetect-script.py
2020/03/01  01:29            74,752 chardetect.exe
2020/03/01  01:27           102,772 easy_install-2.7.exe
2020/03/01  01:27           102,772 easy_install.exe
2020/02/02  11:30         3,979,302 jython.exe
2020/02/02  11:30               282 jython_regrtest.bat
2020/03/01  01:28           102,754 pip.exe
2020/03/01  01:28           102,754 pip2.7.exe
2020/03/01  01:28           102,754 pip2.exe
2020/03/01  01:29               428 rebot-script.py
2020/03/01  01:29            74,752 rebot.exe
2020/03/01  01:29               428 robot-script.py
2020/03/01  01:29            74,752 robot.exe

We can find robot.exe,can't find jbot,because it's alread combine to robot at Robot Framework 3.2b2,you can check it from Robot Framework Github for more.

All right,our env is done.

check it!

**********SDK

C:\Users\65787>jython --version

Jython 2.7.2b3

C:\Users\65787>java -version

java version "1.8.0_231"

Java(TM) SE Runtime Environment (build 1.8.0_231-b11)

Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

C:\Users\65787>robot --version

Robot Framework 3.2b2 (Jython 2.7.2b3 on java1.8.0_231)

**********IDE: eclipse + red editor

1.Download red editor from elipce markplace and install it.

2.configuration robotframework in eclipce perfermance-->robotframework --->installe framework -> dicover (if does not work ,can manual cofig path at D:\Tools\jython2.7\bin)

Lets's slove your problem.

===========prepar a java jar======================

0.prepar a jar or class/java file for robot framework!

this section I use a jar.

1.new a Maven project->robot-utils4j

2.new A HelloWorld class

package org.mfm.robot.utils4j;

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

    public void doNothing() {
    }

}

2.pom.xml

<groupId>org.mfm</groupId>
    <artifactId>robot-utils4j</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

3.run mvn clean package

4.get a jar robot-utils4j-1.0.0-jar-with-dependencies from target directory.

=============run Robot with java jar=================

1.new a Robot project

2.import Library at red.xml

add new library in Refrenceed Library view.

click robot-utils4j-1.0.0-jar-with-dependencies library.

search org.mfm.robot.utils4j.HelloWorld

and click it

Finally ,The result will Like This:

enter image description here

you'd better right click it mark as static libary.

3.new a JavaHello.robot Test Suite

JavaHello.robot

*** Settings ***   
Library    org.mfm.robot.utils4j.HelloWorld         

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

4.run it you will get this result:

Command: jython.exe -J-Dpython.path=D:\Tools\jython2.7\Lib\site-packages -J-cp .;D:\AllWorkspace\lib\robot-utils4j-1.0.0-jar-with-dependencies.jar;D:\AllWorkspace\lib\/*.jar -m robot.run --listener C:\Users\65787\AppData\Local\Temp\RobotTempDir8138272493583159262\TestRunnerAgent.py:14246 --argumentfile C:\Users\65787\AppData\Local\Temp\RobotTempDir8138272493583159262\args_2df9cc70.arg D:\AllWorkspace\EWorkspace4Java\RobotDemo
Suite Executor: Robot Framework 3.2b2 (Jython 2.7.2b3 on java1.8.0_231)
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
==============================================================================
RobotDemo                                                                     
==============================================================================
RobotDemo.JavaHello                                                           
==============================================================================
My Test                                                               | PASS |
------------------------------------------------------------------------------
RobotDemo.JavaHello                                                   | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
RobotDemo                                                             | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  D:\AllWorkspace\EWorkspace4Java\RobotDemo\output.xml
Log:     D:\AllWorkspace\EWorkspace4Java\RobotDemo\log.html
Report:  D:\AllWorkspace\EWorkspace4Java\RobotDemo\report.html

Further more,I use redisson to connect a redis cluster in robot-utils4j-1.0.0-jar-with-dependencies.

1.code

The code of robot-utils4j:

package org.mfm.robot.utils4j;

import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

public class RedisClient {

        private static RedissonClient  instance = null; 

        /**
         * 获取连接redis的Session
         * @param clusterNodes "redis://127.0.0.1:7000", "redis://127.0.0.1:7001"
         * @param password abc123
         * @return
         */
        public RedissonClient getRedissonClient(String clusterNodes,String password) {
            Config config = new Config();
            config.useClusterServers()
                .setScanInterval(2000) 
                .setPassword(password)
                .addNodeAddress(clusterNodes.split(","));
            if(instance == null) {
                instance = Redisson.create(config);
            }
            return instance;
        }

        public static void main(String[] args) {
            RedisClient redisClient = new RedisClient();
            RedissonClient redissonClient = redisClient.getRedissonClient("redis://172.16.2.132:7001, redis://172.16.2.133:7002,redis://172.16.2.134:7003", "keplerredis");
            System.out.println(redissonClient+"连接成功。。。。");
        }

        public String getKey(String key) {
            RBucket<String> rvalue = instance.getBucket(key);
            System.out.println("获取key"+key+"的值"+rvalue.get());
            return rvalue.get();
        }

        public void setKeyValue(String key,String value) {
            RBucket<String> rvalue = instance.getBucket(key);
            System.out.println("获取key"+key+"的值"+rvalue.get());
            rvalue.set(value);
        }
}

2.import Library at red.xml

add new library in Refrenceed Library view.

click robot-utils4j-1.0.0-jar-with-dependencies library.

search org.mfm.robot.utils4j.RedisClient

you'd better right click it mark as static libary.

3.create Robot TestSutie

*** Settings ***
Library    org.mfm.robot.utils4j.RedisClient  

*** Test Cases ***
My Test
    ${connectResult}=    Get Redisson Client    redis://172.16.2.132:7001, redis://172.16.2.133:7002,redis://172.16.2.134:7003    keplerredis
    Log To Console    ${connectResult} 
  1. Run it

    ============================================================================== RobotDemo :: python.path
    ============================================================================== RobotDemo.RobotDemo
    ============================================================================== RobotDemo.RobotDemo.RedisDemoJava
    ============================================================================== My Test org.redisson.Redisson@10bdfbcc | PASS | ------------------------------------------------------------------------------ RobotDemo.RobotDemo.RedisDemoJava | PASS | 1 critical test, 1 passed, 0 failed 1 test total, 1 passed, 0 failed ============================================================================== RobotDemo.RobotDemo | PASS | 1 critical test, 1 passed, 0 failed 1 test total, 1 passed, 0 failed ============================================================================== RobotDemo :: python.path | PASS | 1 critical test, 1 passed, 0 failed 1 test total, 1 passed, 0 failed ==============================================================================

Upvotes: 0

Jinlxz Liu
Jinlxz Liu

Reputation: 463

  1. you need jython to run your tests, it means you need to install jython to run tests.
  2. no special action is needed when importing your java library.
  3. you need to tell jybot where to find your java library or classes.
    jybot --pythonpath <the_path_to_your_java_classes> your_tests
    //e.g.   
    jybot --pythonpath c:/test-project/target/classes  your_tests

Upvotes: 2

AnkitD
AnkitD

Reputation: 66

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

Upvotes: 1

Helio
Helio

Reputation: 3737

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

Upvotes: 0

Related Questions