使用Robot Test Automation Framework来控制只支持TCL libary的测试设备
2012-03-01 00:39
344 查看
有很多测试仪器都只提供了基于Tcl的library,比如SmartBits, Ixia, AX4000。在网络通信测试上,它们都是很常用的通信测试仪器,但是目前的Robot只支持Python,所以就会碰到一个问题,如果来控制这些测试设备,来完成自动化测试呢?
目前我能找到的,主要是两种做法,
两种方法的优缺点分析:
1.第一种方法要求执行自动化测试的机器本地必须安装Tcl以及测试仪器的控制library,有时候,如果对每台客户端都这么要求的话,可能会有点麻烦。优点应该是执行速度比较快,而且可以直接拿到 Tcl liabrary里面函数的返回值
2.第二种方法应该是用起来比较简单,写完的自动化测试脚本,在服务器上可以跑,在本地执行也比较方便。但缺点可能在于每次执行命令前,都需要telnet到服务器去,这样速度会比较慢。还有就是执行完Tcl命令之后,只能通过解析屏幕的输出,来得到你想要的返回值。
上面的想法只是初步想到的,以后在工作中如果有更多的经验,再总结吧
目前上述两种用法,在实际的工作中都有看到,如果我们的工作主要在网络通信测试,那应该会大量用到各种各样的测试仪器,如果我们又要使用Robot Framework的话,那仔细地研究上述两种做法,并比较其中的优劣,甚至考虑设计一种更灵活,兼有两种方法优点的做法,对于自动化测试的效率,应该还是蛮有帮助的。
下面是实际的示例代码,供参考:
1.第一种方法是实例化Tkinter.Tcl,然后在python里面直接调用Tcl语法
2. 第二种是设置一台中间的代理服务器,然后连到那个服务器上,通过Telnet或者SSH,然后通过Python执行命令并取得命令的返回值,再加以分析,以AX4000为例:
class Ax4000:
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
def __init__(self):
self._telnet_connections = {}
self._current = None
#_oldprompt_dict for contain the prompt before running tclsh
self._oldprompt_dict = {}
self._loglevel = "INFO"
def connect_to_ax4000(self, host, port = 23, user = "public", passwd = "public", prompt = None, lib_with_path='$env(AX4000LIB)', bios_path='$env(AX4000BIOS)'):
""" Connect to ax4000 tcl server where the ax4000 tcl library is installed.
For MS window the loading of ax4000 tcl library and identification of bios path
(ax hwdir) is done.
TO DO: The same (load and ax hwdir) should also be done for 'Linux'. Currently
for loading ax4000 tcl library, the keyword 'Setup Ax4000 Commands Environment'
should be used after connecting to AX4000 server.
| Input Paramaters | Man. | Description |
| host | Yes | Identifies to host |
| port | No | Allows to change the default telnet port |
| user | No | Authentication information |
| passwd | No | Authentication information |
| prompt | No | Allows to set the prompt |
| lib_with_path | No | Allow to define the ax4000 tcl path and library |
| | | e.g. C:/Program Files/tcl/ax4000/tclwin/libax4k.dll |
| bios_path | No | Allow to define the ax4000 bios path |
| | | e.g. C:/Program Files/tcl/ax4000/bios |
"""
目前我能找到的,主要是两种做法,
两种方法的优缺点分析:
1.第一种方法要求执行自动化测试的机器本地必须安装Tcl以及测试仪器的控制library,有时候,如果对每台客户端都这么要求的话,可能会有点麻烦。优点应该是执行速度比较快,而且可以直接拿到 Tcl liabrary里面函数的返回值
2.第二种方法应该是用起来比较简单,写完的自动化测试脚本,在服务器上可以跑,在本地执行也比较方便。但缺点可能在于每次执行命令前,都需要telnet到服务器去,这样速度会比较慢。还有就是执行完Tcl命令之后,只能通过解析屏幕的输出,来得到你想要的返回值。
上面的想法只是初步想到的,以后在工作中如果有更多的经验,再总结吧
目前上述两种用法,在实际的工作中都有看到,如果我们的工作主要在网络通信测试,那应该会大量用到各种各样的测试仪器,如果我们又要使用Robot Framework的话,那仔细地研究上述两种做法,并比较其中的优劣,甚至考虑设计一种更灵活,兼有两种方法优点的做法,对于自动化测试的效率,应该还是蛮有帮助的。
下面是实际的示例代码,供参考:
1.第一种方法是实例化Tkinter.Tcl,然后在python里面直接调用Tcl语法
class Smartbits : """This class can be used to config and query Smartbits.""" def __init__(self): """initializes each data member""" self.__root = Tkinter.Tcl() """__root: object to use tcl command.""" self.__tclob = None """__tclob: instance of class Smartbits.""" tclScript = """ package require libcmds package require smartlib package require Itcl package require Smartbits 0.9 return [Smartbits #auto] """ self.__sma = self.__root.tk.eval_r(tclScript) def connect(self,ip_addr): """ Connect to the smartbits @param ip_addr: smartbits ip address. @type ip_addr: string @rtype: 0/-1 @return: 0/-1,success/fail @version: 2007-02-18, Chen Xuan, Create. B{Example:} - C{connect('10.15.201.47')} """ tclScript = """ %s Connect -addr %s """%(self.__sma,ip_addr) return self.__root.tk.eval_r(tclScript)
2. 第二种是设置一台中间的代理服务器,然后连到那个服务器上,通过Telnet或者SSH,然后通过Python执行命令并取得命令的返回值,再加以分析,以AX4000为例:
class Ax4000:
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
def __init__(self):
self._telnet_connections = {}
self._current = None
#_oldprompt_dict for contain the prompt before running tclsh
self._oldprompt_dict = {}
self._loglevel = "INFO"
def connect_to_ax4000(self, host, port = 23, user = "public", passwd = "public", prompt = None, lib_with_path='$env(AX4000LIB)', bios_path='$env(AX4000BIOS)'):
""" Connect to ax4000 tcl server where the ax4000 tcl library is installed.
For MS window the loading of ax4000 tcl library and identification of bios path
(ax hwdir) is done.
TO DO: The same (load and ax hwdir) should also be done for 'Linux'. Currently
for loading ax4000 tcl library, the keyword 'Setup Ax4000 Commands Environment'
should be used after connecting to AX4000 server.
| Input Paramaters | Man. | Description |
| host | Yes | Identifies to host |
| port | No | Allows to change the default telnet port |
| user | No | Authentication information |
| passwd | No | Authentication information |
| prompt | No | Allows to set the prompt |
| lib_with_path | No | Allow to define the ax4000 tcl path and library |
| | | e.g. C:/Program Files/tcl/ax4000/tclwin/libax4k.dll |
| bios_path | No | Allow to define the ax4000 bios path |
| | | e.g. C:/Program Files/tcl/ax4000/bios |
"""
if prompt == None or prompt == '': print "prompt", prompt myprompt = [ "%s@.*\$ " % user, "\w:.*>" ] else: myprompt = prompt conn = TelnetConnection(host, port, myprompt, "60sec", "CR") conn.set_loglevel(self._loglevel) ret = conn.login(user, passwd, "login: ", [ "password: ", "Password: " ]) if ret.find("Microsoft") >= 0: self._telnet_connections[conn] = "MS" self._current = conn #record old prompt input dict with telnet connection as key self._oldprompt_dict[conn] = self._current.set_prompt("% ") self._current.write("tclsh") self._current.read_until_prompt() if self._check_if_environment_variable_defined(lib_with_path) == False or \ self._check_if_environment_variable_defined(bios_path) == False: lib_with_path, bios_path = self._get_library_and_bios_path() self.setup_ax4000_commands_environment(lib_with_path, bios_path) else: self._telnet_connections[conn] = "Linux" self._current = conn #record old prompt input dict with telnet connection as key self._oldprompt_dict[conn] = self._current.set_prompt("% ") self._current.write("tclsh") self._current.read_until_prompt() #LINUX BUG if self._check_if_environment_variable_defined(lib_with_path) == False or \ self._check_if_environment_variable_defined(bios_path) == False: lib_with_path, bios_path = self._get_library_and_bios_path() self.setup_ax4000_commands_environment(lib_with_path, bios_path) return conn
def execute_ax4000_command(self, command): """Executes a command within the tcl shell and checks for error. The command is automatically extended by a CR at the end. In case of a TCL exception a Runtime Error is raised. | Input Paramaters | Man. | Description | | command | Yes | The AX4000 TCL command to be executed | """ # catch TCL exception error_code = self.execute_ax4000_command_without_check("catch {%s} result" % command) # print "raw error", error_code error_code = error_code.splitlines()[1].strip() # print "error", error_code result = self.execute_ax4000_command_without_check('puts $result') # check if TCL exception occured #LINUX BUG if not error_code : error_code = '0' if error_code != '0': # self.disconnect_from_ax4000() raise RuntimeError('TCL Error: %s'%result) return result def execute_ax4000_command_without_check(self, command): """Executes a command within the tcl shell without checking for errors. The command is automatically extended by a CR at the end. | Input Paramaters | Man. | Description | | command | Yes | The AX4000 TCL command to be executed | """ self._current.write(command) return self._current.read_until_prompt()
相关文章推荐
- Robot Framework For Mobile Test Automation
- 解决使用robot framework + selenium测试时,RIDE运行一次后不显示log的问题
- MSDN Webcast - 使用Windows Mobile Test Framework进行自动化测试(Level 300)
- 关于自动化测试环境的集成(Jenkins+RobotFramework+TestLink+SVN)
- QTP自动化测试框架系列一【复合测试框架(Hybrid Test Automation Framework)】
- MSDN Webcast 预告:使用Windows Mobile Test Framework进行自动化测试
- 解决使用robot framework + selenium测试时,RIDE运行一次后不显示log的问题
- 软件测试自动化之路(7)----在Web GUI Automation Test中使用JavaScript应注意的问题
- 使用Windows Mobile Test Framework进行Windows Mobile程序的自动化测试
- Xcode6.2在终端使用Automation进行自动化测试
- python 使用unittest 测试接口
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码(详解)
- Google Test(GTest)使用方法和源码解析——模板类测试技术分析和应用
- 使用 sys.sp_testlinkedserver 测试Linked Server 连接是否正常
- Parasoft C++test使用教程:关于测试用例(二)
- Linux 视频设备驱动V4L2最常用的控制命令使用说明(1.02)
- 构建android UI automation test测试环境
- Linux 视频设备驱动V4L2最常用的控制命令使用说明(1.02)
- 使用Windows Mobile Test Framework进行Windows Mobile程序的自动化测试 - (1)微软Windows Mobile Test Framework简介
- html5 websocket的原理,只能在支持的设备上使用