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就可以啟動一個Library給Robot 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(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放到..\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開發的庫。雖然關系有點亂。但你要知道他們不是同類。
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:\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