python+selenium自动化软件测试:装饰器之用例失败后截图

对于用例失败截图,很多小伙伴都希望用例执行失败的时候能自动截图,想法是很好的,实现起来并不是那么容易,这里小编分享下最近研究装饰器,打算用装饰器来实现自动截图。

一、函数作为形参
1.函数的参数也可以是另外一个函数,也就是说传的参数不仅可以是常见的字符串、数字等,也可以是一个函数。
2.定义aaa为一个加法函数,bbb为减法函数。
3.calculate这个函数传三个参数,第一个参数是一个函数,另外两个参数是函数的两个参数。

二、万能装饰器
1.由于不知道我们被调用的函数到底有几个参数,这时候就可以写一个万能的装饰器,传可变参数。
2.这个装饰器实现一个简单功能:运行一个函数,运行不抛异常,就打印pass;运行函数抛异常就打印fail。

三、实现百度搜索功能。

#实现百度搜索功能
# coding:utf-8
from selenium import webdriver
driver = webdriver.Firefox()
# 截图功能
def get_screen():
    ‘‘‘截图‘‘‘
    import time
    nowTime = time.strftime("%Y_%m_%d_%H_%M_%S")
    driver.get_screenshot_as_file(‘%s.jpg‘ % nowTime)
# 自动截图装饰器
def screen(func):
    ‘‘‘截图装饰器‘‘‘
    def inner(*args, **kwargs):
        try:
            f = func(*args, **kwargs)
            return f
        except:
            get_screen()  # 失败后截图
           raise
    return inner
@screen
def search(driver):
    driver.get("https://www.baidu.com")
    driver.find_element_by_id("kw11").send_keys("python")  # 此行运行失败的
    driver.find_element_by_id("su").click()
search(driver)  # 执行search

这里介绍的是简单的装饰器函数,下面我来介绍复杂一点的带参数的装饰器。

装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数。
上面讲到用装饰器解决异常后自动截图,不过并没有与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

三、截图装饰器
有了上面的参考文档,依着葫芦画瓢就行,最大的麻烦就是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()
时间: 2024-08-07 17:21:25

python+selenium自动化软件测试:装饰器之用例失败后截图的相关文章

python+selenium自动化软件测试(第4章):场景判断与封装

4.1 显示等待WebDriverWait 前言:在脚本中加入太多的sleep后会影响脚本的执行速度,虽然implicitly_wait()这种隐式等待在一定程度上节省了很多时间.但是一旦页面上某些js无法加载出来(其实界面元素已经出来了),左上角那个图标一直转圈,这时候会一直等待的.一.参数解释1.这里主要有三个参数:class WebDriverWait(object):driver, timeout, poll_frequency2.driver:返回浏览器的一个实例,这个不用多说3.ti

python+selenium自动化软件测试(第13章):selenium面试题

前言最近看到群里有小伙伴贴出一组面试题,最近又是跳槽黄金季节,小编忍不住抽出一点时间总结了下 一.selenium中如何判断元素是否存在?expected_conditions模块提供了16种判断方法,以下方法是判断元素存在DOM中:presence_of_element_located    """ An expectation for checking that an element is present on the DOM of a page. This does n

python+selenium自动化软件测试(第10章):测试驱动TDD

测试驱动开发模式,要求开发在写业务代码的时候,先写出测试代码,同时单元测试例子决定了如何来写产品的代码,并且不断的成功的执行编写的所有的单元测试例子,不断的完善单元测试例子进而完善产品代码, 这样随着功能的开发完成,测试代码也会对应的完成, 很显然,这是一个全新的开发模式, 在一定程度上,可以完全的提高软件的质量,以及开发可以对自己写的代码进行一个全面的评估和测试. TDD 模式是一个很大的概念,在这里, 我重点介绍下测试驱动模式与自动化的融合以及精简自动化的测试代码.下面我们来看一个登录的案例

python+selenium自动化软件测试(第9章) :Logging模块

9.1 Logging模块 什么是日志记录?记录是跟踪运行时发生的事件的一种手段.该软件的开发人员将记录调用添加到其代码中,以指示某些事件已发生.事件由描述性消息描述,该消息可以可选地包含可变数据(即,对于事件的每次出现可能不同的数据).事件也是开发人员对事件的重视; 重要性也可以称为级别 或严重性.记录功能logging.debug('此功能提供详细信息')logging.warning('意外发生')logging.error('用于存储异常跟踪')logging.info('确认事情正在按

python+selenium自动化软件测试(第11章):持续集成jenkins和GitHub的使用

11.1 jenkins持续集成环境 相关安装包下载链接:http://pan.baidu.com/s/1qYhmlg4 密码:dcw2赠送jenkins集成selenium环境视频链接http://pan.baidu.com/s/1qXAHwg0 密码:juy7 11.2 tomcat+jenkins *******************************************************************************相关安装包下载链接:http://p

python+selenium自动化软件测试(第14章):基础实战(1)

#coding=utf-8 from selenium import webdriven from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException im

python+selenium自动化软件测试(第8章) 多线程

前戏:线程的基础 运行多个线程同时运行几个不同的程序类似,但具有以下优点:进程内共享多线程与主线程相同的数据空间,如果他们是独立的进程,可以共享信息或互相沟通更容易.线程有时称为轻量级进程,他们并不需要多大的内存开销,他们关心的不是过程便宜.一个线程都有一个开始,执行顺序,并得出结论.它有一个指令指针,保持它的上下文内正在运行的跟踪.(1).它可以是抢占(中断)(2).它可以暂时搁置(又称睡眠),而其他线程正在运行看一下以下的小案例: import thread from time import

python+selenium自动化软件测试(第7章):Page Object模式

什么是Page ObjectModel模式Page Objects是selenium的一种测试设计模式,主要将每个页面看作是一个class.class的内容主要包括属性和方法,属性不难理解,就是这个页面中的元素对象,比如输入用户名的输入框,输入登陆密码的输入框,登陆按钮,这个页面的url等,而方法,主要是指这个页面可以提供的具体功能.为什么选择POM?我们先看一段简单的代码如下: from selenium import webdriver import time driver = webdri

python+selenium自动化软件测试(第12章):Python读写XML文档

XML 即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进 行定义的源语言.xml 有如下特征: 首先,它是有标签对组成:<aa></aa> 标签可以有属性: <aa id=’123’></aa> 标签对可以嵌入数据: <aa>abc</aa>Python对XML文档读写常用有几个模块: (1) xml.etree.ElementTree ElementTree就像一个轻量级的DOM,具有方便友好的A