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

robotremoteserver 是什么?html

   Python Remote Server for Robot Frameworkjava

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

 

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

  但robotremoteserver就能够启动一个LibraryRobot Framework用,无论这个库在本机的任何位置,或远程的某台主机上,或者这个库不是Python开发的。这听上去有点意思,对吧!浏览器

 

如何使用robotremoteserver                                      jvm

  经过上面的链接将robotremoteserver 下载下来,注意不要使用pip安装,它其实也就只有一个robotremoteserver.py文件,咱们须要的也就是这个文件而已。工具

  先来体验一下它的用法。spa

  首先建立一个目录E:\rfremote\ ,目录名你能够随便取。而后,将robotremoteserver.py拷贝到该目录下。接着在该目录下建立CountLibrary.py文件。.net

#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中。3d

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

如今,启动Robot Framework RIDE,导入“Remote”库。

 

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

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

然而,这貌似没有什么卵用。我为何不把CountLibrary.py放到..\Python27\Lib\site-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开发的库。虽然关系有点乱。但你要知道他们不是同类。

  JythonPythonJava 之间的红娘。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:\rfremote\ 目录并解压。

接下来在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

相关文章
相关标签/搜索