import os import time import allure import pytest from ui_automation_framework.core import DriverFactory @pytest.fixture() def set_up(): print("Running method level setUp") yield print("Running method level tearDown") @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): outcome = yield rep = outcome.get_result() setattr(item, "rep_" + rep.when, rep) return rep @pytest.fixture(scope="function", autouse=True) def one_time_set_up(request): print("Running class setUp") wdf = DriverFactory() driver = wdf.get_driver() if request.cls is not None: request.cls.driver = driver yield driver if request.node.rep_call.failed and driver is not None: screenshot_on_failure(driver) if driver is not None: driver.quit() print("Running class tearDown") def screenshot_on_failure(driver): file_name = "screenshot" + str(round(time.time() * 1000)) + ".png" if len(file_name) >= 200: file_name = str(round(time.time() * 1000)) + ".png" screenshot_dir = "../../../screenshots/" relative_name = screenshot_dir + file_name current_dir = os.path.dirname(__file__) destination_file = os.path.join(current_dir, relative_name) destination_dir = os.path.join(current_dir, screenshot_dir) if not os.path.exists(destination_dir): os.makedirs(destination_dir) driver.save_screenshot(destination_file) allure.attach(driver.get_screenshot_as_png(), name=file_name, attachment_type=allure.attachment_type.PNG)