设置RobotFramework的ftplibrary中,将Upload_file操作的异常改为回显错误信息。
测试中需要通过FTP通道,将数据发送给服务器,而这个上传的数据要被阻断。在结合RobotFramework测试中,安装的ftplibrary,使用upload_file操作,如果上传动作失败,会抛出异常,使Robotframework测试失败。
而其实我们就是想让他上传失败,不要抛出异常,导致测试失败。这时需要更改下ftplibrary.py。也是非常简单的,我简单记录下:
首先,下载ftplibrary安装包,下载地址:https://pypi.python.org/pypi/robotframework-ftplibrary/1.3
然后执行: Python setup.py install,将其安装。默认应该安装在\Python27\Lib\site-packages\ftplibrary.py.
打开文件后,可以看到upload_file函数的异常处理:
def upload_file(self, localFileName, remoteFileName=None, connId='default'):
thisConn = self.__getConnection(connId)
outputMsg = ""
remoteFileName_ = ""
localFilePath = os.path.normpath(localFileName)
if not os.path.isfile(localFilePath):
raise FtpLibraryError("Valid file path should be provided.")
else:
if remoteFileName==None:
fileTuple = os.path.split(localFileName)
if len(fileTuple)==2:
remoteFileName_ = fileTuple[1]
else:
remoteFileName_ = 'defaultFileName'
else:
remoteFileName_ = remoteFileName
try:
outputMsg += thisConn.storbinary("STOR " + remoteFileName_, open(localFilePath, "rb"))
except ftplib.all_errors as e:
raise FtpLibraryError(str(e))
if self.printOutput:
logger.info(outputMsg)
return outputMsg
它引用了Python自带的ftplib库(\Python\27\Lib\ftplib.py):
打开ftplib.py可以看到对于异常的处理:
# Exception raised when an error or invalid response is received
class Error(Exception): pass
class error_reply(Error): pass # unexpected [123]xx reply
class error_temp(Error): pass # 4xx errors
class error_perm(Error): pass # 5xx errors
class error_proto(Error): pass # response does not begin with [1-5] # All exceptions (hopefully) that may be raised here and that aren't
# (always) programming errors on our side
all_errors = (Error, IOError, EOFError)
因为FTP上传文件被阻止后,返回的错误代码是553。所以只需要将Upload_file函数的异常处理再添加一个对“5XX”(class error_perm(Error): pass # 5xx errors)的判断即可。
下面是添加后的样子(红色是添加的异常判断):
def upload_file(self, localFileName, remoteFileName=None, connId='default'):
"""
Sends file from local drive to current directory on FTP server in binary mode.
Returns server output.
Parameters:
- localFileName - file name or path to a file on a local drive.
- remoteFileName (optional) - a name or path containing name under which file should be saved.
- connId(optional) - connection identifier. By default equals 'default'
If remoteFileName agument is not given, local name will be used.
Examples:
| upload file | x.txt | connId=ftp1 |
| upload file | D:/rfftppy/y.txt | |
| upload file | u.txt | uu.txt |
| upload file | D:/rfftppy/z.txt | zz.txt |
| upload file | D:\\rfftppy\\v.txt | |
"""
thisConn = self.__getConnection(connId)
outputMsg = ""
remoteFileName_ = ""
localFilePath = os.path.normpath(localFileName)
if not os.path.isfile(localFilePath):
raise FtpLibraryError("Valid file path should be provided.")
else:
if remoteFileName==None:
fileTuple = os.path.split(localFileName)
if len(fileTuple)==2:
remoteFileName_ = fileTuple[1]
else:
remoteFileName_ = 'defaultFileName'
else:
remoteFileName_ = remoteFileName
try:
outputMsg += thisConn.storbinary("STOR " + remoteFileName_, open(localFilePath, "rb"))
except ftplib.error_perm as e:
return str(e)
except ftplib.all_errors as e:
raise FtpLibraryError(str(e))
if self.printOutput:
logger.info(outputMsg)
return outputMsg
然后再执行Robot framework测试时,就可以看到,文件被阻止了,但是测试成功,没有返回Fail:
20160316 11:07:52.446 : INFO : 220 (vsFTPd 2.2.2)230 Login successful.
20160316 11:07:52.694 : INFO :
${output} = 553-Requested action not taken
(by xxxx:10006)
553 END
20160316 11:07:52.698 : INFO :
553-Requested action not taken
(by xxxx:10006)
553 END
设置RobotFramework的ftplibrary中,将Upload_file操作的异常改为回显错误信息。的更多相关文章
- H5中input输入框tppe为date时赋值(回显)
1.当时间为2013-09-05时正常显示 <input class="form-control" name="applytime" type=" ...
- VC中使用ADO操作数据库的方法
源地址:http://blog.csdn.net/xiaobai1593/article/details/7459862 准备工作: (1).引入ADO类 #import "c:\progr ...
- 项目上线,php的错误信息必须不让其在页面中显示给客户,
对于PHP开发者来 说,一旦某个产品投入使用,应该立即将 display_errors选项关闭,以免因为这些错误所透露的路径.数据库连接.数据表等信息而遭到黑客攻击.但是,任何一个产品在投入使用后,都 ...
- ASP.Net MVC3/4中Model验证错误信息的本地化
最近使用ASP.Net MVC4做一个B/S的管理系统,里面有N多的Action和View Model,View Model上又有N多的验证. 一开始写的时候虽然知道要实现多语言,但是没有过多考虑,本 ...
- DVWA中SQL回显注入
一.SQL注入简介 1.1 SQL语句就是操作数据库的语句,SQL注入就是通过web程序在数据库里执行任意SQL语句. SQL 注入是一种常见的Web安全漏洞,攻击者利用这个漏洞,可以访问和修改数据, ...
- AD组策略添加本地账号、设置允许ping回显
AD组策略添加本地账号 1. 管理工具--组策略管理--选择相应GPO(编辑)----首选项--控制面板设置--本地用户和组--右键添加账号 2.域成员计算机刷新组策略(gpupdate/force) ...
- 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Application Data 中的媒体
[源码下载] 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Ap ...
- 关于JavaScript中的delete操作
关于JavaScript中的delete操作 看到一道题,是这样的: (function(x){ delete x; return x; })(1); 1 null undefined Error 我 ...
- 【Java EE 学习 32 下】【JQuery】【JQuey中的DOM操作】
一.JQuery中的DOM操作. 什么是DOM:DOM是一中和浏览器.平台.语言无关的接口,使用该接口可以轻松访问页面中所有的标准组件.DOM简称文档对象模型,是Document Oject Mode ...
随机推荐
- html颜色实体符号表示汇总
颜色的表示方法有许多种,列如black,#000000,rgb(0,0,0)都表示黑色.这三种表示方法分别为英文,十六进制,rgb格式.拥有下列颜色,足以使你的网页充满生机. 颜色名 十六进制颜色值 ...
- 根据html页面模板动态生成html页面(c#类)
本文转载自:http://www.cnblogs.com/yuanbao/archive/2008/01/06/1027985.html点击打开链接 一直以为动态生成静态页面不好做,昨天在网上找了下, ...
- js获取url参数方法
function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...
- php扩展开发-面向对象
在zval变量里IS_OBJECT类型使用zend_object_value来保存变量的,我们看一下他的具体结果. typedef struct _zend_object_value { zend_o ...
- Apache2服务配置ubuntu16.04+django1.11
话不多说直接上步骤 环境 Ubuntu 16.04 Python 3.5.2 Django 1.11 Apache 2.4 1.Apache2安装 sudo apt-get install apach ...
- sql查询平均下单时间
SQL查询订单平均审核时长 今天在写一个sql,需求是算一个订单在执行状态中的各个节点的时长 比如在订单中,状态0为开始接单,状态3为已经审核,那么现在需要计算每个客服的平均审核时长 像图中所示:这个 ...
- Dropping Balls(小球下落)
紫书P148,例题6-6 Sample Input 4 2 3 4 10 1 2 2 8 128 Sample Output 12 7 512 3 255 这应该不仅仅是一棵完全二叉树,题目中说保证所 ...
- 机器学习笔记(一)—— 线性回归问题与Matlab求解
给你多组数据集,例如给你很多房子的面积.房子距离市中心的距离.房子的价格,然后再给你一组面积. 距离,让你预测房价.这类问题称为回归问题. 回归问题(Regression) 是给定多个自变量.一个因变 ...
- 笔记-reactor pattern
笔记-reactor pattern 1. reactor模式 1.1. 什么是reactor模式 The reactor design pattern is an event han ...
- Android 自定义WebView 实现可以加载缓存数据
1.自定义WebView说明 1.1.这个WebView可以加载缓存的数据.(需要后端配合,将html转换成一个字符串,主要是图片要用特殊格式) 1.2.注入了图片链接,为了方便点击webView中的 ...