前言:

装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数

上一篇讲到用装饰器解决异常后自动截图,不过并没有与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-用例失败自动截图【转载】的更多相关文章

  1. Selenium2+python自动化61-Chrome浏览器(chromedriver)【转载】

    前言 selenium2启动Chrome浏览器是需要安装驱动包的,但是不同的Chrome浏览器版本号,对应的驱动文件版本号又不一样,如果版本号不匹配,是没法启动起来的. 一.Chrome遇到问题 1. ...

  2. Selenium2+python自动化39-关于面试的题【转载】

    前言 最近看到群里有小伙伴贴出一组面试题,最近又是跳槽黄金季节,小编忍不住抽出一点时间总结了下, 回答不妥的地方欢迎各位高手拍砖指点.   一.selenium中如何判断元素是否存在? 首先selen ...

  3. QTP场景恢复之用例失败自动截图

    以下代码是在QC里运行QTP来执行脚本过程,当执行过程中发现用例失败后就会自动截图,然后把用例返回到最初始的状态,模拟了场景恢复的机制 Class QCImageErrorCapture Dim qt ...

  4. Selenium2+python自动化5-操作浏览器基本方法【转载】

    前言前面已经把环境搭建好了,这从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可视化工具,我们要学的是w ...

  5. Selenium2+python自动化62-jenkins持续集成环境搭建【转载】

    前言 selenium脚本写完之后,一般是集成到jenkins环境了,方便一键执行. 一.环境准备 小编环境: 1.win10 64位 2.JDK 1.8.0_66 3.tomcat 9.0.0.M4 ...

  6. Selenium2+python自动化46-js解决click失效问题【转载】

    前言 有时候元素明明已经找到了,运行也没报错,点击后页面没任何反应.这种问题遇到了,是比较头疼的,因为没任何报错,只是click事件失效了. 本篇用2种方法解决这种诡异的点击事件失效问题 一.遇到的问 ...

  7. Selenium2+python自动化11-定位一组元素find_elements【转载】

    前言 前面的几篇都是讲如何定位一个元素,有时候一个页面上有多个对象需要操作,如果一个个去定位的话,比较繁琐,这时候就可以定位一组对象. webdriver 提供了定位一组元素的方法,跟前面八种定位方式 ...

  8. Selenium2+python自动化-窗口多标签处理方法总结(转载)

    本篇转自博客:上海-小T 原文地址:https://i.cnblogs.com/EditArticles.aspx?opt=1 我们在用Selenium遇到多个浏览器窗口或单个浏览器多个标签(Tab) ...

  9. Selenium2+python自动化3-解决pip使用异常【转载】

    一.pip出现异常 有一小部分童鞋在打开cmd输入pip后出现下面情况:Did not provide a commandDid not provide a command?这是什么鬼?正常情况应该是 ...

随机推荐

  1. lintcode-127-拓扑排序

    127-拓扑排序 给定一个有向图,图节点的拓扑排序被定义为: 对于每条有向边A--> B,则A必须排在B之前 拓扑排序的第一个节点可以是任何在图中没有其他节点指向它的节点 找到给定图的任一拓扑排 ...

  2. 【WebService】——CXF整合Spring

    相关博客: [WebService]--入门实例 [WebService]--SOAP.WSDL和UDDI 前言: 之前的几篇博客基本上都是使用jdk来实现WebService的调用,没有使用任何框架 ...

  3. 当网卡收到的包的目的地址是主机上另一个网卡的地址.arp总结

    2019/01/13 今天测试发现结果不符合预期呀,发现设置了arp_filter之后,仍然是能ping通主机上的另外一张网卡.但是现在的问题是 -------------------- 内核中是如何 ...

  4. 用户代理UA

    简介: 用户代理英文全称为User Agent,简称UA,现在被广泛用来标识浏览器客户端信息. 发展状况: User Agent在互联网早期就已经存在,那时互联网是完全基于文本的,用户直接浏览器互联网 ...

  5. 【python】 json.dumps() json.dump()的区别

    以前写的很简单,只有几句话,最近发现本文是本博客阅读量最大的一篇文章,觉得这样有种把人骗进来的感觉,于是又细化了一些.如果还有不好的地方,欢迎指出. 首先说明基本功能: dumps是将dict转化成s ...

  6. 【bzoj1180】[CROATIAN2009]OTOCI LCT

    题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出“no”.否则输出“yes”,并且在结点 ...

  7. oracleLinux7上安装oracle11g r2(脚本简单配置环境)

    一 环境脚本简单配置 #!/bin/bashmv /etc/yum.repos.d/* /tmpmv iso.repo /etc/yum.repos.d/tar zxvf a.tar.gzmv 7Se ...

  8. (转)HTTP请求中URL地址的编码和解码

    HTTP请求中,类似   http%3A%2F%2Fwww.baidu.com%2Fcache%2Fuser%2Fhtml%2Fv3Jump.html  的地址 如何解码成    http://www ...

  9. Java面向对象编程三大特性 --- 多态

    多态特性: 子类Child继承父类Father,我们可以编写一个指向子类的父类类型引用,该引用既可以处理父类Father对象,也可以处理子类Child对象,当相同的消息发送给子类或者父类对象时,该对象 ...

  10. Python基础(2)_if、for、while流程控制

    一.流程控制 1.条件语句 1.1单分支 1.2多分支 条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. a = 5 if a > 2: print('yes') 根据Py ...