#82 Playwright with Python | Attach Screenshot in Pytest HTML Report on Failure with Playwright

Опубликовано: 01 Октябрь 2024
на канале: AutomationNeemo
4,638
25

#playwright
#python
#pytest

In this video we learned about
How to attach screenshot in Pytest HTML report on Failure with Playwright
How to generate Pytest HTML report along Screenshots with Playwright

Pytest Documenation:
https://pytest-html.readthedocs.io/en...

Command:
pytest -s --headed .\test_01_sauce_demo.py --html=myreport2.html --self-c
ontained-html --capture=tee-sy

Code snippet attaching Screenshots:
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin("html")
outcome = yield
screen_file = ''
report = outcome.get_result()
extra = getattr(report, "extra", [])
if report.when == "call":
xfail = hasattr(report, "wasxfail")
if report.failed or xfail and "page" in item.funcargs:
page = item.funcargs["page"]
screenshot_dir = Path("screenshots")
screenshot_dir.mkdir(exist_ok=True)
screen_file = str(screenshot_dir / f"{slugify(item.nodeid)}.png")
page.screenshot(path=screen_file)

if (report.skipped and xfail) or (report.failed and not xfail):
add the screenshots to the html report
extra.append(pytest_html.extras.png(screen_file))
report.extra = extra

Github link - https://github.com/automationneemo/Pl...