在使用web自动化测试中,用例失败则自动截图的网上也有,但实际能落地的却没看到,现总结在在实际应用中失败截图的几种方法:

一、使用unittest框架截图方法:

  1、在tearDown中写入截图的功能,如下:

import sys
class SeleniumTest(unittest2.TestCase):
... def tearDown(self):
if sys.exc_info()[0]:
test_method_name = self._testMethodName
self.driver.save_screenshot("Screenshots/%s.png" % test_method_name)
super(SeleniumTest, self).tearDown()

注意,self._testMethodName是unittest特有的,在其它框架中不使用

在测试中,如果用例失败,就会自动以对应的测试函数名为文件名截图存储

2、自定义函数,重写unittest中的run方法:

class ScreenShotUtil:
"Screenshot Utility Class" @staticmethod
def take_screenshot(webdriver, file_name="error.png"):
"""
@param webdriver: WebDriver.
@type webdriver: WebDriver
@param file_name: Name to label this screenshot.
@type file_name: str
"""
if isinstance(webdriver, remote.webdriver.WebDriver):
# Get Screenshot over the wire as base64
base64_data = webdriver.get_screenshot_as_base64()
screenshot_data = base64.decodestring(base64_data)
screenshot_file = open(filename, "w")
screenshot_file.write(screenshot_data)
screenshot_file.close()
else:
webdriver.save_screenshot(filename)

在引中unittest时,重写run方法:

...
class ScreenCaptureTestCase(unittest.TestCase):
...
# Defining an init method so we can pass it a webdriver.
def __init__(self, methodName='runTest', webdriver=None, screenshot_util=None):
super(WDBaseTest, self).__init__(methodName) if webdriver_provider == None:
self._webdriver = WebDriverSingleton.get_instance()
else:
self._webdriver = webdriver if screenshot_util == None:
self._screenshot_util = WebScreenShotUtil
else:
self._screenshot_util = screenshot_util
...
def run(self, result=None):
"""
这里重写run方法
"""
orig_result = result
if result is None:
result = self.defaultTestResult()
startTestRun = getattr(result, 'startTestRun', None)
if startTestRun is not None:
startTestRun()
... more pyunit code ...
except self.failureException:
# Insert our Take Screenshot on test failure code.
fname = str(self).replace("(", "").replace(")", "").replace(" ", "_")
fmt='%y-%m-%d_%H.%M.%S_.PNG'
filename = datetime.datetime.now().strftime(fmt)
self._screenshot_util.take_screenshot(self._webdriver, filename)
result.addFailure(self, sys.exc_info())
... more pyunit code...
except:
# Do the same thing again for errors.
fname = str(self).replace("(", "").replace(")", "").replace(" ", "_")
fmt='%y-%m-%d_%H.%M.%S_.PNG'
filename = datetime.datetime.now().strftime(fmt)
self._screenshot_util.take_screenshot(self._webdriver, filename)
result.addError(self, sys.exc_info()) ...

完成后,测试时,失败则会已时间来截图,当然你也可以将filename按你喜好定义

二、nose框架失败截图

nose框架是unittest的优化,在实际使用中可以继承unittest.TestCase的方法后继续使用中的方法1,如果没导入unittest.TestCase时,也可以使用方法1,但要作小改动,如下:

1、tearDown写截图

class SeleniumTest(unittest2.TestCase):
... def tearDown(self):
if sys.exc_info()[0]:
test_method_name = test_names = [n for n in dir(self) if n.startswith('test_')][0] #注意了,这里与unittest的不一样
self.driver.save_screenshot("Screenshots/%s.png" % test_method_name)
super(SeleniumTest, self).tearDown()

2、修改nose源码,未完待续

