Robot Framework自动化测试(六)--- robotremoteserver使用

robotremoteserver 是什么?

   Python Remote Server for Robot Framework

 下载地址:https://pypi.python.org/pypi/robotremoteserver/

  robotremoteserver是一种远程库接口技术(remote library interface)。其实,通过这两天的使用,我的理解它就是一个远程库的容器。这看上去有点不太好理解,我们知道当我要使用的Robot Framework的库是被安装在..Python27Libsite-packages目录下面的。例如常用的Selenium2Library。

  但robotremoteserver就可以启动一个Library给Robot Framework用,不管这个库在本机的任何位置,或远程的某台主机上,或者这个库不是Python开发的。这听上去有点意思,对吧!

如何使用robotremoteserver                                      

  通过上面的连接将robotremoteserver 下载下来,注意不要使用pip安装,它其实也就只有一个robotremoteserver.py文件,我们需要的也就是这个文件而已。

  先来体验一下它的用法。

  首先创建一个目录E: fremote ,目录名你可以随便取。然后,将robotremoteserver.py拷贝到该目录下。接着在该目录下创建CountLibrary.py文件。

复制代码
#coding=utf-8
import sys    
from robotremoteserver import RobotRemoteServer    

    
class CountLibrary:

    def add(self,a,b):
        '''Computing a and b are two numbers together, for example:
        |    add     |   2    |    5     |
        '''
        return a + b

    def sub(self,a,b):
        '''Computing a and b subtract two numbers, for example:
        |    sub     |   10    |    2     |
        '''
        return a - b
    
    
if __name__ == '__main__':    
    CL = CountLibrary()    
    RobotRemoteServer(CL, *sys.argv[1:])
复制代码

  代码很简单,创建了一个计算类CountLibrary。实现了add()和sub()两个方法,用于计算两个的加法和减法。最后将CountLibrary放到RobotRemoteServer中。

  通过python命令执行该CountLibrary.py文件

现在,启动Robot Framework RIDE,导入“Remote”库。

 

按键盘F5 ,就可以看到Remote库中的“关键字”了。

看!现在你就可以使用。Add 和 Sub  两个关键字了,Stop Remote Server 是由robotremoteserver提供的,用户关闭库的容器。

然而,这貌似没有什么卵用。我为什么不把CountLibrary.py放到..Python27Libsite-packages目录下面调用呢!?

远程调用robotremoteserver                                    

  如果你细心会看到,刚才使用python命令启动CountLibrary.py的时候,启动是一个Remote server 并且指定127.0.0.1:8270 本机。

  那么这个Library其实也可以在远程的某台主机上启动。

  下面把整个rfremote目录拷贝到虚拟机或远程某台主机。通过“ipconfig”或“ifconfig”查看IP地址。我们假设远程的这台主机的IP是:192.168.31.179 。

打开robotremoteserver.py修改host:

复制代码
……
class RobotRemoteServer(SimpleXMLRPCServer):
    allow_reuse_address = True
    _generic_exceptions = (AssertionError, RuntimeError, Exception)
    _fatal_exceptions = (SystemExit, KeyboardInterrupt)

    def __init__(self, library, host='192.168.31.179', port=8270, port_file=None,
                 allow_stop=True):
……
复制代码

 好了!现在你的远程主机上通过python命令启动CountLibrary.py文件。

 然后,在本机上再次启动Robot Framework RIDE 

因为是远程库,所以,在引入这个库时要指定它是远程的IP和端口号。

然后,这依然没有什么卵用。下面就用它做点有卵用的事儿。

调用Sikuli                                                                                                        

  关于sikuli的介绍,请参考:http://www.cnblogs.com/fnng/archive/2012/12/15/2819367.html

  这是一种另类的自动化技术,有它的缺点,也有它的优,如果能与现有的Robot Framework工具结合,无疑是比较牛X的说。

  那么问题来了,sikuli虽然内部使用了python开发(也不是全python),但它是个jar包,也就是说它是由Java打包,只能给java调用。而Robot Framework是由纯python开发,只能引用python开发的库。虽然关系有点乱。但你要知道他们不是同类。

  Jython是Python与Java 之间的红娘。Jython基于jvm虚拟机开发的Python语法。通过它可以调用Java程序或Java的标准库。

Jython下载地址:http://www.jython.org

安装(需要有java环境): > java -jar jython-installer-2.7.0.jar

使用Jython

其实,Jython也可以当Python用,我们一般用的python是基于C实现的,而Jython是基于JVM实现的python,基于JVM的语言很多,比如Groovy 、JRuby 等。

得到sikuli-script.jar 包,它可以看作是sikuli的核心模块。

两种方法:

单独下载:http://download.csdn.net/download/hqd1986/4557974

安装sikuli  http://www.sikuli.org/downloadrc3.html ,在安装目录下找到sikuli-script.jar 文件。然后将其拷贝到E: fremote 目录并解压。

接下来在rfremote目录下创建SikuliLibrary.py文件。

复制代码
import sys    
from robotremoteserver import RobotRemoteServer    
from org.sikuli.script import *    
    
class SikuliLibrary:    
    
    def __init__(self):    
        self.SS = Screen()    
        self.PT = Pattern()    
    
    def _wait(self, imgFile, timeOut, similarity):    
        try:    
            self.PT = Pattern(imgFile)    
            self.PT = self.PT.similar(float(similarity))    
            self.SS.wait(self.PT, float(timeOut))    
        except FindFailed, err:    
            print "ERR: _wait"    
            raise AssertionError(err)    
    
    def click_object(self, imgFile, timeOut, similarity):    
        try:    
            self._wait(imgFile, timeOut, similarity)    
            self.SS.click(imgFile)    
        except FindFailed, err:    
            raise AssertionError("Cannot click [" + imgFile + "]")    
    
    def object_exists(self, imgFile, similarity, timeOut):    
        try:    
            self._wait(imgFile, timeOut, similarity)    
        except FindFailed, err:    
            raise AssertionError("Could not find [" + imgFile + "]")    
    
    def type_at_object(self, imgFile, txt, timeOut, similarity):    
        try:    
            self._wait(imgFile, timeOut, similarity)    
            self.SS.type(imgFile, txt)    
        except FindFailed, err:    
            raise AssertionError("Cannot type at [" + imgFile + "]")    
    
    def paste_at_object(self, imgFile, txt, timeOut, similarity):    
        try:    
            self._wait(imgFile, timeOut, similarity)    
            self.SS.paste(imgFile, txt)    
        except FindFailed, err:    
            raise AssertionError("Cannot paste at [" + imgFile + "]")    
    
if __name__ == '__main__':    
    SL = SikuliLibrary()    
    RobotRemoteServer(SL, *sys.argv[1:])
复制代码

 这个程序是关键,通过Jython第调用了org.sikuli.script.* 中的方法重新实现。可以理解成,调用java程序,重新实现成python程序,然后给python程序使用。

 这一次用需要使用Jython运行该文件。

然后,再次启动Robot Framework RIDE

把要操作的对象截好图:

然后,在Robot Framework中调用这些图片。

过程很简单,就是点击“开始”菜单,打开chrome浏览器。

参考:

http://blog.sina.com.cn/s/blog_654c6ec70101044p.html

http://blog.csdn.net/xc5683/article/details/11189259

原文地址:https://www.cnblogs.com/zhangfeivip/p/10110811.html