The images inside the Jenkins report are broken but the same images are not inside the local report.
I am using the Jenkins Free model to run test cases but in the Jenkins report the images appear broken even though they appear correctly in local report.
This is the result of Jenkins report "Broken image on Jenkins report"
and this the same result on the local report "visible images on local report":
my screenshots are in MainFolder>Reports>screenshots
and the code that handles the screenshot is in MainFolder>Main>conftest
and this is my code
@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.")
My question is how can I make the images visible? Note that I used post-action as Build Artifacts and it correctly works.
When Jenkins runs the tests, the paths in the generated report might be relative or incorrect, causing the browser to fail to load the images. Jenkins reports likely are done from differences in how file paths are resolved or accessed between your local environment and the Jenkins.
make sure the paths in the Jenkins report are absolute paths or URLs accessible from the Jenkins server itself. otherwise, you need to change your code to generate absolute paths. Also, ensure the Reports/screenshots
folder is included in the artifacts.
update your ensure_screenshot_folder
method to print the folder path for debugging, i.e:
print(f"Screenshots folder: {screenshots_folder}")
Run the Jenkins job and check the logs, is it created as expected?