1

我正在尝试将失败的硒测试的屏幕截图添加到我的 pytest-html 报告中,但它不起作用。屏幕截图仅显示为未找到图像图标。 截图错误

这是我的 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()


@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":
        # always add url to report
        xfail = hasattr(report, "wasxfail")
        if (report.skipped and xfail) or (report.failed and not xfail):
            # only add additional html on failure
            extra.append(pytest_html.extras.image(REPORT_PATH+r"screenshots\image.png"))
            extra.append(pytest_html.extras.html("<div>Additional HTML</div>"))
        report.extra = extra

我正在运行它,pytest --no-header -v ../tests/ --html=./Test_Report.html --self-contained-html 我直接从 pytest-html 文档中获得了钩子。我在这里想念什么?

编辑:发布了错误的代码,现在已修复

4

1 回答 1

0

较新的版本pytest-html改变了屏幕截图的工作方式。这就是为什么我必须将版本固定到2.0.1才能显示屏幕截图。我将它与SeleniumBase一起使用以在顶部添加饼图摘要,详情请点击此处。这是运行 SeleniumBase 测试后我的 html 报告的样子:

SeleniumBase pytest-html 报告

于 2022-01-12T17:52:14.000 回答