设置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 ...
随机推荐
- java后台输入数据的2种方式
java后台输入数据的2种方式 (1) import java.io.BufferedReader; import java.io.InputStreamReader; public class 输入 ...
- mongdb安装配置
一.先登录Mongodb官网https://www.mongodb.com/download-center#community 下载 安装包.32.64位的都行. 或者查看我的百度云(使用win7 ...
- JS常用数组方法及实例
1.join(separator):将数组的元素组起一个字符串,以separator为分隔符 ,,,,]; var b = a.join("|"); //如果不用分隔符,默认逗号隔 ...
- POJ:1017-Packets(贪心+模拟,神烦)
传送门:http://poj.org/problem?id=1017 Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- Android Html处理器通用类 HtmlUtil
1.整体分析 1.1.首先看一下源代码,可以直接Copy. public class HtmlUtil { /** * 获取 html 中的纯文本 */ public static String Ht ...
- 调用startActivityForResult后直接调用onActivityResult
人员都知道,可以经由过程应用 startActivityForResult() 和 onActivityResult() 办法来传递或接管参数. 然而在"轻听"项目中,还没比及被调 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目4
2014-03-20 03:08 题目:给定一个集合,返回其幂集. 解法:DFS. 代码: // 9.4 Return all subsets of a set #include <cstdio ...
- 《Cracking the Coding Interview》——第7章:数学和概率论——题目4
2014-03-20 02:16 题目:只用加法和赋值,实现减法.乘法.除法. 解法:我只实现了整数范围内的.减法就是加上相反数.乘法就是连着加上很多个.除法就是减到不能减为止,数数总共减了多少个. ...
- 【Deep Learning】林轩田机器学习技法
这节课的题目是Deep learning,个人以为说的跟Deep learning比较浅,跟autoencoder和PCA这块内容比较紧密. 林介绍了deep learning近年来受到了很大的关注: ...
- 最短路径(Floyd法)
最短路径法: 算法的主要思想是:单独一条边的路径也不一定是最佳路径. 从任意一条单边路径开始.所有两点之间的距离是边的权的和,(如果两点之间没有边相连, 则为无穷大). 对于每一对顶点 u 和 v,看 ...