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

Attaching video URL after every test run in pytest html report

Open rahulbasu86 opened this issue 5 years ago • 6 comments
trafficstars

After every test run i am getting the video URL but when the report gets generated the URL is only shown for the 2nd test case which is of the 1st one and for 1st one it doesn't show anything. Note: the video URLs are generated correctly for each test cases it just that i am not able to properly attach the link to each test case in the report. can someone help me with this please:

Below is my code snippet: @pytest.fixture(scope="function") def test_obj(browser, browser_version, platform, os_version, request): global driver driver_fact = DriverFactory() test_name = os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0] driver = driver_fact.get_web_driver(browser, browser_version, platform, os_version, test_name) print(driver) browserstack_obj = BrowserStack_Library() active_session_id = browserstack_obj.get_active_session_id() driver.maximize_window() request.cls.driver = driver yield driver.quit() global video_url video_url = browserstack_obj.get_session_url(active_session_id)

@pytest.mark.hookwrapper def pytest_runtest_makereport(multicall, item): pytest_html = item.config.pluginmanager.getplugin('html') outcome = yield report = outcome.get_result() extra = getattr(report, 'extra', [])

if report.when == "call":
    extra.append(pytest_html.extras.url(video_url))
    xfail = hasattr(report, 'wasxfail')
    if (report.skipped and xfail) or (report.failed and not xfail):
        file_name = report.nodeid.replace("::", "_") + ".png"
        # _capture_screenshot(file_name)
        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

image

rahulbasu86 avatar Jan 30 '20 19:01 rahulbasu86

Are you using pytest-selenium?

BeyondEvil avatar Jan 31 '20 01:01 BeyondEvil

No, but i am using pytest-html report

rahulbasu86 avatar Jan 31 '20 02:01 rahulbasu86

May I ask why not? Using pytest-selenium would probably solve this for you.

BeyondEvil avatar Jan 31 '20 09:01 BeyondEvil

I dont also understand why the video URL is only getting attached with the last test case. Maybe it has to do how "pytest_runtest_makereport" is implemented internally. The URL gets generated after the driver is closed for every test case and I want the URL to be attached in the report after that.

rahulbasu86 avatar Jan 31 '20 14:01 rahulbasu86

On a sidenote, I think multicall is deprecated. https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_runtest_makereport

BeyondEvil avatar Jan 31 '20 15:01 BeyondEvil

It's kind of hard understanding the code that you've shared, especially with using globals.

Do you think you can create a small reproducible case that I can run to help troubleshoot?

BeyondEvil avatar Jan 31 '20 15:01 BeyondEvil