From f39d4627eba75f881b2a5bf719c73193dafdd4ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E7=9F=A5=E6=98=AF=E5=93=AA=E4=B8=AA=E5=8F=B7?= <625574612@qq.com> Date: Fri, 26 Dec 2025 21:36:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81iphone=E6=A8=A1=E6=8B=9F?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 list_simulate_devices 方法获取已经打开的模拟器 --- phone_agent/xctest/connection.py | 75 +++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/phone_agent/xctest/connection.py b/phone_agent/xctest/connection.py index deb29369..f7b140c1 100644 --- a/phone_agent/xctest/connection.py +++ b/phone_agent/xctest/connection.py @@ -53,6 +53,79 @@ def __init__(self, wda_url: str = "http://localhost:8100"): For network devices, use http://:8100 """ self.wda_url = wda_url.rstrip("/") + + def list_simulate_devices(self) -> list[DeviceInfo]: + """ + List all Booted iOS Simulate devices. + + Returns: + List of DeviceInfo objects. + + Note: + Requires xcode-select to be installed. + Install on macOS: brew install xcode-select + """ + conn_type = ( + ConnectionType.NETWORK + ) + try: + # Get list of booted simulate + result = subprocess.run( + ["xcrun", "simctl", "list", "devices", "booted"], + capture_output=True, + text=True, + timeout=5, + ) + + devices = [] + + for line in result.stdout.strip().split("\n"): + simulate_info = line.strip() + if not simulate_info or "Booted" not in simulate_info: + continue + for uuid in simulate_info.split(" "): + if '-' not in uuid: + continue + uuid = uuid.replace("(", "").replace(")", "").replace(" ", "") + device_name_result = subprocess.run( + ["xcrun", "simctl", "getenv", uuid, "SIMULATOR_DEVICE_NAME"], + capture_output=True, + text=True, + timeout=5, + ) + device_version_result = subprocess.run( + ["xcrun", "simctl", "getenv", uuid, "SIMULATOR_RUNTIME_VERSION"], + capture_output=True, + text=True, + timeout=5, + ) + device_model_result = subprocess.run( + ["xcrun", "simctl", "getenv", uuid, "SIMULATOR_MODEL_IDENTIFIER"], + capture_output=True, + text=True, + timeout=5, + ) + devices.append( + DeviceInfo( + device_id=uuid, + status="connected", + connection_type=conn_type, + model=device_model_result.stdout.strip().split("\n"), + ios_version=device_version_result.stdout.strip().split("\n"), + device_name=device_name_result.stdout.strip().split("\n"), + ) + ) + return devices + + except FileNotFoundError: + print( + "Error: xcrun not found. Install xcode-select: brew install xcode-select" + ) + return [] + except Exception as e: + print(f"Error listing devices: {e}") + return [] + pass def list_devices(self) -> list[DeviceInfo]: """ @@ -379,4 +452,4 @@ def list_devices() -> list[DeviceInfo]: List of DeviceInfo objects. """ conn = XCTestConnection() - return conn.list_devices() + return conn.list_devices() + conn.list_simulate_devices()