Skip to content

Instantly share code, notes, and snippets.

@rafalf
Created November 25, 2018 12:07
Show Gist options
  • Save rafalf/21a6eedb45968fb83dacbeee1838d129 to your computer and use it in GitHub Desktop.
Save rafalf/21a6eedb45968fb83dacbeee1838d129 to your computer and use it in GitHub Desktop.
html webdriver python report screenshots
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
"""
Extends the PyTest Plugin to take and embed screenshots 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':
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
report_directory = os.path.dirname(item.config.option.htmlpath)
file_name = str(int(round(time.time() * 1000))) + ".png"
# full_path = os.path.join("C:\Screenshots", file_name)
full_path = os.path.join(report_directory, file_name)
if item.funcargs.get('driver'):
print(f"[INFO] screenshot: {full_path}")
item.funcargs['driver'].get_screenshot_as_file(full_path)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment