Untitled
#!/usr/bin/env python3 import os import re import sys import xml.etree.ElementTree as ET SPECIAL_KEYWORD = "SPECIAL KEYWORD" SPECIAL_MARKER = "--- SPECIAL---" def extract_special_keyword_msgs(log_xml_path): """ Parse the Robot Framework XML (log.xml or output.xml) and find all calls to `SPECIAL_KEYWORD`. For each call, collect any <msg> that contains `--- SPECIAL---`. Returns a list of messages, in the order they appear in the XML. Example return: ["Full message text #1", "Full message text #2", ...] """ tree = ET.parse(log_xml_path) root = tree.getroot() # Robot's top-level element is usually <robot>, containing <suite>, <test>, <kw>, etc. # We'll recursively look for <kw> tags that have name="SPECIAL KEYWORD". special_msgs_in_order = [] def _recursive_search_kw(parent_element): for kw in parent_element.findall('kw'): kw_name = kw.get('name', '').strip() if kw_name == SPECIAL_KEYWORD: # Found a call to SPECIAL KEYWORD. Gather special messages from this kw. for msg in kw.findall('msg'): if msg.text and SPECIAL_MARKER in msg.text: special_msgs_in_order.append(msg.text.strip()) # Recurse into child keywords _recursive_search_kw(kw) # Start recursion from the root. Robot structure typically has <suite> or <kw> at top levels. _recursive_search_kw(root) return special_msgs_in_order def insert_msgs_into_robot(robot_file_path, messages): """ Reads the .robot file line by line. Each time it sees a line calling 'SPECIAL KEYWORD', it inserts the next message in `messages` on its own line right after that call. If there are more calls than messages, the remaining calls won't get any insertion. If there are more messages than calls, extra messages won't be inserted. """ with open(robot_file_path, 'r', encoding='utf-8') as f: lines = f.readlines() new_lines = [] # Regex to detect calls to SPECIAL KEYWORD # Example line: " SPECIAL KEYWORD arg1 arg2" # We'll look for a line that has 'SPECIAL KEYWORD' as the first token after some optional space, # ignoring trailing arguments. call_pattern = re.compile(r'^\s*' + re.escape(SPECIAL_KEYWORD) + r'(\s{2,}|\s+$|$)') # messages is a list; we'll treat it like a queue msg_index = 0 msg_count = len(messages) for line in lines: stripped = line.strip() new_lines.append(line) # Always write the original line first if msg_index < msg_count: # Check if this line calls "SPECIAL KEYWORD" if call_pattern.match(stripped): # Insert the next message right after this line. # We'll insert it as a Robot comment line or some other custom marker. special_message = messages[msg_index] msg_index += 1 # Example insertion as a comment line: insertion_line = f" # Inserted SPECIAL MSG: {special_message}\n" new_lines.append(insertion_line) # Overwrite the .robot file (or write to a new file if you want to preserve original) with open(robot_file_path, 'w', encoding='utf-8') as f: f.writelines(new_lines) def main(): if len(sys.argv) < 3: script_name = os.path.basename(sys.argv[0]) print(f"Usage: {script_name} <path_to_log_xml> <path_to_robot_file>") sys.exit(1) log_xml_path = sys.argv[1] robot_file_path = sys.argv[2] # 1) Extract the special messages from log.xml messages = extract_special_keyword_msgs(log_xml_path) if not messages: print(f"No calls to '{SPECIAL_KEYWORD}' with '{SPECIAL_MARKER}' found in {log_xml_path}.") return print(f"Found {len(messages)} special messages for '{SPECIAL_KEYWORD}'. Inserting now...") # 2) Insert the messages into the .robot file insert_msgs_into_robot(robot_file_path, messages) print("Done! Check your .robot file for inserted lines.") if __name__ == "__main__": main()
Leave a Comment