Selenium2+python自動化67-用例失敗自動截圖

來源:互聯網
上載者:User

標籤:sid   實現   變數   world   執行   失敗   self   set   file   

前言

裝飾器其實就是一個以函數作為參數並返回一個替換函數的可執行函數

上一篇講到用裝飾器解決異常後自動,不過並沒有與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)"@decoratorWithoutArgumentsdef 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-8from selenium import webdriverclass 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 unittestclass 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-用例失敗自動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.