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

相關文章
相關標籤/搜索