将装饰器参数传递给内部函数

时间:2020-10-08 07:29:56

标签: python python-decorators

我正在继续学习python。但是我有一些想法,但是面临实现问题。 我正在使用硒,每个元素的起因有多种情况,例如:

submit_btn = (By.XPATH, "//button[@type='submit']")
revert_btn = (By.XPATH, "//button[@class='revert']")

这让我非常烦恼,因为我一直在复制By.CSS_SELECTOR或xpath。 我想制作一个接受路径参数和默认设置为“ css”的by_what参数的装饰器 并用于操纵WebDriver,例如:

@by("[data-qa='selected']")
def save_form(self):
   self.find_element().click()

我的方法find_element看起来像:

 def find_element(self, locator, time=10):
        try:
            el = WebDriverWait(self.browser, time).until(ec.presence_of_element_located(locator),
                                                         message=f"Can't find elements by locator {locator}")
            return el
        except TimeoutException:
            return False
        except StaleElementReferenceException:
            return WebDriverWait(self.browser, time).until(ec.staleness_of(locator),
                                                           message=f"Can't find element by locator {locator}")

或者是另一种方式。 我正在尝试创建装饰器,例如:

def by(selector, what="css", data_qa=True):
    def inner_function(function):
        def wrapper(param):
            if what == "css":
                find_by = By.CSS_SELECTOR
            elif what == "xpath":
                find_by = By.XPATH
            if data_qa:
                path = (find_by, f"[data-qa='{param}']")
            else:
                path = (find_by, selector)
            return function(path)

        return wrapper

    return inner_function

很高兴将其用作

@by('selected')
def get_selected_el(self)
   # set path returned by decorator to find element method
   self.find_element(smth_returned_by_decorator)

所以我想将locator作为装饰器实现。 怎么办?

1 个答案:

答案 0 :(得分:0)

执行类似操作的一种可能方法是在装饰函数中具有一个参数,该参数未在装饰器返回的函数中公开:

# Decorator that automatically forwards an argument
def autoparam(param):
    def decorator(f):
        def wrapper(self):
            f(self, param)
        return wrapper
    return decorator

class MyClass:
    def method1(self, param):
        print(param)
    @autoparam('method2 value')
    def method2(self, param):
        self.method1(param)

# Test
obj = MyClass()
# Calling method2 does not require an argument
obj.method2()
# method2 value