标签:nts and turn print creat 截图 aaa 软件测试 百度
对于用例失败截图,很多小伙伴都希望用例执行失败的时候能自动截图,想法是很好的,实现起来并不是那么容易,这里小编分享下最近研究装饰器,打算用装饰器来实现自动截图。
一、函数作为形参
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()
python+selenium自动化软件测试:装饰器之用例失败后截图
标签:nts and turn print creat 截图 aaa 软件测试 百度
原文地址:http://www.cnblogs.com/zidonghua/p/7428922.html