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()
Selenium2+python自动化67-用例失败自动截图【转载】的更多相关文章
- Selenium2+python自动化61-Chrome浏览器(chromedriver)【转载】
前言 selenium2启动Chrome浏览器是需要安装驱动包的,但是不同的Chrome浏览器版本号,对应的驱动文件版本号又不一样,如果版本号不匹配,是没法启动起来的. 一.Chrome遇到问题 1. ...
- Selenium2+python自动化39-关于面试的题【转载】
前言 最近看到群里有小伙伴贴出一组面试题,最近又是跳槽黄金季节,小编忍不住抽出一点时间总结了下, 回答不妥的地方欢迎各位高手拍砖指点. 一.selenium中如何判断元素是否存在? 首先selen ...
- QTP场景恢复之用例失败自动截图
以下代码是在QC里运行QTP来执行脚本过程,当执行过程中发现用例失败后就会自动截图,然后把用例返回到最初始的状态,模拟了场景恢复的机制 Class QCImageErrorCapture Dim qt ...
- Selenium2+python自动化5-操作浏览器基本方法【转载】
前言前面已经把环境搭建好了,这从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可视化工具,我们要学的是w ...
- Selenium2+python自动化62-jenkins持续集成环境搭建【转载】
前言 selenium脚本写完之后,一般是集成到jenkins环境了,方便一键执行. 一.环境准备 小编环境: 1.win10 64位 2.JDK 1.8.0_66 3.tomcat 9.0.0.M4 ...
- Selenium2+python自动化46-js解决click失效问题【转载】
前言 有时候元素明明已经找到了,运行也没报错,点击后页面没任何反应.这种问题遇到了,是比较头疼的,因为没任何报错,只是click事件失效了. 本篇用2种方法解决这种诡异的点击事件失效问题 一.遇到的问 ...
- Selenium2+python自动化11-定位一组元素find_elements【转载】
前言 前面的几篇都是讲如何定位一个元素,有时候一个页面上有多个对象需要操作,如果一个个去定位的话,比较繁琐,这时候就可以定位一组对象. webdriver 提供了定位一组元素的方法,跟前面八种定位方式 ...
- Selenium2+python自动化-窗口多标签处理方法总结(转载)
本篇转自博客:上海-小T 原文地址:https://i.cnblogs.com/EditArticles.aspx?opt=1 我们在用Selenium遇到多个浏览器窗口或单个浏览器多个标签(Tab) ...
- Selenium2+python自动化3-解决pip使用异常【转载】
一.pip出现异常 有一小部分童鞋在打开cmd输入pip后出现下面情况:Did not provide a commandDid not provide a command?这是什么鬼?正常情况应该是 ...
随机推荐
- c++单例模式代码分析
单例模式就是一个C++语法精华浓缩的一个体现,有句老话:麻雀虽小五脏俱全!来形容单例非常贴切! 下面的代码分析了如果自己malloc并且memcpy一个单例指针会带来很大危害并如何防止这种情况发生. ...
- 微信小程序—setTimeOut定时器的坑
原文地址: http://fanjiajia.cn/2018/06/27/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E2%80%94setTimeOu ...
- [剑指Offer] 14.链表中倒数第k个结点
[思路]利用两个相隔为k-1个结点的指针进行遍历,当后一个指针移到末尾时,前一个指针就是要求的结点. /* struct ListNode { int val; struct ListNode *ne ...
- 多线程 定时器 Timer TimerTask
定时器是一种特殊的多线程,使用Timer来安排一次或者重复执行某个任务 package org.zln.thread; import java.util.Date; import java.util. ...
- 服务器下面的WEB-INF 不能直接访问,可以通过servlet进行访问
服务器下面的WEB-INF 不能直接访问,可以通过servlet进行访问
- [bzoj1062] [NOI2008]糖果雨
Description 有一个美丽的童话:在天空的尽头有一个" 糖果国" ,这里大到摩天大厦,小到小花小草都是用糖果建造而成的.更加神奇的是,天空中飘满了五颜六色的糖果云,很快糖果 ...
- [洛谷P3937]Changing
题目大意:有 $n$ 盏灯环形排列,顺时针依次标号为 $1\cdots n$.初始时刻为 $0$ ,初始时刻第 $i$ 盏灯的亮灭 $a_i$, $0$ 表示灭, $1$ 表示亮.下一时刻每盏灯的亮灭 ...
- 如何在数据访问层上提高js的执行效率
本文讲到的是如何从数据访问层面上提高JS 代码的执行效率.总的来讲有以下几条原则: 函数中读写局部变量总是最快的,而全局变量的读取则是最慢的: 尽可能地少用with 语句,因为它会增加with 语句以 ...
- POJ2253:Frogger(改造Dijkstra)
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 64864 Accepted: 20127 题目链接:ht ...
- AngularJS+BootStrap的一些插件
插件网址:http://jquerypluginplus.com/ 树 1.angular-bootstrap-nav-tree http://jquerypluginplus.com/angula ...