修改驱动启用;两个大块逻辑。

This commit is contained in:
2026-03-13 18:01:36 +08:00
parent aa4d4c7d7c
commit b80f553009
10 changed files with 391 additions and 250 deletions

View File

@@ -23,13 +23,31 @@ import globals.create_link as create_link
class DeviceAutomation:
def __init__(self, device_id=None):
# 如果没有提供设备ID则自动获取
if device_id is None:
self.device_id = self.get_device_id()
else:
self.device_id = device_id
# def __init__(self, device_id=None):
# # 如果没有提供设备ID则自动获取
# if device_id is None:
# self.device_id = driver_utils.get_device_id()
# else:
# self.device_id = device_id
# # 初始化权限
# if driver_utils.grant_appium_permissions(self.device_id):
# logging.info(f"设备 {self.device_id} 授予Appium权限成功")
# else:
# logging.warning(f"设备 {self.device_id} 授予Appium权限失败")
# # 确保Appium服务器正在运行,不在运行则启动
# if not driver_utils.check_server_status(4723):
# driver_utils.start_appium_server()
# # 初始化Appium驱动和页面对象
# self.init_driver()
# # 创建测试结果目录
# self.results_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_results')
def __init__(self, driver=None, wait=None, device_id=None):
self.driver = driver
self.wait = wait
self.device_id = device_id
# 初始化权限
if driver_utils.grant_appium_permissions(self.device_id):
logging.info(f"设备 {self.device_id} 授予Appium权限成功")
@@ -42,80 +60,70 @@ class DeviceAutomation:
# 初始化Appium驱动和页面对象
self.init_driver()
# 创建测试结果目录
self.results_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_results')
@staticmethod
def get_device_id() -> str:
"""
获取设备ID优先使用已连接设备否则使用全局配置
"""
try:
# 检查已连接设备
result = subprocess.run(
["adb", "devices"],
capture_output=True,
text=True,
timeout=10
)
# @staticmethod
# def get_device_id() -> str:
# """
# 获取设备ID优先使用已连接设备否则使用全局配置
# """
# try:
# # 检查已连接设备
# result = subprocess.run(
# ["adb", "devices"],
# capture_output=True,
# text=True,
# timeout=10
# )
# # 解析设备列表
# for line in result.stdout.strip().split('\n')[1:]:
# if line.strip() and "device" in line and "offline" not in line:
# device_id = line.split('\t')[0]
# logging.info(f"使用已连接设备: {device_id}")
# global_variable.GLOBAL_DEVICE_ID = device_id
# return device_id
target_port = "4723"
for line in result.stdout.strip().split('\n')[1:]:
if line.strip() and "device" in line and "offline" not in line:
device_id = line.split('\t')[0]
# target_port = "4723"
# for line in result.stdout.strip().split('\n')[1:]:
# if line.strip() and "device" in line and "offline" not in line:
# device_id = line.split('\t')[0]
# 检查是否为无线设备且端口为4723
if ':' in device_id:
ip_port = device_id.split(':')
if len(ip_port) == 2 and ip_port[1] == target_port:
logging.info(f"找到目标无线设备(端口{target_port}): {device_id}")
global_variable.GLOBAL_DEVICE_ID = device_id
return device_id
# # 检查是否为无线设备且端口为4723
# if ':' in device_id:
# ip_port = device_id.split(':')
# if len(ip_port) == 2 and ip_port[1] == target_port:
# logging.info(f"找到目标无线设备(端口{target_port}): {device_id}")
# global_variable.GLOBAL_DEVICE_ID = device_id
# return device_id
# 如果没有找到端口4723的设备找其他无线设备
for line in result.stdout.strip().split('\n')[1:]:
if line.strip() and "device" in line and "offline" not in line:
device_id = line.split('\t')[0]
# # 如果没有找到端口4723的设备找其他无线设备
# for line in result.stdout.strip().split('\n')[1:]:
# if line.strip() and "device" in line and "offline" not in line:
# device_id = line.split('\t')[0]
# 检查是否为无线设备(任何端口)
if ':' in device_id and device_id.split(':')[-1].isdigit():
logging.info(f"未找到端口{target_port}的设备,使用其他无线设备: {device_id}")
global_variable.GLOBAL_DEVICE_ID = device_id
return device_id
# # 检查是否为无线设备(任何端口)
# if ':' in device_id and device_id.split(':')[-1].isdigit():
# logging.info(f"未找到端口{target_port}的设备,使用其他无线设备: {device_id}")
# global_variable.GLOBAL_DEVICE_ID = device_id
# return device_id
# 如果没有任何无线设备,找有线设备
for line in result.stdout.strip().split('\n')[1:]:
if line.strip() and "device" in line and "offline" not in line:
device_id = line.split('\t')[0]
logging.info(f"未找到无线设备,使用有线设备: {device_id}")
global_variable.GLOBAL_DEVICE_ID = device_id
return device_id
# # 如果没有任何无线设备,找有线设备
# for line in result.stdout.strip().split('\n')[1:]:
# if line.strip() and "device" in line and "offline" not in line:
# device_id = line.split('\t')[0]
# logging.info(f"未找到无线设备,使用有线设备: {device_id}")
# global_variable.GLOBAL_DEVICE_ID = device_id
# return device_id
logging.error("未找到任何可用设备")
return None
# logging.error("未找到任何可用设备")
# return None
except Exception as e:
logging.warning(f"设备检测失败: {e}")
# except Exception as e:
# logging.warning(f"设备检测失败: {e}")
# 使用全局配置
device_id = global_variable.GLOBAL_DEVICE_ID
logging.info(f"使用全局配置设备: {device_id}")
return device_id
# # 使用全局配置
# device_id = global_variable.GLOBAL_DEVICE_ID
# logging.info(f"使用全局配置设备: {device_id}")
# return device_id
def init_driver(self):
"""初始化Appium驱动"""
try:
# 使用全局函数初始化驱动
self.driver, self.wait = driver_utils.init_appium_driver(self.device_id)
# # 使用全局函数初始化驱动
# self.driver, self.wait = driver_utils.init_appium_driver(self.device_id)
# 初始化页面对象
logging.info(f"设备 {self.device_id} 开始初始化页面对象")
self.login_page = LoginPage(self.driver, self.wait)
@@ -273,9 +281,9 @@ class DeviceAutomation:
# GLOBAL_TESTED_BREAKPOINT_LIST 把已打完的写入日志文件
with open(os.path.join(self.results_dir, "打数据完成线路.txt"), "w", encoding='utf-8') as f:
for bp in global_variable.GLOBAL_TESTED_BREAKPOINT_LIST:
f.write(f"{bp}\n")
# with open(os.path.join(self.results_dir, "打数据完成线路.txt"), "w", encoding='utf-8') as f:
# for bp in global_variable.GLOBAL_TESTED_BREAKPOINT_LIST:
# f.write(f"{bp}\n")
return task_count > 0