Python Forum
[split] Pytest-html add screenshots help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: [split] Pytest-html add screenshots help (/thread-10939.html)



[split] Pytest-html add screenshots help - rafiPython1 - Jun-13-2018

Hi,

I can't figure how to add screen shots for pytest-html or even to do a mall change like modifying the Environment details, I'm able to see the basic html report when removing the conftest file but when adding It getting the error:
Error:
INTERNALERROR> driver.get_screenshot_as_file(name) INTERNALERROR> AttributeError: 'NoneType' object has no attribute 'get_screenshot_as_file'
I'm using below command to trigger the test:
py.test -s -v TestClass.py --html=Htmlreport.html
I tried to follow below recommandation with no luck:

pytest-html reports


TestClass.py
    def test_Login_with_valid_credentials(self):
        self.driver.get(self.baseURL)
        self.lp.UserLogin(parameter1, parameter2)
        # time.sleep(3)
        result=self.lp.VerifyLogin()
        self.driver.quit()
        assert result==True
conftest.py
from selenium import webdriver
import pytest
driver = None


@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
    """
    Extends the PyTest Plugin to take and embed screenshot in html report, whenever test fails.
    :param item:
    """
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when == 'call' or report.when == "setup":
        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


def _capture_screenshot(name):
    driver.get_screenshot_as_file(name)


@pytest.fixture(scope='session', autouse=True)
def browser():
    global driver
    if driver is None:
        driver = webdriver.Firefox()
    return driver
Your help will be appreciated :)


RE: [split] Pytest-html add screenshots help - Gourav - Apr-30-2020

I have added the above code and this code is working fine for me in python.

This code can show the failed screenshot just below the failed Script/Test Cases in the same Test Cases/Scripts folder.

Please any one tell me i want to save the same screenshot in other directory/folder in my framework how should i do this. The structure naming and formatting of screenshot would be same but i want to change the location of saving the screenshot.

please help me to resolve this issue what and where should i change in the code to change the location of failure screenshot.