Python Forum
[split] Pytest-html add screenshots help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Pytest-html add screenshots help
#1
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 :)
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with screenshots for each 'div class' paunshotts 0 376 Feb-05-2024, 10:19 AM
Last Post: paunshotts
  I am trying to run pytest and getting error that doesn't show up when running pytest. uobouhb 1 2,038 May-20-2022, 02:19 AM
Last Post: uobouhb
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,639 Mar-14-2021, 12:23 PM
Last Post: rfeyer
Question Selenium Screenshots store in Database Nuwan16 3 2,012 Oct-05-2020, 02:55 PM
Last Post: Nuwan16
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,366 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  [split] Using beautiful soup to get html attribute value moski 6 6,304 Jun-03-2019, 04:24 PM
Last Post: moski
  Taking screenshots with http authentication with username and password from website onenessboy 3 4,123 Nov-30-2018, 04:17 AM
Last Post: onenessboy
  Mocks with pytest Vector22 1 2,510 May-21-2018, 10:45 AM
Last Post: Vector22
  Pytest-html hook help flashnet1 5 10,686 Jan-19-2018, 01:52 PM
Last Post: buran
  Visiting websites and taking screenshots implicitly? bigmit37 4 4,464 May-01-2017, 04:26 PM
Last Post: bigmit37

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020