Skip to content

Instantly share code, notes, and snippets.

@andgineer
Created March 20, 2019 17:37
Show Gist options
  • Save andgineer/4ec6d58857bb8689907c87f63475525f to your computer and use it in GitHub Desktop.
Save andgineer/4ec6d58857bb8689907c87f63475525f to your computer and use it in GitHub Desktop.
Python allure pytest automatic Selenium webdriver screenshot
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
rep = outcome.get_result()
if rep.when == 'call' and rep.failed:
mode = 'a' if os.path.exists('failures') else 'w'
try:
with open('failures', mode) as f:
if 'browser' in item.fixturenames:
web_driver = item.funcargs['browser']
else:
print('Fail to take screen-shot')
return
allure.attach(
web_driver.get_screenshot_as_png(),
name='screenshot',
attachment_type=allure.attachment_type.PNG
)
except Exception as e:
print('Fail to take screen-shot: {}'.format(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment