多设备启动,端口固定,时间接口未返回

This commit is contained in:
2026-02-04 10:58:46 +08:00
parent cc59e8b8da
commit 9ce1dbadc1
22 changed files with 1344 additions and 297 deletions

View File

@@ -60,6 +60,21 @@ def init_appium_driver(device_id, app_package="com.bjjw.cjgc", app_activity=".ac
# 等待应用稳定
time.sleep(2)
# 设置屏幕永不休眠
try:
# 使用ADB命令设置屏幕永不休眠
screen_timeout_cmd = [
"adb", "-s", device_id,
"shell", "settings", "put", "system", "screen_off_timeout", "86400000"
]
timeout_result = subprocess.run(screen_timeout_cmd, capture_output=True, text=True, timeout=15)
if timeout_result.returncode == 0:
logging.info(f"设备 {device_id} 已成功设置屏幕永不休眠")
else:
logging.warning(f"设备 {device_id} 设置屏幕永不休眠失败: {timeout_result.stderr}")
except Exception as timeout_error:
logging.warning(f"设备 {device_id} 设置屏幕永不休眠时出错: {str(timeout_error)}")
logging.info(f"设备 {device_id} Appium驱动初始化完成")
return driver, wait
@@ -87,7 +102,7 @@ def check_session_valid(driver, device_id=None):
bool: 会话有效返回True否则返回False
"""
if device_id is None:
device_id = global_variable.GLOBAL_DEVICE_ID
device_id = global_variable.get_device_id()
device_str = f"设备 {device_id} " if device_id else ""
if not driver:
@@ -158,7 +173,7 @@ def reconnect_driver(device_id, old_driver=None, app_package="com.bjjw.cjgc", ap
"""
# 使用传入的device_id或从全局变量获取
if not device_id:
device_id = global_variable.GLOBAL_DEVICE_ID
device_id = global_variable.get_device_id()
# 修复device_id参数类型问题并使用全局设备ID作为备用
actual_device_id = device_id
@@ -174,7 +189,7 @@ def reconnect_driver(device_id, old_driver=None, app_package="com.bjjw.cjgc", ap
# 如果仍然没有有效的设备ID使用全局变量
if not actual_device_id or (isinstance(actual_device_id, str) and ("session=" in actual_device_id or len(actual_device_id.strip()) == 0)):
actual_device_id = global_variable.GLOBAL_DEVICE_ID
actual_device_id = global_variable.get_device_id()
logging.warning(f"无法获取有效设备ID使用全局变量GLOBAL_DEVICE_ID: {actual_device_id}")
device_id = actual_device_id # 使用修正后的设备ID
@@ -395,7 +410,6 @@ def ensure_appium_server_running(port=4723):
logging.error("Appium 服务启动超时!请检查 appium 命令是否在命令行可直接运行。")
return False
# ... (保留 init_appium_driver, safe_quit_driver 等其他函数) ...
def wait_for_appium_start(port, timeout=10):
"""
@@ -442,7 +456,7 @@ def safe_quit_driver(driver, device_id=None):
device_id: 设备ID可选
"""
if device_id is None:
device_id = global_variable.GLOBAL_DEVICE_ID
device_id = global_variable.get_device_id()
device_str = f"设备 {device_id} " if device_id else ""
logging.info(f"{device_str}开始关闭驱动")
@@ -504,7 +518,7 @@ def launch_app_manually(driver, device_id, package_name="com.bjjw.cjgc", activit
"""
try:
if not device_id:
device_id = global_variable.GLOBAL_DEVICE_ID
device_id = global_variable.get_device_id()
# 尝试从driver获取设备ID
if driver and hasattr(driver, 'capabilities'):
device_id = driver.capabilities.get('udid')