Selenium2+python Automation 67-use case failure automatic screenshot "reprint"

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:

Class Decoratorwithoutarguments (object):

def __init__ (self, f):
"""
If There is no decorator arguments, the function
To being 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



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:

Class Decoratorwitharguments (object):

def __init__ (self, arg1, arg2, ARG3):
"""
If There is decorator arguments, the function
To being decorated is isn't passed to the constructor!
"""
Print "Inside __init__ ()"
SELF.ARG1 = Arg1
SELF.ARG2 = arg2
SELF.ARG3 = Arg3

def __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__ ()"
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



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
From selenium import Webdriver

Class screen (object):
U ' ' This should function as an adorner '
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


# Here's a case where the adorner is combined with the UnitTest
Import UnitTest
Class Test (UnitTest. TestCase):

Driver = Webdriver. Firefox () # Global parameter driver

def setUp (self):
Self.driver.get ("https://www.baidu.com")

@Screen (Driver)
def test01 (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)
def test_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 ()

def tearDown (self):
Self.driver.quit ()

if __name__ = = "__main__":
Unittest.main ()

Selenium2+python Automation 67-use case failure automatic "reprint"

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.