def create_namespace_loader(cls, config, ns_file_path): from arjuna.tpi.constant import ArjunaOption multi_context_enabled = config.value(ArjunaOption.GUIAUTO_DEF_MULTICONTEXT) context = multi_context_enabled and None or config.guiauto_context _, file_extension = os.path.splitext(ns_file_path) ext = file_extension.upper()[1:] considered_path = ns_file_path try: file_format = FileFormat[ext] except: raise Exception("Unsupported format for namespace: {}".format(file_extension)) else: full_file_path = ns_file_path if os.path.isdir(full_file_path): raise Exception("Namespace file path is a directory and not a file: {}".format(considered_path)) elif not os.path.isfile(full_file_path): from arjuna import log_warning log_warning("Namespace file path does not exist: {}".format(considered_path)) return DummyGnsLoader(considered_path) # raise Exception() # if file_format == FileFormat.GNS: # if multi_context_enabled: # return MGNSFileLoader(full_file_path) # else: # return GNSFileLoader(full_file_path, context) if file_format == FileFormat.YAML: return YamlGnsLoader(full_file_path, context) else: raise Exception("Unsupported format for namespace: {}".format(file_extension))
def enhance_reports(cls, item, result, *, ignore_passed=True, ignore_fixtures=False): ''' Automatically add screenshot to HTML Report File. To be used in **pytest_runtest_makereport** hook in **conftest.py**. Args: item: **pytest**'s Item object result: **pytest**'s TestReport object. Keyword Arguments: ignore_passed: (Optional) If set to True, screenshot is taken when the test function completes. Default is True. ignore_fixtures: (Optional) If set to True, screenshot is not taken for test fixture functions. Default is False. Note: - For taking the screenshot, it does a look up for a **screen_shooter** attribute in the object spaces in following order: - Function Space - Module Space - Session Space - The screen_shooter attribute should contain a **ScreenShooter** i.e. an object of a class that inherits from ScreenShooter class and completes its protocol. - This is a lenient hook. This means that if any exception happens in it, it ignores the exception and logs a warning message. ''' try: html_plugin = cls._get_html_report_plugin(item) pytest_html = html_plugin report = result.get_result() extra = getattr(report, 'extra', []) # if ignore_fixtures: # if report.when == 'call': # return xfail = hasattr(report, 'wasxfail') if ignore_passed and report.passed: pass else: # if (report.skipped and xfail) and (report.failed and not xfail): # extra.append(pytest_html.extras.url(app.url)) try: screen_shooter = cls._get_screen_shooter(item) except AttributeError: pass else: screen_shooter.take_screenshot(prefix=report.nodeid) from arjuna import Arjuna test_container = Arjuna.get_report_metadata() if test_container.has_content(): extra.append( pytest_html.extras.html(test_container.as_report_html())) # For fixtures with errors, failures, clean the resources. if report.when in {"setup", "teardown"}: if not report.passed: Arjuna.get_report_metadata().clear() else: Arjuna.get_report_metadata().clear() report.extra = extra except Exception as e: raise from arjuna import log_warning log_warning("Error in enhance_reports hook: " + str(e))
def enhance_reports(cls, item, result): ''' Automatically add screenshot to HTML Report File. To be used in **pytest_runtest_makereport** hook in **conftest.py**. Args: item: **pytest**'s Item object result: **pytest**'s TestReport object. Note: - For taking the screenshot, it does a look up for a **screen_shooter** attribute in the object spaces in following order: - Function Space - Module Space - Session Space - The screen_shooter attribute should contain a **ScreenShooter** i.e. an object of a class that inherits from ScreenShooter class and completes its protocol. - This is a lenient hook. This means that if any exception happens in it, it ignores the exception and logs a warning message. ''' try: from arjuna import Arjuna, ArjunaOption, log_debug ignore_passed_for_screenshots = not Arjuna.get_config().value( ArjunaOption.REPORT_SCREENSHOTS_ALWAYS) ignore_passed_for_network = not Arjuna.get_config().value( ArjunaOption.REPORT_NETWORK_ALWAYS) include_images = True include_network = True html_plugin = cls._get_html_report_plugin(item) pytest_html = html_plugin report = result.get_result() log_debug("Node ID: {}".format(report.nodeid), contexts="report") log_debug("Stage: {}".format(report.when), contexts="report") extra = getattr(report, 'extra', []) # if ignore_fixtures: # if report.when == 'call': # return xfail = hasattr(report, 'wasxfail') if ignore_passed_for_screenshots and report.passed: include_images = False else: # if (report.skipped and xfail) and (report.failed and not xfail): # extra.append(pytest_html.extras.url(app.url)) try: screen_shooter = cls._get_protocol_object( item, "screen_shooter") except AttributeError: pass else: try: screen_shooter.take_screenshot(prefix=report.nodeid) except Exception: # any error in auto-screen shot is ignored. pass log_debug("Attempting to get network_recorder from request", contexts="report") try: network_recorder = cls._get_protocol_object( item, "network_recorder") except AttributeError as e: log_debug("No network_recorder", contexts="report") else: try: log_debug("Registering traffic", contexts="report") network_recorder.register() log_debug("Traffic registered.", contexts="report") except Exception as e: log_debug("Exception in registering network traffic: " + str(e), contexts="report") if ignore_passed_for_network and report.passed: include_network = False log_debug("Include images {}".format(include_images), contexts="report") log_debug("Include network {}".format(include_network), contexts="report") # When this place is reached by a resource that failed/erred or a test (irrespective of result) if (report.when in {"setup", "teardown"} and not report.passed) or report.when == "call": test_container = Arjuna.get_report_metadata() if test_container.has_content(): log_debug("Extra Content Found for HTML Report", contexts="report") extra_html = test_container.as_report_html( include_images=include_images, include_network=include_network) if extra_html: extra.append(pytest_html.extras.html(extra_html)) report.extra = extra # For fixtures with errors, failures, clean the resources. if report.when in {"setup", "teardown"}: if not report.passed: log_debug("Clearing report extras.", contexts="report") Arjuna.get_report_metadata().clear() else: log_debug("Clearing report extras.", contexts="report") Arjuna.get_report_metadata().clear() except Exception as e: from arjuna import log_warning log_warning("Error in enhance_reports hook: " + str(e), contexts="report") raise
def add_screenshot_for_result(cls, item, result, *, ignore_passed=True, ignore_fixtures=False): ''' Automatically add screenshot to HTML Report File. To be used in **pytest_runtest_makereport** hook in **conftest.py**. Args: item: **pytest**'s Item object result: **pytest**'s TestReport object. Keyword Arguments: ignore_passed: (Optional) If set to True, screenshot is taken when the test function completes. Default is True. ignore_fixtures: (Optional) If set to True, screenshot is not taken for test fixture functions. Default is False. Note: - For taking the screenshot, it does a look up for a **screen_shooter** attribute in the object spaces in following order: - Function Space - Module Space - Session Space - The screen_shooter attribute should contain a **ScreenShooter** i.e. an object of a class that inherits from ScreenShooter class and completes its protocol. - This is a lenient hook. This means that if any exception happens in it, it ignores the exception and logs a warning message. ''' try: try: screen_shooter = cls._get_screen_shooter(item) except AttributeError: return html_plugin = cls._get_html_report_plugin(item) pytest_html = html_plugin report = result.get_result() extra = getattr(report, 'extra', []) if ignore_fixtures: if report.when == 'call': return xfail = hasattr(report, 'wasxfail') if ignore_passed and report.passed: return # if (report.skipped and xfail) and (report.failed and not xfail): # extra.append(pytest_html.extras.url(app.url)) import re rname = re.sub(r"\[.*?\]", "", report.nodeid) image = screen_shooter.take_screenshot(prefix=rname) fpath = "../screenshot/{}".format(image.file_name) img_elem = '''<img src="data:image/png;base64,{}"/>'''.format(image.base64) extra.append( pytest_html.extras.html( '''<div class="image"><a href="{}" target="_blank">{}</a>'''.format(fpath, img_elem) ) ) report.extra = extra except Exception as e: from arjuna import log_warning log_warning("Error in add_screenshot_for_result hook: " + str(e))
def __log(cls, invoker, why, seconds): from arjuna import log_warning log_warning("Hardcoded sleep executed for {} seconds by {}. Reason by author: {}".format(seconds, invoker, why))