Untitled

 avatar
unknown
plain_text
6 months ago
1.8 kB
2
Indexable
# custom_listener.py
from robot.api import TestData
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_data = TestData(source=self.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):
        for test_case in self.test_data.testcase_table:
            test_name = test_case.name
            self.test_arguments[test_name] = []
            self.test_keywords[test_name] = []

            # Extract arguments from the test case keywords
            for keyword in test_case.keywords:
                if keyword.name == '[Arguments]':
                    self.test_arguments[test_name].extend(keyword.args)
                else:
                    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