pytest-dev / pytest-selenium

Plugin for running Selenium with pytest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

reports missing <img> tags for screenshots

anothermattbrown opened this issue · comments

The generated report html is missing the <img> tag for screenshots.

Example test:

def test_foo(selenium):
    selenium.get('http://google.com')
    assert False

command:

$ pytest --driver chrome --driver-path path/to/chromedriver test.py --html report.html

The report shows a blank square instead of an image:
Screen Shot 2020-05-12 at 12 46 50 PM

In the inspector, there's an empty <a> tag:
Screen Shot 2020-05-12 at 12 47 18 PM

The link in the <a> tag goes to the (expected) screenshot on disk.

I can't tell if this is a problem with pytest-selenium, pytest-html, or something else. Any help would be appreciated.

I can shed some light.

  1. Here's the code in pytest-selenium that gathers the screenshot and passes it to pytest-html:

    def _gather_screenshot(item, report, driver, summary, extra):
    try:
    screenshot = driver.get_screenshot_as_base64()
    except Exception as e:
    summary.append("WARNING: Failed to gather screenshot: {0}".format(e))
    return
    pytest_html = item.config.pluginmanager.getplugin("html")
    if pytest_html is not None:
    # add screenshot to the html report
    extra.append(pytest_html.extras.image(screenshot, "Screenshot"))

  2. Here's the code in pytest-html that handles that call and generates the report: https://github.com/pytest-dev/pytest-html/blob/103c849cac7b8e0dbaf0a151a541a8c085fb008e/pytest_html/plugin.py#L293-L323

The flow is as follows:

The fix could be in either place: pytest-selenium could create the asset before passing it in, or pytest-html could use the img tag even when it creates the asset. I haven't yet been able to determine which fix is the right one.

Okay, digging, it looks like this was an unintentional result of refactoring in pytest-html: pytest-dev/pytest-html@588c41b

Before that commit, the img tag was applied for created assets. That no longer happens in the refactored code.

Cross-linking to existing pytest-html issue: pytest-dev/pytest-html#265

Thanks @dhalperi. For now, I confirmed that --self-contained-html is a work-around.

Hopefully in not a too distant future we'll release a new version of pytest-html, which will solve this issue.

Please fix it. Thanks