Robot Framework 自动化框架 - 定制自己的library
Robot 自动化框架内置提供了一些library,如OperatingSystem(包含一些常用的的文件操作关键字,如copy文件,创建目录),Telent,Screenshot,String,另外还有一些第三提供的library ,比较常用的如SeleniumLibrary,用于Web自动化测试。但如何定制适合自己项目需求的library呢?
- 支持的编程语言
支持的语言包括:Python & Java. Robot框架本身就是有Python开发,理所当然支持Python来实现library。当运行时,选择了Jybot,那么你也可以用Java来实现library。
- Robot框架提供了三种不同的实现library的API
- Static API
这是最简单的方式,实现一个python module,在这个module里面有一些functions,或则在module里面有个python 的类,里面有一些方法。通过Java来实现的话,提供一个Class,里面有些public 的method。当import 这个python module, python class或java class这些method就会自动映射为keyword。
有一点需要注意的是,如何判断一个keyword的状态,当执行这个keyword时,是Pass,还是Fail呢?如果在方法里面抛出了异常,那么这个keyword的状态就是Fail.
下面先来看看之前写的一个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)
当一个Pyhton class中,会忽略以_开头的function,不认为是keyword。
看一下实际应用在ride中如何导入:
因为Python module名和class的名字是一样的,所以可以直接用module名,如果不一样,就需要以这样的格式来导入mymodule.myclass来导入library。看下这个参数,这个参数是传递给构造函数的。
如果你的library导入成功了,那么这些library中的keyword颜色就会变成这样,把鼠标放上去,按ctrl就是出现提示。
2. Dynamic API
在keywords状态,logging和返回值方面,dynamic library API 和static library API是一样的。唯一的不同是Robot Framework如何判别导入的library里面的keywords。static library API是通过反射机制来实现的,dynamic library采用一种特别的方式。
就static library keywords而言,所有的keywords必须在一个class,或modules中。而dynamic library API,你的keywords可以分布在不同的class中。
Dynamic API中必须实现两个方法:run_keyword 和 get_keyword_names,Robot Framework通过这两个方法,得知在library实现了哪些keyword,怎么调用这些keyword.
有个第三方的库JavalibCore,实现了run_keyword和get_keyword_names,用户只需要实现自己的keyword就可以了。这里就不举例子了,建议看javalibcore的源码。
3. Hybrid API
Hybrid library API是间于static API, dynamic API之间的。
和dynamic library API 一样,你需要提供一个get_keyword_names方法,来返回这个library 可以提供的所有keywords的名字。还有一点,Hybrid library API 只适用于Python,对于Java不可以的。
下面之间看一个例子,这个library的实现就是采用的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()
其实,Hybrid API的实质就是应用Python中委派机制,即__getattr__内置函数,当尝试调用一个不存在的方法时,Python会默认调用__getattr__。
首先,先来看get_keyword_names,这个方法返回了这个libray包含的所有的keywords,它调用了有两个method,第一个返回这个class中所有不是以_开头的方法名,另一个返回一个额外的class中的方法。当执行的method不在这个class中的时候,就会调用__getattr__,从而实现委派调用。
参考资料:http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.7.4#remote-library-interface
转载于:https://www.cnblogs.com/matt123/archive/2012/09/15/2687078.html
Robot Framework 自动化框架 - 定制自己的library相关推荐
- Robot Framework 自动化框架大纲
Python + Robot Framework 环境搭建 Android SDK + Appium 环境搭建 RobotFramework - AppiumLibrary 之元素定位 Robot ...
- robot framework自动化框架——多重继承
在看DatabaseLibrary时,突然发现Query类里使用到了ConnectionManager类里面的变量_dbconnection,很不能理解,Query类里没有对ConnectionMan ...
- Robot Framework自动化测试框架核心指南-如何使用Java编写自定义的RobotFramework Lib
如何使用Java编写自定义的RobotFramework Lib 本文包括2个章节 1. Robot Frdamwork中如何调用java Lib库 2.使用 java编写自定义的Lib 本文作者为: ...
- Robot Framework 自动化测试框架核心指南-初识Robot Framework(学习笔记2)
初识Robot Framework 1.1如何创建一个自动化测试项目 1.1.2 创建测试套件 1.1.3 创建测试用例 1.2 Robot Framework基础关键字 1.2.1 如何搜索Robo ...
- Robot Framework + Pywinauto 框架实现Windows GUI Automation
作者:Daly 出处:http://www.cnblogs.com/daly 欢迎转载,也请保留这段声明.谢谢! 最近刚给Windows GUI automation项目介的实现框架,总结记录一下, ...
- Robot Framework + Selenium 框架,关键字封装,知识点记录
目录 框架概述: 通用关键字: 等待类关键字: 通用组件 等待 元素: 通用组件 获取 元素居中滚动像素: 通用组件 页面滚动 : 通用组件 等待包含文字: 通用组件 自动切换frame : 通用组件 ...
- 【Android自动化测试】Robot Framework+adb框架(五)——L1层测试用例
前言 Robot Framework是一款用Python编写的通用型测试框架,具有良好的可扩展性,支持关键字驱动,可以同时测试多种类型客户端(Web,PC,移动端)或者接口,也可以进行分布式测试执行. ...
- Robot Framework 实战中学习(Web自动化)
Robot Framework 自动化框架可以实现的功能及其丰富,首先从Web自动化开始总结学习(由于引用Selenium2Library,因此与Selenium页面元素定位及操作类似),后面也会依次 ...
- 火影推荐程序连载23-Robot Framework自动化测试框架核心指南-如何做好自动化测试平台框架的设计
自动化测试如果需要能高效快速的支撑软件项目的测试,项目的快速迭代以及上线,除了以上我们介绍的需要许多的Lib来支持以及需要高效的去编写自动化测试案例外,还需要一个好的自动化测试框架平台来支撑我们的自动 ...
最新文章
- 【Libevent】Libevent学习笔记(三):事件循环
- 拿 C# 搞函数式编程 - 2
- 微信小程序 - 骨架屏
- 前端学习(3059):vue+element今日头条管理-优化文章状态
- 洛谷 U3357 C2-走楼梯
- 03-windows下Oracle的下载与安装
- 图解Python 3.x多继承时方法解析顺序MRO
- Linux 命令(114)—— nl 命令
- shell逻辑运算符优先级_linux shell 逻辑运算符、逻辑表达式详解
- GeoDa空间计量(一)——空间权重矩阵的生成
- java电商ERP源码 跨境电商ERP源码 ERP源码
- Java性能优化攻略详解
- js实现省市名称简称 ,echarts全国地图省市名称简称
- 云计算是用来做什么的
- 高位十字星收盘什么意思?高位十字星缩量是什么?
- 网络安全红蓝军对抗完整战术周期
- 互联网快讯:猿辅导推品牌“飞象星球”入局TO B市场;京东云首次发布物联网全景图;苹果更新App Store规则
- 【location】将url参数转换为对象的形式
- 地理国情监测类毕业论文文献都有哪些?
- 然而沼跃鱼早就看穿了一切 hihoCoder1082