playwright-pytest icon indicating copy to clipboard operation
playwright-pytest copied to clipboard

[FEATURE] Add screenshot/video file to pytest-html report

Open mxschmitt opened this issue 3 years ago • 5 comments

See this existing html report where we can add attachments: https://github.com/pytest-dev/pytest-html

Relates https://github.com/microsoft/playwright-pytest/issues/105

mxschmitt avatar Jul 06 '22 08:07 mxschmitt

I think this would be amazing feature tbh. Like, incredibly useful. I was just thinking of adding this myself in our project but it makes much more sense to do this as a part of this repo.

lubosjerabek avatar Mar 03 '23 17:03 lubosjerabek

Is there any update on this feature?

sudip1990pudasaini avatar Jun 01 '23 16:06 sudip1990pudasaini

This is already supported in pytest-html, using the report.extra,

In my conftest.py, I have the following, this is called for every test, so you need to have a global to makes sure you only add uncataloged screenshots, You also need to use unique filenames of screenshots:

screenshots_used = {}
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin('html')
    global extras
    outcome = yield
    #print("extras:{}".format(extras))
    report = outcome.get_result()
    extra = getattr(report, "extra", [])
    report.description = str(item.function.__doc__)
    if args.webhost and os.path.isdir(args.webhost[0]):
        screens = [s for s in os.listdir(args.webhost[0]) if s.endswith('.png')]
        for s in screens:
            if s not in screenshots_used:
                log.info(f"screenshot:{s}")
                extra.append(pytest_html.extras.png(f"{args.webhost[0]}/{s}"))
                screenshots_used[s] = 1
    report.extra = extra

And in your pytest tests you have the following: (for me webhost is our testenvironment fixture like --webhost=test1), but it is the directory you add screenshots to in your test.

page.screenshot(path=f"{webhost}/02contact.png", full_page=True)

screenshot-enclosed

fenchu avatar Aug 15 '23 12:08 fenchu

Hi! Would you add reporter like Node.js to main QA language? https://playwright.dev/python/docs/test-reporters

smolkin895 avatar Dec 22 '23 20:12 smolkin895

pytest-playwright can automatically generate screenshots and video for each test. So it would be great if pytest-html could automatically include those. I tried the approche proposed by @fenchu. But it wasn't successful because the image and videos are generated after the pytest_runtest_makereport hook is called.

smojon avatar Mar 19 '24 21:03 smojon