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

简介:

robotremoteserver 是什么?

   Python Remote Server for Robot Framework

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

 

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

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

 

如何使用robotremoteserver                                      

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

  先来体验一下它的用法。

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

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

    
class CountLibrary:

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

    def sub(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放到..\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浏览器。

 

 

目录
相关文章
|
3月前
|
JSON 自然语言处理 机器人
接口自动化测试教程:如何使用 Robot Framework
Robot Framework 是一个用于实现自动化测试和机器人流程自动化(RPA)的开放源代码框架。它由一个名为 Robot Framework Foundation 的组织得到推广,得到了多家领军企业在软件开发中的广泛应用。框架以其开放性和灵活性为特点,能够无缝整合各种其他工具,无论团队规模大小,均无需承担额外许可成本。
|
9月前
|
数据可视化 机器人 测试技术
自动化测试 RobotFramework安装以及使用教程
自动化测试 RobotFramework安装以及使用教程
|
JSON 数据管理 测试技术
RobotFrameWork编写接口测试及如何断言
RobotFrameWork编写接口测试及如何断言
111 0
RobotFrameWork编写接口测试及如何断言
|
自然语言处理 机器人
Robot Framework(8)- Collections 测试库常用的关键字列表
Robot Framework(8)- Collections 测试库常用的关键字列表
100 0
Robot Framework(8)- Collections 测试库常用的关键字列表
|
机器人
Robot Framework(7)- DateTime 测试库常用的关键字列表
Robot Framework(7)- DateTime 测试库常用的关键字列表
476 0
Robot Framework(7)- DateTime 测试库常用的关键字列表
|
机器人
Robot Framework(6)- BuiltIn 测试库常用的关键字列表
Robot Framework(6)- BuiltIn 测试库常用的关键字列表
216 0
Robot Framework(6)- BuiltIn 测试库常用的关键字列表
|
XML 机器人 Java
Robot Framework(5)- 使用测试库
Robot Framework(5)- 使用测试库
229 0
Robot Framework(5)- 使用测试库
|
机器人 测试技术 Python
Robot Framework(4)- 测试套件的基本使用
Robot Framework(4)- 测试套件的基本使用
433 0
Robot Framework(4)- 测试套件的基本使用
|
移动开发 小程序 机器人
关于《自动化测试实战宝典:Robot Framework + Python从小工到专家》
互联网行业的工程师就好比运动员,要想在竞技场 上获胜,需要在训练场里长期刻苦地练习技巧,想要成为一个不被时代抛弃的技术人,就需要不断地更新迭代自己的知识体系。
1419 0
|
移动开发 机器人 测试技术
Robot Framework+Appium之微信H5自动化测试
前言 总会有人问微信的自动化测试怎么做。其实我不太明白,为啥你要对ta做自动化测试啊,除非你们公司产品是基于微信做的开发否则没必要。即使一个公众号我也觉得没必要做自动化测试,基本功能点下没问题就可以了,毕竟都是配置的,除非你后台配置错了。
3023 0