修复bug
This commit is contained in:
@@ -204,11 +204,10 @@ class DeviceInfo:
|
||||
if major > 17:
|
||||
print("进入iOS17设备的分支")
|
||||
print(f"[WDA] iOS>17 调用 IOSActivator (port={wdaScreenPort})")
|
||||
try:
|
||||
IOSActivator().activate(udid)
|
||||
print("wda启动完成")
|
||||
except Exception as e:
|
||||
print("错误信息:",e)
|
||||
print("准备启动隧道")
|
||||
out = IOSActivator().activate(udid)
|
||||
print("------------------",out)
|
||||
print("wda启动完成")
|
||||
else:
|
||||
print(f"[WDA] iOS<=17 启动 WDA app_start (port={wdaScreenPort})")
|
||||
dev = tidevice.Device(udid)
|
||||
|
||||
@@ -9,6 +9,7 @@ import subprocess
|
||||
from typing import Optional, List, Tuple, Dict, Set
|
||||
|
||||
from Entity.Variables import WdaAppBundleId
|
||||
import time as _t
|
||||
|
||||
|
||||
class IOSActivator:
|
||||
@@ -32,139 +33,178 @@ class IOSActivator:
|
||||
self._live_ifaces: Dict[str, Set[str]] = {} # udid -> {iface names}
|
||||
self._registered = False
|
||||
|
||||
|
||||
# =============== 公共入口 ===============
|
||||
def activate(
|
||||
self,
|
||||
udid: str,
|
||||
wda_bundle_id: str = WdaAppBundleId,
|
||||
ready_timeout_sec: float = 60.0,
|
||||
wda_bundle_id: Optional[str] = WdaAppBundleId,
|
||||
ready_timeout_sec: float = 120.0,
|
||||
mount_retries: int = 3,
|
||||
backoff_seconds: float = 2.0,
|
||||
rsd_probe_retries: int = 5,
|
||||
rsd_probe_delay_sec: float = 3.0,
|
||||
pre_mount_first: bool = True,
|
||||
mount_retries: int = 2,
|
||||
backoff_seconds: float = 1.5,
|
||||
keep_tunnel: bool = False,
|
||||
broad_cleanup_on_exit: bool = True,
|
||||
keep_tunnel: bool = False, # 默认 False:WDA 拉起后关闭隧道
|
||||
broad_cleanup_on_exit: bool = True, # 退出时顺带清理所有 pmd3 残留网卡
|
||||
) -> str:
|
||||
"""
|
||||
Windows 简版:不读任何 tunneld 日志,也不做 RSD 解析。
|
||||
逻辑:先探活 -> 开隧道 -> 直接用 HTTP 隧道端口反复尝试启动 WDA -> 探活成功即返回。
|
||||
流程:挂镜像(可选) -> 开隧道 -> 等 RSD -> 启动 WDA
|
||||
- keep_tunnel=False:WDA 启动后关闭隧道并清理
|
||||
- keep_tunnel=True:隧道常驻,由上层/atexit 清理
|
||||
"""
|
||||
import time, ctypes, traceback
|
||||
|
||||
if not udid or not isinstance(udid, str):
|
||||
raise ValueError("udid is required and must be a non-empty string")
|
||||
|
||||
print(f"[activate] UDID={udid}", flush=True)
|
||||
print(f"[activate] UDID = {udid}")
|
||||
self._ensure_exit_hooks(broad_cleanup_on_exit=broad_cleanup_on_exit)
|
||||
|
||||
# —— 管理员提示(Windows 清理虚拟网卡常用)——
|
||||
try:
|
||||
if ctypes.windll.shell32.IsUserAnAdmin() == 0:
|
||||
print("[⚠] 未以管理员运行:若需要移除虚拟网卡,可能失败。", flush=True)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# —— 退出钩子(可选)——
|
||||
try:
|
||||
self._ensure_exit_hooks(broad_cleanup_on_exit=broad_cleanup_on_exit) # type: ignore[attr-defined]
|
||||
except Exception as e:
|
||||
print(f"[activate] _ensure_exit_hooks warn: {e}", flush=True)
|
||||
|
||||
# —— 小工具:探活 WDA —— #
|
||||
def _wda_alive(timeout: float = 2.0) -> bool:
|
||||
# Windows 管理员检测
|
||||
if os.name == "nt":
|
||||
import ctypes
|
||||
try:
|
||||
if hasattr(self, "_wda_alive_now"):
|
||||
return bool(self._wda_alive_now(udid, timeout=timeout)) # type: ignore[attr-defined]
|
||||
if hasattr(self, "_wda_client"):
|
||||
cli = self._wda_client(udid) # type: ignore[attr-defined]
|
||||
if hasattr(cli, "wait_ready"):
|
||||
return bool(cli.wait_ready(timeout=timeout))
|
||||
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
|
||||
except Exception:
|
||||
return False
|
||||
return False
|
||||
is_admin = False
|
||||
if not is_admin:
|
||||
print("[⚠] 未以管理员运行:若需要移除虚拟网卡,可能失败。")
|
||||
|
||||
# 0) 快路径:WDA 已活
|
||||
if _wda_alive(2.0):
|
||||
print("[activate] WDA already alive, skip launching.", flush=True)
|
||||
return "WDA already alive"
|
||||
start_ts = _t.time()
|
||||
|
||||
# 1) 预挂载(失败不致命)
|
||||
if pre_mount_first and hasattr(self, "_auto_mount_developer_disk"):
|
||||
if pre_mount_first:
|
||||
try:
|
||||
self._auto_mount_developer_disk(udid, retries=mount_retries,
|
||||
backoff_seconds=backoff_seconds) # type: ignore[attr-defined]
|
||||
time.sleep(1.5)
|
||||
self._auto_mount_developer_disk(
|
||||
udid, retries=mount_retries, backoff_seconds=backoff_seconds
|
||||
)
|
||||
_t.sleep(2)
|
||||
except Exception as e:
|
||||
print(f"[activate] 预挂载失败(继续):{e}", flush=True)
|
||||
print(f"[activate] 预挂载失败(稍后再试):{e}")
|
||||
|
||||
# 2) 开隧道(关键:拿到 HTTP 端口即可;不读取任何 stdout/stderr)
|
||||
proc = None
|
||||
http_host, http_port = "127.0.0.1", None
|
||||
try:
|
||||
ret = self._start_tunneld(udid) # type: ignore[attr-defined]
|
||||
if isinstance(ret, tuple):
|
||||
proc, http_port = ret[0], ret[1]
|
||||
else:
|
||||
proc = ret
|
||||
if http_port is None:
|
||||
# 若你的 _start_tunneld 固定端口,可在这里写死(例如 8100/某自定义端口)
|
||||
raise RuntimeError("未获取到 HTTP 隧道端口(_start_tunneld 未返回端口)")
|
||||
except Exception:
|
||||
# 即便开隧道失败,也再探活一次(可能本来就活)
|
||||
if _wda_alive(2.0):
|
||||
print("[activate] WDA already alive (tunnel start failed but OK).", flush=True)
|
||||
return "WDA already alive"
|
||||
raise
|
||||
# 2) 启动 tunneld
|
||||
http_host: Optional[str] = None
|
||||
http_port: Optional[str] = None # ⚠️ 端口以 str 存储
|
||||
rsd_host: Optional[str] = None
|
||||
rsd_port: Optional[str] = None # ⚠️ 端口以 str 存储
|
||||
iface_names: Set[str] = set()
|
||||
|
||||
print(f"[tunneld] HTTP tunnel at {http_host}:{http_port}", flush=True)
|
||||
proc, _port_ignored = self._start_tunneld(udid)
|
||||
self._live_procs[udid] = proc
|
||||
self._live_ifaces[udid] = iface_names
|
||||
|
||||
# 3) 直接用 HTTP 隧道反复尝试启动 WDA + 探活
|
||||
deadline = time.time() + (ready_timeout_sec if ready_timeout_sec > 0 else 60.0)
|
||||
launched = False
|
||||
captured: List[str] = []
|
||||
out: str = ""
|
||||
wda_started = False
|
||||
mount_done = pre_mount_first
|
||||
|
||||
try:
|
||||
while time.time() < deadline:
|
||||
# 已活则成功返回
|
||||
if _wda_alive(1.5):
|
||||
print("[activate] WDA detected alive.", flush=True)
|
||||
launched = True
|
||||
assert proc.stdout is not None
|
||||
for line in proc.stdout:
|
||||
captured.append(line)
|
||||
# 日志长度控制,防止常驻时内存涨太多
|
||||
if len(captured) > 20000:
|
||||
captured = captured[-10000:]
|
||||
|
||||
print(f"[tunneld] {line}", end="")
|
||||
|
||||
# 捕获虚拟网卡名
|
||||
for m in self.IFACE_RE.finditer(line):
|
||||
iface_names.add(m.group(1))
|
||||
|
||||
# 子进程若退出则停止读取
|
||||
if proc.poll() is not None:
|
||||
break
|
||||
|
||||
# 尝试发起一次 HTTP 启动(失败就下一轮重试)
|
||||
try:
|
||||
if hasattr(self, "_launch_wda_via_http_tunnel"):
|
||||
self._launch_wda_via_http_tunnel( # type: ignore[attr-defined]
|
||||
# 捕获 HTTP 网关端口(保持为字符串)
|
||||
if http_port is None:
|
||||
m = self.HTTP_RE.search(line)
|
||||
if m:
|
||||
http_host = m.group(1)
|
||||
http_port = m.group(2)
|
||||
# 简单校验
|
||||
try:
|
||||
_ = int(http_port)
|
||||
except Exception:
|
||||
print(f"[tunneld] bad http port: {http_port}")
|
||||
http_host, http_port = None, None
|
||||
else:
|
||||
print(f"[tunneld] Tunnel API: {http_host}:{http_port}")
|
||||
|
||||
# 只处理当前 UDID 的 RSD 行
|
||||
if not self._line_is_for_udid(line, udid):
|
||||
continue
|
||||
|
||||
m = self.RSD_CREATED_RE.search(line) or self.RSD_FALLBACK_RE.search(line)
|
||||
if m and rsd_host is None and rsd_port is None:
|
||||
rsd_host = m.group(1)
|
||||
rsd_port = m.group(2)
|
||||
try:
|
||||
_ = int(rsd_port) # 仅作数字校验
|
||||
except Exception:
|
||||
print(f"[tunneld] bad rsd port: {rsd_port}")
|
||||
rsd_host, rsd_port = None, None
|
||||
else:
|
||||
print(f"[tunneld] Device-level tunnel ready (RSD {rsd_host}:{rsd_port}).")
|
||||
|
||||
# ========= 尝试启动 WDA =========
|
||||
if (not wda_started) and wda_bundle_id and (rsd_host is not None) and (rsd_port is not None):
|
||||
if not mount_done:
|
||||
self._auto_mount_developer_disk(
|
||||
udid, retries=mount_retries, backoff_seconds=backoff_seconds
|
||||
)
|
||||
_t.sleep(2)
|
||||
mount_done = True
|
||||
|
||||
# RSD 优先;探测时临时转 int;启动命令仍传 str 端口
|
||||
rsd_port_int = int(rsd_port)
|
||||
if self._wait_for_rsd_ready(
|
||||
rsd_host, rsd_port_int, retries=rsd_probe_retries, delay=rsd_probe_delay_sec
|
||||
):
|
||||
# 这里的实现通常会拼 subprocess 命令行,故端口保持 str
|
||||
self._launch_wda_via_rsd(
|
||||
bundle_id=wda_bundle_id,
|
||||
http_host=http_host,
|
||||
http_port=str(http_port),
|
||||
rsd_host=rsd_host,
|
||||
rsd_port=rsd_port, # ⚠️ 传入 str,避免 subprocess 报错
|
||||
udid=udid,
|
||||
)
|
||||
except Exception as e:
|
||||
# 仅打印,不中断;下一次循环再试
|
||||
print(f"[activate] _launch_wda_via_http_tunnel error: {e}", flush=True)
|
||||
wda_started = True
|
||||
elif (http_host is not None) and (http_port is not None):
|
||||
self._launch_wda_via_http_tunnel(
|
||||
bundle_id=wda_bundle_id,
|
||||
http_host=http_host,
|
||||
http_port=http_port, # ⚠️ 传入 str
|
||||
udid=udid,
|
||||
)
|
||||
wda_started = True
|
||||
else:
|
||||
raise RuntimeError("No valid tunnel endpoint for WDA.")
|
||||
|
||||
# 启动后给一点时间让 WDA ready
|
||||
for _ in range(3):
|
||||
if _wda_alive(1.0):
|
||||
launched = True
|
||||
# ✅ WDA 已启动;默认一次性模式直接退出读取循环
|
||||
if wda_started and not keep_tunnel:
|
||||
_t.sleep(0.5) # 给隧道多刷几行
|
||||
print("[activate] WDA launched; exiting reader loop (keep_tunnel=False).")
|
||||
break
|
||||
time.sleep(0.5)
|
||||
|
||||
if launched:
|
||||
# 超时保护(仅在 WDA 尚未启动时生效)
|
||||
if (not wda_started) and ready_timeout_sec > 0 and (_t.time() - start_ts > ready_timeout_sec):
|
||||
print(f"[tunneld] Timeout waiting for device tunnel ({ready_timeout_sec}s). Aborting.")
|
||||
break
|
||||
|
||||
time.sleep(1.0) # 下一轮重试
|
||||
|
||||
if not launched:
|
||||
raise RuntimeError(f"WDA not ready within {ready_timeout_sec}s via HTTP tunnel")
|
||||
|
||||
print("[activate] Done.", flush=True)
|
||||
return f"http://{http_host}:{http_port}"
|
||||
print("[activate] 启动 WDA 读取阶段结束")
|
||||
out = "".join(captured)
|
||||
|
||||
except Exception as e:
|
||||
print(f"[activate] 发生异常:{e}")
|
||||
raise
|
||||
finally:
|
||||
if not keep_tunnel:
|
||||
try:
|
||||
self.stop_tunnel(udid, broad_cleanup=broad_cleanup_on_exit) # type: ignore[attr-defined]
|
||||
except Exception as e:
|
||||
print(f"[activate] stop_tunnel warn: {e}", flush=True)
|
||||
self.stop_tunnel(udid, broad_cleanup=broad_cleanup_on_exit)
|
||||
except Exception as ce:
|
||||
print(f"[activate] stop_tunnel 清理异常:{ce}")
|
||||
|
||||
print("[activate] Done.")
|
||||
return out
|
||||
|
||||
|
||||
# =============== 外部可显式调用的清理 ===============
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -556,6 +556,7 @@ class ScriptManager():
|
||||
return
|
||||
else:
|
||||
print(f"找不到输入框")
|
||||
raise Exception("找不到输入框")
|
||||
|
||||
input = session.xpath('//XCUIElementTypeSearchField')
|
||||
if input.exists:
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user