Selenium2+python Automation 67-automatic screenshot of use case failure

Source: Internet
Author: User

Objective

An adorner is actually an executable function that takes a function as a parameter and returns a replacement function

The previous article about using the adorner to solve the exception automatically, but did not combine with the unittest, this piece of the decorator improved under, you can achieve the use case execution failure automatically.

An adorner with no variables

1. Reference: http://www.artima.com/weblogs/viewpost.jsp?thread=240845, this article is very good here, you can read the original

2. This is an adorner without a variable __init__ is the initialization parameter, __call__ inside is the original function parameter

Decorators without Arguments

If we create a decorator without arguments, the function to be decorated are passed to the constructor, and the __call__ () method is called whenever the decorated function is invoked:

classdecoratorwithoutarguments (object):def __init__(self, f):"""If There is no decorator arguments, the function to be decorated is passed to the constructor. """        Print "Inside __init__ ()"SELF.F=Fdef __call__(Self, *args):"""The __call__ method is no called until the decorated function is called. """        Print "Inside __call__ ()"Self.f (*args)Print "After self.f (*args)"@decoratorWithoutArgumentsdefSayHello (A1, A2, A3, A4):Print 'SayHello arguments:', A1, A2, A3, A4


Second, the adorner with the variable

1. This is a parameter with variables, and the parameters are written in __init__.

Decorators with Arguments

Now let's modify the above example to see what's happens when we add arguments to the decorator:

classdecoratorwitharguments (object):def __init__(self, arg1, arg2, ARG3):"""If There is decorator arguments, the function to be decorated is not passed to the constructor! """        Print "Inside __init__ ()"Self.arg1=arg1 self.arg2=arg2 Self.arg3=Arg3def __call__(self, f):"""If There is decorator arguments, __call__ () was 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__ ()"        defWrapped_f (*args):Print "Inside wrapped_f ()"            Print "Decorator arguments:", Self.arg1, Self.arg2, Self.arg3 F (*args)Print "After F (*args)"        returnwrapped_f@decoratorwitharguments ("Hello"," World", 42)defSayHello (A1, A2, A3, A4):Print 'SayHello arguments:', A1, A2, A3, A4

Three, the decoration device

1. With the reference document above, according to the gourd painting scoop, the biggest trouble is driver parameter processing, here put in the __init__ can be

Iv. Reference Cases

#Coding:utf-8 fromSeleniumImportWebdriverclassScreen (object): U" "This should be the function of the adorner" "    def __init__(self, driver): Self.driver=Driverdef __call__(self, f):defInner (*args):Try:                returnF (*args)except:                ImportTime Nowtime= Time.strftime ("%y_%m_%d_%h_%m_%s") Self.driver.get_screenshot_as_file ('%s.jpg'%nowtime)Raise        returnInner#The following are examples of the combination of adorners and unittestImportUnitTestclassTest (unittest. TestCase): Driver= Webdriver. Firefox ()#Global Parameter Driver    defsetUp (self): Self.driver.get ("https://www.baidu.com") @Screen (driver)deftest01 (self): U" "This is a case of failure." "self.driver.find_element_by_id ("11kw"). Send_keys ("python") self.driver.find_element_by_id ("su"). Click () @Screen (driver)deftest_02 (self): U" "This is a case of passage." "self.driver.find_element_by_id ("kw"). Send_keys ("Yoyo") self.driver.find_element_by_id ("su"). Click ()defTearDown (self): Self.driver.quit ()if __name__=="__main__": Unittest.main ()

Selenium2+python Automation 67-use case failure automatic

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.