Pytest Selenium Web 驱动程序在使用 getfixturevalue 时返回为 NoneType
Pytest Selenium Web Driver Coming Back as NoneType When using getfixturevalue
谁能帮我弄清楚为什么驱动程序在我的 pytest.hookimpl 夹具中作为 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
.
我绞尽脑汁想解决这个问题。我认为这与我的固定装置被限定为 class 或我在我的驱动程序固定装置中使用 request
遗漏了什么有关?
如有任何帮助,我们将不胜感激。
这是我的错误日志以增加详细信息
INTERNALERROR> File "~/pytest_project\scr\tests\conftest.py", line 57, in pytest_runtest_makereport
INTERNALERROR> driver.save_screenshot(img_path)
INTERNALERROR> AttributeError: 'NoneType' object has no attribute 'save_screenshot'
我用“~”清理了我的个人计算机路径
所有荣耀归于小蹄子。
正如他在评论中所说,我忘记交出我的实际 driver。
@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()
谁能帮我弄清楚为什么驱动程序在我的 pytest.hookimpl 夹具中作为 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
.
我绞尽脑汁想解决这个问题。我认为这与我的固定装置被限定为 class 或我在我的驱动程序固定装置中使用 request
遗漏了什么有关?
如有任何帮助,我们将不胜感激。
这是我的错误日志以增加详细信息
INTERNALERROR> File "~/pytest_project\scr\tests\conftest.py", line 57, in pytest_runtest_makereport
INTERNALERROR> driver.save_screenshot(img_path)
INTERNALERROR> AttributeError: 'NoneType' object has no attribute 'save_screenshot'
我用“~”清理了我的个人计算机路径
所有荣耀归于小蹄子。
正如他在评论中所说,我忘记交出我的实际 driver。
@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()