(Python) 将 Playwright Page 对象传递给包装函数的函数装饰器
(Python) Function decorator to pass Playwright Page object to wrapped function
目标:我正在尝试创建一个函数装饰器,它将 Playwright Page
(playwright.sync_api._generated.Page)
对象传递给包装函数。
问题:对于大多数函数调用,我可以return函数调用return的值。但是,由于 Playwright 需要 browser.close()
调用,我不能简单地 return Page
对象。我不确定问题是出在(1)我定义的函数装饰器,还是(2)我对函数装饰器的使用。
我试图在 pytest
固定装置之后为我的装饰器建模。使用 pytest
,我会做这样的事情:
@pytest.fixture(scope="module")
def playwright_page():
with sync_playwright() as play:
browser = play.chromium.launch()
page = browser.new_page()
yield page
browser.close()
然后
def open_google(playwright_page):
playwright_page.goto("https://google.com")
函数装饰器:
>>> from playwright.sync_api import sync_playwright
>>> def playwright_page(func):
def wrapper(*args, **kwargs):
with sync_playwright() as play:
browser = play.chromium.launch()
page = browser.new_page()
yield page
browser.close()
return wrapper
尝试 1:
>>> @playwright_page
def open_google():
page.goto("https://google.com")
>>> open_google()
<generator object playwright_page.<locals>.wrapper at 0x0000015C5F6CBC10>
尝试 2:
>>> @playwright_page
def open_google():
page = next(page)
page.goto("https://google.com")
>>> open_google()
<generator object playwright_page.<locals>.wrapper at 0x0000015C5FDB7F90>
我应该调用 func
并传递 Page
对象,而不是尝试生成 Page
对象,例如 pytest
固定装置。
>>> from playwright.sync_api import sync_playwright
>>> def playwright_page(func):
def wrapper():
with sync_playwright() as play:
browser = play.chromium.launch()
page = browser.new_page()
results = func(page)
browser.close()
return results
return wrapper
>>> @playwright_page
def open_google(page):
page.goto("https://google.com")
目标:我正在尝试创建一个函数装饰器,它将 Playwright Page
(playwright.sync_api._generated.Page)
对象传递给包装函数。
问题:对于大多数函数调用,我可以return函数调用return的值。但是,由于 Playwright 需要 browser.close()
调用,我不能简单地 return Page
对象。我不确定问题是出在(1)我定义的函数装饰器,还是(2)我对函数装饰器的使用。
我试图在 pytest
固定装置之后为我的装饰器建模。使用 pytest
,我会做这样的事情:
@pytest.fixture(scope="module")
def playwright_page():
with sync_playwright() as play:
browser = play.chromium.launch()
page = browser.new_page()
yield page
browser.close()
然后
def open_google(playwright_page):
playwright_page.goto("https://google.com")
函数装饰器:
>>> from playwright.sync_api import sync_playwright
>>> def playwright_page(func):
def wrapper(*args, **kwargs):
with sync_playwright() as play:
browser = play.chromium.launch()
page = browser.new_page()
yield page
browser.close()
return wrapper
尝试 1:
>>> @playwright_page
def open_google():
page.goto("https://google.com")
>>> open_google()
<generator object playwright_page.<locals>.wrapper at 0x0000015C5F6CBC10>
尝试 2:
>>> @playwright_page
def open_google():
page = next(page)
page.goto("https://google.com")
>>> open_google()
<generator object playwright_page.<locals>.wrapper at 0x0000015C5FDB7F90>
我应该调用 func
并传递 Page
对象,而不是尝试生成 Page
对象,例如 pytest
固定装置。
>>> from playwright.sync_api import sync_playwright
>>> def playwright_page(func):
def wrapper():
with sync_playwright() as play:
browser = play.chromium.launch()
page = browser.new_page()
results = func(page)
browser.close()
return results
return wrapper
>>> @playwright_page
def open_google(page):
page.goto("https://google.com")