RobotFramework-JavaRemoteLibrary扩展java远程库

java远程库可以纯java实现robot库扩展,不需要再用python封装(java本地库需要使用python再次封装;或者使用jython执行用例,jython对中文好像需要特殊处理还是怎么回事,jybot执行用例时中文会报错)

一、下载远程库需要的jar包:jrobotremoteserver-standalone-3.0

  这里面包含所有依赖

二、创建远程库项目,本例子中项目结构如下:
  • robot.remotekeywords
    • JavaRemoteLibrary.java
  • robot.remotekeywords.keywords
    • RemoteKeywordsForJava.java

  其中JavaRemoteLibrary.java是启动文件,RemoteKeywordsForJava.java是关键字库。

只有一个库的时候可以这样写:
多个库也可以如下写法,但是不会显示关键字描述
RemoteKeywordsForJava.java内容如下:

package robot.remotekeywords.keywords;

import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywords;

@RobotKeywords
public class RemoteKeywordsForJava {
	
	@RobotKeyword("return int1+int2")
	@ArgumentNames({ "int1", "int2" })
	public int add(int int1, int int2) {
		return int1 + int2;
	}

	@RobotKeyword("return int1-int2")
	@ArgumentNames({ "int1", "int2" })
	public int subtraction(int int1, int int2) {
		return int1 - int2;
	}
	
}

JavaRemoteLibrary.java内容如下:

package robot.remotekeywords;

import org.robotframework.javalib.library.AnnotationLibrary;
import org.robotframework.remoteserver.RemoteServer;

public class JavaRemoteLibrary extends AnnotationLibrary {
    public JavaRemoteLibrary() {
        // 关键字类库的位置
        super("robot/remotekeywords/keywords/*.class");
    }

    // 开启进程,打开远程库服务
    public static void main(String[] args) throws Exception {
        RemoteServer.configureLogging();
        RemoteServer server = new RemoteServer();
        server.putLibrary("/", new JavaRemoteLibrary());
//        server.putLibrary("/other", new OtherLibrary());  //这是添加第二个库,但是不会显示关键字描述
//        server.setHost("192.168.21.129");  //这句是需要远程引用时设置,本机引用可以忽略,直接localhost:8270引用就行
        server.setPort(8270);
        server.start();
    }

}

多个关键字库写法建议如下
JavaRemoteLibrary.java内容如下:

package robot.remotekeywords;
import org.robotframework.remoteserver.RemoteServer;

import robot.remotekeywords.keywords.RemoteKeywordsForJava;

public class JavaRemoteLibrary {

    /* Starts jrobotremoteserver with an example library and returns. The application will shutdown when all of the
     * web server's threads exit.
     */
    public static void main(String[] args) throws Exception {
        RemoteServer.configureLogging();
        RemoteServer server = new RemoteServer();
        server.putLibrary("/keywords", new RemoteKeywordsForJava());
//        server.putLibrary("/keywordssecond", new TestSecondLibrary());  //添加多个库,同时会显示关键字描述
//        server.setHost("192.168.21.129");  // 这句是需要远程引用时设置,本机引用可以忽略,直接localhost:8270引用就行
        server.setPort(8270);
        server.start();
    }

}

RemoteKeywordsForJava.java内容如下:

package robot.remotekeywords.keywords;

import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywords;
import org.robotframework.javalib.library.AnnotationLibrary;

@RobotKeywords
public class RemoteKeywordsForJava extends AnnotationLibrary {

	public RemoteKeywordsForJava() {
		// tell AnnotationLibrary where to find the keywords
		super("robot/remotekeywords/keywords/*.class");
	}

	@RobotKeyword("return int1+int2")
	@ArgumentNames({ "int1", "int2" })
	public int add(int int1, int int2) {
		return int1 + int2;
	}

	@RobotKeyword("return int1-int2")
	@ArgumentNames({ "int1", "int2" })
	public int subtraction(int int1, int int2) {
		return int1 - int2;
	}

}

三、打包

  需要打包成可执行jar包;
  打包时需要将依赖包解压,不然robot引用库的时候会因为找不到对应类报错,也就是在eclipse打包时选择Extract required libraries into generated JAR

四、启动服务
java -jar remote-keywords.jar

启动后会显示如下信息则表示成功:

0 [main] INFO org.robotframework.remoteserver.RemoteServer  - Mapped path / to library robot.remotekeywords.JavaRemoteLibrary.
0 [main] INFO org.robotframework.remoteserver.RemoteServer  - Robot Framework remote server starting
1 [main] INFO org.eclipse.jetty.server.Server  - jetty-7.x.y-SNAPSHOT
16 [main] INFO org.eclipse.jetty.server.handler.ContextHandler  - started o.e.j.s.ServletContextHandler{/,null}
233 [main] INFO org.eclipse.jetty.server.AbstractConnector  - Started SelectChannelConnector@0.0.0.0:8270
233 [main] INFO org.robotframework.remoteserver.RemoteServer  - Robot Framework remote server started on port 8270.

服务启动后可以在浏览器中输入http://localhost:8270/查看已经启动的库名字和路径,如图启动了两个库:
在这里插入图片描述

五、robot中引用

  Library Remote localhost:8270${PATH}
  其中${PATH}是关键字库文件相对于主文件的路径,就是上面server.putLibrary里面写的路径;
  一次只能引用一个路径下的库,多个路径下的库需要多次引用;
  多个库文件不能在同一个路径下,同一路径下引用多个库文件只会显示最后引入的库;
  现在只能本地引用,想远程引用设置IP为服务器IP

引用示例:

Library    Remote    localhost:8270/keywords    WITH NAME    RemoteKeywordsForJava
Library    Remote    localhost:8270/keywordssecond    WITH NAME    TestSecondLibrary

现在能开启服务并正常使用里面的关键字,但是只要执行用例服务端就会报错(执行结果正常没影响),报错内容如下,希望知道原因的大神指导下,谢谢:

个人猜测是robot调用了方法,而java库中没有关于这个使用或定义的原因,目前可以禁用库文件中的RemoteServer.configureLogging();这一句来禁用错误信息显示,但是这样什么错误信息都不会显示,不利于问题的查找

91997 [qtp391447681-17] ERROR org.apache.xmlrpc.server.XmlRpcErrorLogger  - No such handler: get_keyword_tags
org.apache.xmlrpc.server.XmlRpcNoSuchHandlerException: No such handler: get_keyword_tags
	at org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping.getHandler(AbstractReflectiveHandlerMapping.java:214)
	at org.apache.xmlrpc.server.XmlRpcServerWorker.execute(XmlRpcServerWorker.java:45)
	at org.apache.xmlrpc.server.XmlRpcServer.execute(XmlRpcServer.java:86)
	at org.apache.xmlrpc.server.XmlRpcStreamServer.execute(XmlRpcStreamServer.java:200)
	at org.apache.xmlrpc.webserver.XmlRpcServletServer.execute(XmlRpcServletServer.java:112)
	at org.apache.xmlrpc.webserver.XmlRpcServlet.doPost(XmlRpcServlet.java:196)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
	at org.robotframework.remoteserver.servlet.RemoteServerServlet.service(RemoteServerServlet.java:78)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:565)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:479)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1031)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:406)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:965)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
	at org.eclipse.jetty.server.Server.handle(Server.java:348)
	at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:452)
	at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:894)
	at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:948)
	at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:851)
	at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
	at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77)
	at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:606)
	at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:46)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:603)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:538)
	at java.lang.Thread.run(Unknown Source)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值