Untitled

 avatar
unknown
plain_text
5 months ago
2.1 kB
2
Indexable
# custom_listener.py
from robot.parsing.model import TestCaseFile
from robot.libraries.BuiltIn import BuiltIn

class CustomListener:
    ROBOT_LISTENER_API_VERSION = 2

    def __init__(self, robot_file):
        self.ROBOT_LIBRARY_LISTENER = self
        self.robot_file = robot_file
        self.test_arguments = {}
        self.test_keywords = {}

        # Parse the robot file to extract arguments and keywords
        self._parse_robot_file()

    def _parse_robot_file(self):
        # Load and parse the .robot file
        suite = TestCaseFile(source=self.robot_file).populate()

        for test_case in suite.testcase_table:
            test_name = test_case.name
            self.test_arguments[test_name] = []
            self.test_keywords[test_name] = []

            # Extract arguments and keywords for the test case
            for step in test_case.steps:
                if '[Arguments]' in step.as_list():
                    # Capture arguments
                    args = step.as_list()[1:]  # Arguments follow the keyword [Arguments]
                    self.test_arguments[test_name].extend(args)
                else:
                    # Capture keyword names
                    keyword_name = step.name
                    self.test_keywords[test_name].append(keyword_name)

    def end_test(self, name, attributes):
        if attributes['status'] == 'FAIL':
            # Retrieve arguments and keywords for the failed test
            args = self.test_arguments.get(name, [])
            keywords = self.test_keywords.get(name, [])

            # Build log message
            log_message = "<h2>Test Failed</h2>"
            log_message += "<b>Test Name:</b> {}<br>".format(name)
            log_message += "<b>Arguments:</b> {}<br>".format(args)
            log_message += "<b>Keywords:</b> <ul>"
            for keyword in keywords:
                log_message += "<li>{}</li>".format(keyword)
            log_message += "</ul>"

            # Log the information with HTML formatting
            BuiltIn().log(log_message, html=True)
Editor is loading...
Leave a Comment