|
1 | | -import os |
| 1 | +import os, requests |
2 | 2 | import re |
3 | 3 | import json |
4 | 4 | import subprocess |
@@ -110,7 +110,6 @@ def extract_elements(node): |
110 | 110 | return [extract_elements(root)] |
111 | 111 |
|
112 | 112 |
|
113 | | - |
114 | 113 | def generate_zeuz_action(element_xml, action_type, action_value="Sample Text"): |
115 | 114 | """ |
116 | 115 | Generate a Zeuz action in the required format. |
@@ -208,6 +207,95 @@ def toggle_view(): |
208 | 207 | view_mode.set("tree") |
209 | 208 | update_screenshot(None) |
210 | 209 |
|
| 210 | +def find_matching_element(root, selected_element) -> bool: |
| 211 | + """Find an element in `root` that matches `selected_element` by tag, attributes, and text.""" |
| 212 | + tag = selected_element.tag |
| 213 | + attribs = selected_element.attrib |
| 214 | + text = (selected_element.text or "").strip() |
| 215 | + |
| 216 | + # Iterate through all elements with the same tag in root |
| 217 | + for elem in root.iter(tag): |
| 218 | + if elem.attrib == attribs and (elem.text or "").strip() == text: |
| 219 | + elem.set("zeuz", "ai") |
| 220 | + return True |
| 221 | + |
| 222 | + return False |
| 223 | + |
| 224 | + |
| 225 | +def read_settings_conf(): |
| 226 | + """Read settings from the Framework/settings.conf file.""" |
| 227 | + try: |
| 228 | + import configparser |
| 229 | + import os |
| 230 | + |
| 231 | + # Get the current file's directory and navigate to Framework/settings.conf |
| 232 | + current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 233 | + settings_path = os.path.join(os.path.dirname(os.path.dirname(current_dir)), 'Framework', 'settings.conf') |
| 234 | + |
| 235 | + config = configparser.ConfigParser() |
| 236 | + config.read(settings_path) |
| 237 | + api_key = config.get('Authentication', 'api-key').strip() |
| 238 | + server_address = config.get('Authentication', 'server_address').strip() |
| 239 | + url = server_address + "/" if server_address[-1] != "/" else server_address |
| 240 | + return { |
| 241 | + 'api_key': api_key, |
| 242 | + 'url': url |
| 243 | + } |
| 244 | + except Exception as e: |
| 245 | + print(f"Error reading settings.conf: {str(e)}") |
| 246 | + return {'api_key': '', 'url': ''} |
| 247 | + |
| 248 | +def send_to_zeuz(): |
| 249 | + """Send the inspected Element and UI hierarchy to Zeuz.""" |
| 250 | + selected_item = tree.selection() if view_mode.get() == "tree" else flat_tree.selection() |
| 251 | + if not selected_item: |
| 252 | + messagebox.showwarning("No Selection", "Please select an element first.") |
| 253 | + return |
| 254 | + |
| 255 | + try: |
| 256 | + # Read settings |
| 257 | + settings = read_settings_conf() |
| 258 | + if not settings['api_key'] or not settings['url']: |
| 259 | + messagebox.showerror("Configuration Error", "API key or server address not found in settings.conf") |
| 260 | + return |
| 261 | + |
| 262 | + # Read the original UI XML file |
| 263 | + tree_xml = ET.parse(UI_XML_PATH) |
| 264 | + root = tree_xml.getroot() |
| 265 | + |
| 266 | + # Get the XML string of the selected element |
| 267 | + selected_xml = tree.item(selected_item, "tags")[0] if view_mode.get() == "tree" else flat_tree.item(selected_item, "tags")[0] |
| 268 | + |
| 269 | + # Create an ElementTree from the selected XML string |
| 270 | + selected_element = ET.fromstring(selected_xml) |
| 271 | + find_matching_element(root, selected_element) |
| 272 | + |
| 273 | + # Convert the modified XML tree to string |
| 274 | + modified_xml_string = ET.tostring(root, encoding='unicode') |
| 275 | + |
| 276 | + # Send the request to Zeuz server |
| 277 | + headers = { |
| 278 | + "X-Api-Key": settings['api_key'], |
| 279 | + } |
| 280 | + content = json.dumps({ |
| 281 | + 'page_src': modified_xml_string, |
| 282 | + "action_type": "appium", |
| 283 | + }) |
| 284 | + response = requests.post( |
| 285 | + f"{settings['url']}ai_record_single_action/", |
| 286 | + headers=headers, |
| 287 | + data=content, |
| 288 | + verify=False |
| 289 | + ) |
| 290 | + |
| 291 | + if response.status_code == 200: |
| 292 | + messagebox.showinfo("Success", "Successfully sent to Zeuz!") |
| 293 | + else: |
| 294 | + messagebox.showerror("Error", f"Failed to send data: {response.text}") |
| 295 | + |
| 296 | + except Exception as e: |
| 297 | + messagebox.showerror("Error", f"Failed to process UI hierarchy: {str(e)}") |
| 298 | + |
211 | 299 |
|
212 | 300 | def update_screenshot(event=None): |
213 | 301 | """Update and highlight only the selected element in the screenshot.""" |
@@ -335,6 +423,9 @@ def tap_element(): |
335 | 423 | btn_toggle_view = ttk.Button(button_frame, text="Toggle View", command=toggle_view) |
336 | 424 | btn_toggle_view.pack(side="left", padx=5) |
337 | 425 |
|
| 426 | +btn_toggle_view = ttk.Button(button_frame, text="Send to Zeuz", command=send_to_zeuz) |
| 427 | +btn_toggle_view.pack(side="left", padx=5) |
| 428 | + |
338 | 429 | # View mode (Tree or Flat) |
339 | 430 | view_mode = tk.StringVar(value="tree") |
340 | 431 |
|
|
0 commit comments