Robot Framework 自动化框架 - 定制自己的library
Robot 自動(dòng)化框架內(nèi)置提供了一些library,如OperatingSystem(包含一些常用的的文件操作關(guān)鍵字,如copy文件,創(chuàng)建目錄),Telent,Screenshot,String,另外還有一些第三提供的library ,比較常用的如SeleniumLibrary,用于Web自動(dòng)化測(cè)試。但如何定制適合自己項(xiàng)目需求的library呢?
- 支持的編程語(yǔ)言
支持的語(yǔ)言包括:Python & Java. Robot框架本身就是有Python開(kāi)發(fā),理所當(dāng)然支持Python來(lái)實(shí)現(xiàn)library。當(dāng)運(yùn)行時(shí),選擇了Jybot,那么你也可以用Java來(lái)實(shí)現(xiàn)library。
- Robot框架提供了三種不同的實(shí)現(xiàn)library的API
這是最簡(jiǎn)單的方式,實(shí)現(xiàn)一個(gè)python module,在這個(gè)module里面有一些functions,或則在module里面有個(gè)python 的類(lèi),里面有一些方法。通過(guò)Java來(lái)實(shí)現(xiàn)的話(huà),提供一個(gè)Class,里面有些public 的method。當(dāng)import 這個(gè)python module, python class或java class這些method就會(huì)自動(dòng)映射為keyword。
有一點(diǎn)需要注意的是,如何判斷一個(gè)keyword的狀態(tài),當(dāng)執(zhí)行這個(gè)keyword時(shí),是Pass,還是Fail呢?如果在方法里面拋出了異常,那么這個(gè)keyword的狀態(tài)就是Fail.
下面先來(lái)看看之前寫(xiě)的一個(gè)library,截取了一段代碼:
class RemoveSMDLibrary(RuntimeError):'''A test library providing keyword for SMLD uninstallation related tasks.'''#Test library scopeROBOT_LIBRARY_SCOPE = 'GLOBAL'#specifiying library versionROBOT_LIBRARY_VERSION = '0.2'ROBOT_EXIT_ON_FAILURE = Truedef __init__(self,is_windows=-1):self._is_windows=is_windowssys.path.append("MyConfigParser.py")logger.console('Add MyConfigParser.py file into Python path!')def get_smdsys_path(self,msg=None):logger.console('Get smdsys.ini File...')winsmdsysPath = os.path.expandvars('$systemroot\\smdsys.ini')nonwinsmdsysPath = "/etc/smdsysV2.ini"if os.path.isfile(winsmdsysPath):self._is_windows = 1self._smdsyspath = winsmdsysPathelif os.path.isfile(nonwinsmdsysPath):self._is_windows = 0self._smdsyspath = nonwinsmdsysPathif self._is_windows ==-1: if not msg:msg="File '%s' does not exist" %winsmdsysPathraise AssertionError(msg) def _get_windows_path(self,smdsysPath):config = ConfigParser()config.read(smdsysPath)productPath = config.get("SMDConf","ProductPath")notesiniPath = config.get("DomSvr0","DomSvr0NotesIniPath")return productPath,notesiniPathdef _get_nonwindows_path(self,smdsysPath):config = ConfigParser()config.read(smdsysPath)SMDInstanceList = config.get("SMDInstances","SMDInstanceList")productPath = config.get(SMDInstanceList,"ProductPath")DomSvr = config.get(SMDInstanceList,"DomSvrISMDSecs")notesiniPath = config.get(SMDInstanceList,DomSvr)return productPath,notesiniPath def get_notesini_path(self):if self._is_windows == 1:return self._get_windows_path(self._smdsyspath) else:return self._get_nonwindows_path(self._smdsyspath) ?當(dāng)一個(gè)Pyhton class中,會(huì)忽略以_開(kāi)頭的function,不認(rèn)為是keyword。
看一下實(shí)際應(yīng)用在ride中如何導(dǎo)入:
因?yàn)镻ython module名和class的名字是一樣的,所以可以直接用module名,如果不一樣,就需要以這樣的格式來(lái)導(dǎo)入mymodule.myclass來(lái)導(dǎo)入library。看下這個(gè)參數(shù),這個(gè)參數(shù)是傳遞給構(gòu)造函數(shù)的。
如果你的library導(dǎo)入成功了,那么這些library中的keyword顏色就會(huì)變成這樣,把鼠標(biāo)放上去,按ctrl就是出現(xiàn)提示。
? 2.? Dynamic API
在keywords狀態(tài),logging和返回值方面,dynamic library API 和static library API是一樣的。唯一的不同是Robot Framework如何判別導(dǎo)入的library里面的keywords。static library API是通過(guò)反射機(jī)制來(lái)實(shí)現(xiàn)的,dynamic library采用一種特別的方式。
就static library keywords而言,所有的keywords必須在一個(gè)class,或modules中。而dynamic library API,你的keywords可以分布在不同的class中。
Dynamic API中必須實(shí)現(xiàn)兩個(gè)方法:run_keyword 和 get_keyword_names,Robot Framework通過(guò)這兩個(gè)方法,得知在library實(shí)現(xiàn)了哪些keyword,怎么調(diào)用這些keyword.
有個(gè)第三方的庫(kù)JavalibCore,實(shí)現(xiàn)了run_keyword和get_keyword_names,用戶(hù)只需要實(shí)現(xiàn)自己的keyword就可以了。這里就不舉例子了,建議看javalibcore的源碼。
3. Hybrid API
Hybrid library API是間于static API, dynamic API之間的。
和dynamic library API 一樣,你需要提供一個(gè)get_keyword_names方法,來(lái)返回這個(gè)library 可以提供的所有keywords的名字。還有一點(diǎn),Hybrid library API 只適用于Python,對(duì)于Java不可以的。
下面之間看一個(gè)例子,這個(gè)library的實(shí)現(xiàn)就是采用的Hybrid API方式。
class Smdauto(RuntimeError):'''A test library providing keywords for SMLD Automation'''ROBOT_LIBRARY_SCOPE = 'GLOBAL'ROBOT_LIBRARY_VERSION = '0.1'ROBOT_EXIT_ON_FAILURE = Truedef __init__(self):locator=KeywordsServiceLocator()#kw={ 'tracefile' : sys.stdout, 'auth' : ( AUTH.httpbasic, 'test1', '111111' ) }#self._port_type=locator.getKeywords(**kw)self._port_type=locator.getKeywords()self._notes_mail = Noneself._lib_kws=self._notes_mail_kws=Nonedef get_keyword_names(self):return self._get_library_keywords() + self._get_notes_mail_keywords()def _get_library_keywords(self):if self._lib_kws is None:self._lib_kws = [ name for name in dir(self)if not name.startswith('_') and name != 'get_keyword_names'and inspect.ismethod(getattr(self, name)) ]return self._lib_kwsdef _get_notes_mail_keywords(self):if self._notes_mail_kws is None:notes_mail=self._get_notes_mail()self._notes_mail_kws=[ name for name in dir(notes_mail)if not name.startswith('_') and inspect.ismethod(getattr(notes_mail, name)) ]return self._notes_mail_kwsdef __getattr__(self,name):if name not in self._get_notes_mail_keywords():raise AttributeError(name)notes_mail = self._notes_mail is None and self._get_notes_mail() or self._notes_mailprint dir(notes_mail)return getattr(notes_mail,name)def _get_notes_mail(self):return NotesMail()其實(shí),Hybrid API的實(shí)質(zhì)就是應(yīng)用Python中委派機(jī)制,即__getattr__內(nèi)置函數(shù),當(dāng)嘗試調(diào)用一個(gè)不存在的方法時(shí),Python會(huì)默認(rèn)調(diào)用__getattr__。
首先,先來(lái)看get_keyword_names,這個(gè)方法返回了這個(gè)libray包含的所有的keywords,它調(diào)用了有兩個(gè)method,第一個(gè)返回這個(gè)class中所有不是以_開(kāi)頭的方法名,另一個(gè)返回一個(gè)額外的class中的方法。當(dāng)執(zhí)行的method不在這個(gè)class中的時(shí)候,就會(huì)調(diào)用__getattr__,從而實(shí)現(xiàn)委派調(diào)用。
?
參考資料:http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.7.4#remote-library-interface
轉(zhuǎn)載于:https://www.cnblogs.com/matt123/archive/2012/09/15/2687078.html
總結(jié)
以上是生活随笔為你收集整理的Robot Framework 自动化框架 - 定制自己的library的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Sliverlight好教程
- 下一篇: iOS - 让view触发点击事件