22import os
33import subprocess
44import base64
5- import time
65from typing import Literal
76import asyncio
87
@@ -32,6 +31,7 @@ class DeviceInfo(BaseModel):
3231 """Model for device information."""
3332 serial : str
3433 status : str
34+ name : str | None = None
3535 # model: str | None = None
3636 # product: str | None = None
3737
@@ -42,33 +42,34 @@ def get_devices():
4242 # Get list of devices
4343 devices_output = run_adb_command (f"{ ADB_PATH } devices -l" )
4444 devices = []
45-
45+
4646 # Parse adb devices output
47- for line in devices_output .split ('\n ' )[1 :]: # Skip first line (header)
47+ index = 1
48+ for line in devices_output .split ("\n " )[1 :]: # Skip first line (header)
4849 if line .strip ():
4950 parts = line .split ()
5051 if len (parts ) >= 2 :
5152 serial = parts [0 ]
5253 status = parts [1 ]
54+ name = f"device { index } "
55+ index += 1
5356 # model = run_adb_command(f"{ADB_PATH} -s {serial} shell getprop ro.product.model")
5457 # product = run_adb_command(f"{ADB_PATH} -s {serial} shell getprop ro.product.name")
5558
56- devices .append (DeviceInfo (
57- serial = serial ,
58- status = status
59- ))
60-
59+ devices .append (DeviceInfo (serial = serial , status = status , name = name ))
60+
6161 return devices
6262 except Exception as e :
6363 return []
6464
65+
6566@router .get ("/inspect" )
66- def inspect ():
67+ def inspect (device_serial : str | None = None ):
6768 """Get the Mobile DOM and screenshot."""
6869 try :
6970 # Capture UI and screenshot
70- capture_ui_dump ()
71- capture_screenshot ()
71+ capture_ui_dump (device_serial = device_serial )
72+ capture_screenshot (device_serial = device_serial )
7273
7374 # Read XML file
7475 with open (UI_XML_PATH , 'r' ) as xml_file :
@@ -108,9 +109,12 @@ def run_adb_command(command):
108109 return f"Error: { e .stderr .strip ()} "
109110
110111
111- def capture_ui_dump ():
112+ def capture_ui_dump (device_serial : str | None = None ):
112113 """Capture the current UI hierarchy from the device"""
113- out = run_adb_command (f"{ ADB_PATH } shell uiautomator dump /sdcard/ui.xml" )
114+ device_flag = f"-s { device_serial } " if device_serial else ""
115+ out = run_adb_command (
116+ f"{ ADB_PATH } { device_flag } shell uiautomator dump /sdcard/ui.xml" .strip ()
117+ )
114118 if out .startswith ("Error:" ):
115119 from Framework .Built_In_Automation .Mobile .CrossPlatform .Appium .BuiltInFunctions import appium_driver
116120 if appium_driver is None :
@@ -119,22 +123,29 @@ def capture_ui_dump():
119123 with open (UI_XML_PATH , 'w' ) as xml_file :
120124 xml_file .write (page_src )
121125 else :
122- out = run_adb_command (f"{ ADB_PATH } pull /sdcard/ui.xml { UI_XML_PATH } " )
126+ out = run_adb_command (
127+ f"{ ADB_PATH } { device_flag } pull /sdcard/ui.xml { UI_XML_PATH } "
128+ )
123129 if out .startswith ("Error:" ):
124130 return
125131
126132
127- def capture_screenshot ():
133+ def capture_screenshot (device_serial : str | None = None ):
128134 """Capture the current UI hierarchy from the device"""
129- out = run_adb_command (f"{ ADB_PATH } shell screencap -p /sdcard/screen.png" )
135+ device_flag = f"-s { device_serial } " if device_serial else ""
136+ out = run_adb_command (
137+ f"{ ADB_PATH } { device_flag } shell screencap -p /sdcard/screen.png" .strip ()
138+ )
130139 if out .startswith ("Error:" ):
131140 from Framework .Built_In_Automation .Mobile .CrossPlatform .Appium .BuiltInFunctions import appium_driver
132141 if appium_driver is None :
133142 return
134143 full_screenshot_path = os .path .join (os .getcwd (), SCREENSHOT_PATH )
135144 appium_driver .save_screenshot (full_screenshot_path )
136145 else :
137- out = run_adb_command (f"{ ADB_PATH } pull /sdcard/screen.png { SCREENSHOT_PATH } " )
146+ out = run_adb_command (
147+ f"{ ADB_PATH } { device_flag } pull /sdcard/screen.png { SCREENSHOT_PATH } "
148+ )
138149 if out .startswith ("Error:" ):
139150 return
140151
0 commit comments