首页 > 解决方案 > 将屏幕截图添加到 Pytest Html 报告时出错

问题描述

我无法在所需的 html 报告中添加失败的 Scanerios 的屏幕截图。我所做的只是在其他所需文件夹中移动了 html 报告的创建,并且我收到屏幕截图未放置在 HTML 报告中的错误 下面是我在 conftest 中的 ScreenShot 代码

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
    """
        Extends the PyTest Plugin to take and embed screenshot in html report, whenever test fails.
        :param item:
        """
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when == 'call' or report.when == "setup":
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            file_name = report.nodeid.replace("::", "_") + ".png"
            _capture_screenshot(file_name)
            if file_name:
                html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
                       'onclick="window.open(this.src)" align="right"/></div>' % file_name
                extra.append(pytest_html.extras.html(html))
        report.extra = extra


def _capture_screenshot(name):
        driver.get_screenshot_as_file(name) 

标签: pythonselenium-webdriverautomation

解决方案


推荐阅读