Selenium2+python自动化67-用例失败自动截图
时间:2022-05-07
本文章向大家介绍Selenium2+python自动化67-用例失败自动截图,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
前言:
装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数
上一篇讲到用装饰器解决异常后自动截图,不过并没有与unittest结合,这篇把截图的装饰器改良了下,可以实现用例执行失败自动截图。 一、不带变量的装饰器 1.参考资料:http://www.artima.com/weblogs/viewpost.jsp?thread=240845,这里这篇讲的很好,可以看下原文 2.这个是不带变量的装饰器__init__里是初始化参数,__call__里面是原函数参数
Decorators without Arguments
If we create a decorator without arguments, the function to be decorated is passed to the constructor, and the __call__() method is called whenever the decorated function is invoked:
class decoratorWithoutArguments(object):
def __init__(self, f):
"""
If there are no decorator arguments, the function
to be decorated is passed to the constructor.
"""
print "Inside __init__()"
self.f = f
def __call__(self, *args):
"""
The __call__ method is not called until the
decorated function is called.
"""
print "Inside __call__()"
self.f(*args)
print "After self.f(*args)"
@decoratorWithoutArguments
def sayHello(a1, a2, a3, a4):
print 'sayHello arguments:', a1, a2, a3, a4
二、带变量的装饰器
1.这个是带变量的参数,参数写到__init__里
Decorators with Arguments
Now let's modify the above example to see what happens when we add arguments to the decorator:
class decoratorWithArguments(object):
def __init__(self, arg1, arg2, arg3):
"""
If there are decorator arguments, the function
to be decorated is not passed to the constructor!
"""
print "Inside __init__()"
self.arg1 = arg1
self.arg2 = arg2
self.arg3 = arg3
def __call__(self, f):
"""
If there are decorator arguments, __call__() is only called
once, as part of the decoration process! You can only give
it a single argument, which is the function object.
"""
print "Inside __call__()"
def wrapped_f(*args):
print "Inside wrapped_f()"
print "Decorator arguments:", self.arg1, self.arg2, self.arg3
f(*args)
print "After f(*args)"
return wrapped_f
@decoratorWithArguments("hello", "world", 42)
def sayHello(a1, a2, a3, a4):
print 'sayHello arguments:', a1, a2, a3, a4
三、截图装饰器 1.有了上面的参考文档,依着葫芦画瓢就行,最大的麻烦就是driver参数处理,这里放到__init__里就可以了
四、参考案例
# coding:utf-8
from selenium import webdriver
class Screen(object):
u'''这个应该截图功能的装饰器'''
def __init__(self, driver):
self.driver = driver
def __call__(self, f):
def inner(*args):
try:
return f(*args)
except:
import time
nowTime = time.strftime("%Y_%m_%d_%H_%M_%S")
self.driver.get_screenshot_as_file('%s.jpg' % nowTime)
raise
return inner
# 以下是装饰器与unittest结合的案例
import unittest
class Test(unittest.TestCase):
driver = webdriver.Firefox() # 全局参数driver
def setUp(self):
self.driver.get("https://www.baidu.com")
@Screen(driver)
def test01(self):
u'''这个是失败的案例'''
self.driver.find_element_by_id("11kw").send_keys("python")
self.driver.find_element_by_id("su").click()
@Screen(driver)
def test_02(self):
u'''这个是通过的案例'''
self.driver.find_element_by_id("kw").send_keys("yoyo")
self.driver.find_element_by_id("su").click()
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
- 初试git+github(linux环境)
- Raspberry PI Nginx 安装
- 微信支付-Java-SpringMVC基础一些代码支持
- 使用Kafka的High Level Consumer
- 微信公众号发送模板消息 Java实现。
- [Intellij IDEA]File size exceeds configured limit
- 那些年Struts 1.X 而今我还是不会
- 关于MySQL DNS解析探究之一:DNS解析确实在起作用
- 每一个程序员需要了解的10个Linux命令
- 实战案例 | 使用机器学习和大数据预测心脏病
- 关于JVM直接内存触发Full GC
- 极客DIY:通过树莓派发送摩斯码
- Hadoop SequnceFile.Writer 压缩模式及压缩库浅析
- Thrift抛直接内存OOM一点解决思路
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 怎么理解int main(int argc, const char *argv[])
- Julia简易教程——2_julia数学运算及其基本功能
- Elasticsearch: 运用 Field collapsing 来减少基于单个字段的搜索结果
- Julia简易教程——1_julia中的整数和浮点数
- Linux 工作常用命令笔记(持续更新)
- Vim实用技巧——Vim分屏技巧总结
- Activity onStop,onDestroy延迟10s执行
- 内存优化实战
- Nali:一个离线查询 IP 地理信息和 CDN 提供商的终端利器
- MySQL8.0的几个新特性
- read_only和super_read_only参数的区别
- AWS 命名提示需要指定 region
- AWS CodeArtifact 如何设置用户的 TOKEN
- GORM V2 自动迁移和迁移接口的方法
- Vue Nginx反向代理配置 解决生产环境跨域