-
-
Save prashanth-sams/f0cc2102fc3619b11748e0cbda22598b to your computer and use it in GitHub Desktop.
from selenium import webdriver | |
import unittest | |
from pytest_html_reporter import attach | |
class TestClass(unittest.TestCase): | |
def __init__(self, driver): | |
super().__init__(driver) | |
def setUp(self): | |
global driver | |
self.driver = webdriver.Chrome() | |
def test_demo(self): | |
self.driver.get("http://devopsqa.wordpress.com/") | |
assert 5 == 4 | |
def tearDown(self): | |
self.screenshot_on_failure() | |
self.driver.close() | |
self.driver.quit() | |
def screenshot_on_failure(self): | |
for self._testMethodName, error in self._outcome.errors: | |
if error: | |
attach(data=self.driver.get_screenshot_as_png()) | |
if __name__ == '__main__': | |
unittest.main() |
I am unable to see the screenshots when running the attach(data=self.driver.get_screenshot_as_png())
Unable to see a screenshot
you can use this script
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
setattr(item, f"rep_{report.when}", report)
if report.when == "call" and report.failed:
try:
driver = item.funcargs["driver"]
take_screenshot(driver, item.name)
except Exception as e:
print(f"Failed to take screenshot: {str(e)}")
return report
def take_screenshot(driver, name):
screenshot_name = f"{name}{datetime.now().strftime('%Y-%m-%d%H-%M-%S')}.png"
os.makedirs(SCREENSHOT_PATH, exist_ok=True)
screenshot_path = os.path.join(SCREENSHOT_PATH, screenshot_name)
driver.save_screenshot(screenshot_path)
print(f"Screenshot saved at {screenshot_path}")
with open(screenshot_path, "rb") as file:
file_content = file.read()
attach(file_content)
you can use this script @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): outcome = yield report = outcome.get_result() setattr(item, f"rep_{report.when}", report)
if report.when == "call" and report.failed: try: driver = item.funcargs["driver"] take_screenshot(driver, item.name) except Exception as e: print(f"Failed to take screenshot: {str(e)}") return report
def take_screenshot(driver, name): screenshot_name = f"{name}{datetime.now().strftime('%Y-%m-%d%H-%M-%S')}.png" os.makedirs(SCREENSHOT_PATH, exist_ok=True) screenshot_path = os.path.join(SCREENSHOT_PATH, screenshot_name) driver.save_screenshot(screenshot_path) print(f"Screenshot saved at {screenshot_path}")
with open(screenshot_path, "rb") as file: file_content = file.read() attach(file_content)
this code print
"Failed to take screenshot: type object 'HTMLReporter' has no attribute 'base_path'"
version is :
pytest-html-reporter 0.2.9
we need pytest version downgrade (test 7.1.3 ok)
pytest is continuously updated.
It appears that newer versions of pytest are not properly compatible with pytest-html-reporter.
I am also facing same issue