【问题标题】:Pytest HTML Report Screenshot Not DisplayingPytest HTML 报告屏幕截图不显示
【发布时间】:2022-01-12 17:36:49
【问题描述】:

我正在尝试将失败的硒测试的屏幕截图添加到我的 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 文档中得到了钩子。我在这里错过了什么?

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

【问题讨论】:

    标签: python selenium pytest webpage-screenshot pytest-html


    【解决方案1】:

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

    【讨论】:

    • 我的代码正确吗?我只需要使用 pytest-html v2.0.1?
    • 我无法直接通过查看您的代码来判断,但首先尝试使用 pytest-html v2.0.1 看看它是否有效。
    • 我看到REPORT_PATH,但我不知道它是否指向有效路径等。请先验证您的路径。
    • 是的,降级不起作用。它的有效路径。我在其他地方使用它没有问题
    • 您可以从SeleniumBase 复制/粘贴您需要的直接代码。从中生成的 pytest-html 报告包含屏幕截图。
    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2015-07-19
    • 2020-12-18
    • 2013-01-20
    • 1970-01-01
    相关资源
    最近更新 更多