python web自动化测试中失败截图方法汇总的更多相关文章

  1. 【Python】自动化测试框架-共通方法汇总

    1.滚动滚动条(有的时候页面元素element取得对但是并没有回显正确的数据,可能是因为页面第一次加载很慢,所以页面可能做了滚动到哪里就加载到哪里的效果,此刻我们就需要用到滚动条自动滚动这段代码让页面 ...

  2. Python+selenium自动化测试中Windows窗口跳转方法

    Python+selenium自动化测试中Windows窗口跳转方法 #第一种方法 #获得当前窗口 nowhandle=driver.current_window_handle #打开弹窗 drive ...

  3. Python 爬虫的代理 IP 设置方法汇总

    本文转载自:Python 爬虫的代理 IP 设置方法汇总 https://www.makcyun.top/web_scraping_withpython15.html 需要学习的地方:如何在爬虫中使用 ...

  4. Python Web自动化测试入门与实战,从入门到入行

    Python Web自动化测试入门与实战 购买地址 · 京东:https://item.jd.com/69239480564.html   天猫:https://detail.tmall.com/it ...

  5. Web Api中实现Http方法(Put,Post,Delete)

    在Web Api中实现Http方法(Put,Post,Delete) 系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在Web Api中,我 ...

  6. Python Web开发中的WSGI协议简介

    在Python Web开发中,我们一般使用Flask.Django等web框架来开发应用程序,生产环境中将应用部署到Apache.Nginx等web服务器时,还需要uWSGI或者Gunicorn.一个 ...

  7. JavaScript在web自动化测试中的作用

    前言 JS的全称JavaScript,是一种运行在浏览器中的解释型脚本语言,通常用来实现web前端页面的基本功能,对于前端开发人员是不得不掌握的一门基本技能,但是对于做web自动化测试的人员来说,如果 ...

  8. web自动化测试中绕开验证码登陆的方式

    web自动化测试中登陆需验证码是很大的一个困扰.现推荐一种简单的避开验证码登陆的方式,先代码进入登录页,人工输入验证码登录后浏览器自动保存cookie,再在新的标签中登录. 具体代码如下: publi ...

  9. Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)

    Python 在子类中调用父类方法详解(单继承.多层继承.多重继承)   by:授客 QQ:1033553122   测试环境: win7 64位 Python版本:Python 3.3.5 代码实践 ...

随机推荐

  1. iOS: lame框架将PCM录音转成MP3格式

    lame框架将PCM录音转成MP3格式 1.lame下载地址:https://github.com/rbrito/lame,它是一个不可执行的文件,需要借助build-lame.sh脚本将其编译成.a ...

  2. JavaScript 空间分析库——JSTS和Turf【转】

    https://blog.csdn.net/neimeng0/article/details/80363468 前言 项目中有管线的空间拓扑关系查询需求,在npm中检索到JSTS和Turf两个Java ...

  3. 如何用 async 控制流程

    来自: http://larry850806.github.io/2016/05/31/async/ [Javascript] 如何用 async 控制流程 (一) 31 May 2016 async ...

  4. vmware vSphere 5.5的14个新功能

    摘录自:http://www.networkworld.com/slideshow/117304/12-terrific-new-updates-in-vmware-vsphere-55.html#s ...

  5. 解决javah生成c头文件时找不到android类库的问题

    问题描述: cmd下面进入工程的bin/classes下面,执行 javah xxx.xxx.A 生成头文件, 一般来说都是可以成功执行的,但是如果xxx.xxx.A类里面引用了android类库里面 ...

  6. Type Call requires API level 11 (current min is 8)解决办法

    解决办法: 1:project-->clean.. 2:右键工程-->Android Tools-->clean lint markers 3:修改AndroidManifest.x ...

  7. intellij IDEA 安装和配置和使用

    下载:https://www.jetbrains.com/idea/download/download-thanks.html?platform=windows 安装教程:https://blog.c ...

  8. 分享:android图片浏览器—类微信朋友圈相片浏览【android代码下载】

    今天给大家分享个android图片/相册浏览器,类似微信朋友圈相片浏览,可以左右滑动,可以双击放大,捏拉放大 效果如下:<ignore_js_op> device-2013-09-04-1 ...

  9. docker的swarm介绍

    转载自:https://blog.csdn.net/karamos/article/details/80132082 另外一篇:https://www.jianshu.com/p/9eb9995884 ...

  10. (1) Mysql高性能优化规范建议

    数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名识意 ...