pytest-html
pytest-html copied to clipboard
Attaching video URL after every test run in pytest html report
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

Are you using pytest-selenium?
No, but i am using pytest-html report
May I ask why not? Using pytest-selenium would probably solve this for you.
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.
On a sidenote, I think multicall is deprecated. https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_runtest_makereport
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?