【问题标题】:Pytest Selenium Web Driver Coming Back as NoneType When using getfixturevaluePytest Selenium Web 驱动程序在使用 getfixturevalue 时返回为 NoneType
【发布时间】:2022-01-14 20:55:27
【问题描述】:

谁能帮我弄清楚为什么驱动程序在我的 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。

我绞尽脑汁试图解决这个问题。我认为这与我的夹具被限定为类有关,或者我在驱动程序夹具中使用 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'

我用“~”擦洗了我的个人电脑路径

【问题讨论】:

  • yield driver 在夹具中。

标签: python selenium selenium-webdriver pytest pytest-html


【解决方案1】:

所有的荣耀都归于 hoefling。

正如他在 cmets 中所说,我忘了让我的实际司机。

@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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-28
    相关资源
    最近更新 更多