有人能帮我弄清楚为什么驱动程序在我的pytest.hookimpl fixture中以NoneType的形式返回吗?
这是我的conftest.py
@pytest.fixture()
def chrome_driver_init(request, path_to_chrome):
driver = webdriver.Chrome(options=opts, executable_path=path_to_chrome)
request.cls.driver = driver
page_object_init(request, driver)
driver.get(URL)
driver.maximize_window()
yield
driver.quit()
# Adds a screenshot to the pytest-html report for a failed test
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin("html")
outcome = yield
report = outcome.get_result()
extra = getattr(report, "extra", [])
if report.when == "call":
feature_request = item.funcargs['request']
driver = feature_request.getfixturevalue('chrome_driver_init')
nodeid = item.nodeid
xfail = hasattr(report, "wasxfail")
if (report.skipped and xfail) or (report.failed and not xfail):
file_name = f'{nodeid}_{datetime.today().strftime("%Y-%m-%d_%H:%M")}.png'.replace("/", "_").replace("::", "_")
img_path = os.path.join(REPORT_PATH, "screenshots", file_name)
driver.save_screenshot(img_path)
extra.append(pytest_html.extras.image(img_path))
report.extra = extra
错误发生在driver.save_screenshot()
,但我认为真正的问题是
driver = feature_request.getfixturevalue('chrome_driver_init')
实际上并没有返回我的网络驱动程序,而是返回None
。
为了解决这个问题,我绞尽脑汁。我认为这与我的fixture被定为类有关,或者我在驱动程序fixture中使用request
遗漏了什么?
如有任何帮助,我们将不胜感激。
这是我添加的详细信息的错误日志
INTERNALERROR> File "~/pytest_projectscrtestsconftest.py", line 57, in pytest_runtest_makereport
INTERNALERROR> driver.save_screenshot(img_path)
INTERNALERROR> AttributeError: 'NoneType' object has no attribute 'save_screenshot'
我用"~">
一切荣耀归于霍夫林。
正如他在评论中所说,我忘了让我的实际司机下车。
@pytest.fixture()
def chrome_driver_init(request, path_to_chrome):
driver = webdriver.Chrome(options=opts, executable_path=path_to_chrome)
request.cls.driver = driver
page_object_init(request, driver)
driver.get(URL)
driver.maximize_window()
yield driver
driver.quit()