失败的测试用例图像在我的 Jenkins 报告中已损坏

问题描述 投票:0回答:1

Jenkins 报告中的图像已损坏,但相同的图像不在本地报告中。

我正在使用 Jenkins 免费模型来运行测试用例,但在 Jenkins 报告中,图像显示为损坏,即使它们在本地报告中正确显示。

这是 Jenkins 报告“Jenkins 报告上的破碎图像”的结果

这与本地报告“本地报告上的可见图像”的结果相同:

我的截图在

MainFolder>Reports>screenshots
处理屏幕截图的代码位于
MainFolder>Main>conftest
这是我的代码

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item):
    """
    Extends the PyTest Plugin to take and embed screenshots in the HTML report and JUnit XML,
    whenever a test fails.
    """
    pytest_html = item.config.pluginmanager.getplugin('html')
    junit_xml = item.config.pluginmanager.getplugin('junitxml')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when in ['call', 'setup']:
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            screenshots_folder = ensure_screenshot_folder()
            screenshot_path = screenshots_folder / f"{report.nodeid.replace('::', '_')}.png"

            try:
                # Capture and save the screenshot
                _capture_screenshot(screenshot_path)

                if screenshot_path.exists():
                    # Embed screenshot in HTML report
                    if pytest_html:
                        html = (
                            f'<div><img src="{screenshot_path}" alt="screenshot" '
                            f'style="width:304px;height:228px;" '
                            f'onclick="window.open(this.src)" align="right"/></div>'
                        )
                        extra.append(pytest_html.extras.html(html))

                    # Embed screenshot in JUnit XML report
                    if junit_xml:
                        report.longrepr = str(report.longrepr) + (
                            f'\n<![CDATA[Screenshot: {screenshot_path}]]>'
                        )
            except Exception as e:
                # Log or handle screenshot failure
                report.longrepr += f"\n[Warning] Failed to capture screenshot: {e}"

        report.extras = extra


def ensure_screenshot_folder():
    """
    Ensure the screenshots folder exists.
    """
    project_root = Path(__file__).resolve().parent.parent  # Adjust to your project structure
    screenshots_folder = project_root / "Reports" / "screenshots"
    screenshots_folder.mkdir(parents=True, exist_ok=True)
    return screenshots_folder


def _capture_screenshot(screenshot_path):
    """
    Capture a screenshot and save it to the specified path.
    """

    if isinstance(driver, WebDriver):
        driver.get_screenshot_as_file(str(screenshot_path))
    else:
        raise ValueError("WebDriver instance not available for capturing screenshots.")

我的问题是如何使图像可见? 请注意,我使用后期操作作为构建工件并且它可以正常工作。

python jenkins pytest report testcase
1个回答
0
投票

Jenkins运行测试时,生成的报告中的路径可能是相对的或不正确的,导致浏览器无法加载图像。 Jenkins 报告可能是由于本地环境和 Jenkins 之间解析或访问文件路径的方式不同而完成的。

确保 Jenkins 报告中的路径是绝对路径或可从 Jenkins 服务器本身访问的 URL。否则,您需要更改代码以生成绝对路径。另外,请确保

Reports/screenshots
文件夹包含在工件中。 更新您的
ensure_screenshot_folder
方法以打印文件夹路径以进行调试,即:
print(f"Screenshots folder: {screenshots_folder}")
运行 Jenkins 作业并检查日志,是否按预期创建?

© www.soinside.com 2019 - 2024. All rights reserved.