Skip to content

Commit b8faf63

Browse files
Send data to server
1 parent b3c73bd commit b8faf63

2 files changed

Lines changed: 95 additions & 4 deletions

File tree

Apps/Mobile/Android_Inspector_ZeuZ.py

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os
1+
import os, requests
22
import re
33
import json
44
import subprocess
@@ -110,7 +110,6 @@ def extract_elements(node):
110110
return [extract_elements(root)]
111111

112112

113-
114113
def generate_zeuz_action(element_xml, action_type, action_value="Sample Text"):
115114
"""
116115
Generate a Zeuz action in the required format.
@@ -208,6 +207,95 @@ def toggle_view():
208207
view_mode.set("tree")
209208
update_screenshot(None)
210209

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+
211299

212300
def update_screenshot(event=None):
213301
"""Update and highlight only the selected element in the screenshot."""
@@ -335,6 +423,9 @@ def tap_element():
335423
btn_toggle_view = ttk.Button(button_frame, text="Toggle View", command=toggle_view)
336424
btn_toggle_view.pack(side="left", padx=5)
337425

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+
338429
# View mode (Tree or Flat)
339430
view_mode = tk.StringVar(value="tree")
340431

Apps/Windows/ZeuZ_Windows_Inspector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ def Authenticate():
102102
global server, api_key
103103
config = configparser.ConfigParser()
104104
config.read("..\..\Framework\settings.conf")
105-
try: api_key = config.get("Authentication", "api-key")
105+
try: api_key = config.get("Authentication", "api-key").strip()
106106
except: api_key = ""
107-
try: server = config.get("Authentication", "server_address")
107+
try: server = config.get("Authentication", "server_address").strip()
108108
except: server = ""
109109
while not server or not api_key:
110110
server = input("Provide Server Address: ")

0 commit comments

Comments
 (0)