action建立连接(主食了开始测量之后的逻辑),add_trasition(调用run_add()函数)加转点。
This commit is contained in:
Binary file not shown.
86
actions.py
86
actions.py
@@ -24,27 +24,6 @@ import globals.create_link as create_link
|
||||
|
||||
|
||||
class DeviceAutomation:
|
||||
# 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
|
||||
@@ -213,9 +192,7 @@ class DeviceAutomation:
|
||||
return False
|
||||
|
||||
task_count = 0
|
||||
max_tasks = 1 # 最大任务数量,防止无限循环
|
||||
|
||||
# while task_count < max_tasks:
|
||||
|
||||
# 获取测量任务
|
||||
logging.info(f"设备 {self.device_id} 获取测量任务 (第{task_count + 1}次)")
|
||||
# task_data = apis.get_measurement_task()
|
||||
@@ -243,15 +220,9 @@ class DeviceAutomation:
|
||||
logging.info(f"设备 {self.device_id} 当前要处理的项目名称:{global_variable.GLOBAL_CURRENT_PROJECT_NAME}")
|
||||
|
||||
# 执行测量操作
|
||||
# logging.info(f"设备 {self.device_id} 开始执行测量操作")
|
||||
if not self.measure_tabbar_page.measure_tabbar_page_manager():
|
||||
logging.error(f"设备 {self.device_id} 测量操作执行失败")
|
||||
|
||||
# # 返回到测量页面
|
||||
# self.driver.back()
|
||||
# self.check_and_click_confirm_popup_appium()
|
||||
|
||||
# continue # 继续下一个任务
|
||||
|
||||
logging.info(f"设备 {self.device_id} 测量页面操作执行成功")
|
||||
|
||||
@@ -259,41 +230,33 @@ class DeviceAutomation:
|
||||
logging.info(f"设备 {self.device_id} 开始执行断面里程配置")
|
||||
if not self.section_mileage_config_page.section_mileage_config_page_manager():
|
||||
logging.error(f"设备 {self.device_id} 断面里程配置执行失败")
|
||||
# continue # 继续下一个任务
|
||||
|
||||
# 任务完成后短暂等待
|
||||
logging.info(f"设备 {self.device_id} 第{task_count}个任务完成")
|
||||
task_count += 1
|
||||
|
||||
# # +++++++++++++white+++++++++++++++++
|
||||
|
||||
# logging.info(f"设备 {self.device_id} 已完成{task_count}个任务,结束打数据流程")
|
||||
# if task_count == 0:
|
||||
# logging.error(f"没有完成打数据的线路,结束任务")
|
||||
# # 执行打数据加转点
|
||||
# if not self.check_station.run():
|
||||
# logging.error(f"设备 {self.device_id} 打数据加转点执行失败")
|
||||
# return False
|
||||
|
||||
# 执行打数据加转点
|
||||
if not self.check_station.run():
|
||||
logging.error(f"设备 {self.device_id} 打数据加转点执行失败")
|
||||
return False
|
||||
# # 执行上传配置管理,传入当前断点名称
|
||||
# breakpoint_name = global_variable.GLOBAL_CURRENT_PROJECT_NAME
|
||||
# line_num = global_variable.GLOBAL_LINE_NUM
|
||||
# if self.upload_config_page.upload_config_page_manager(self.results_dir, breakpoint_name, line_num):
|
||||
# logging.info(f"设备 {self.device_id} 断点 '{breakpoint_name}' 上传成功")
|
||||
# upload_success_count += 1
|
||||
# else:
|
||||
# logging.error(f"设备 {self.device_id} 断点 '{breakpoint_name}' 上传失败")
|
||||
# for i in range(3):
|
||||
# if self.upload_config_page.upload_config_page_manager(self.results_dir, breakpoint_name, line_num):
|
||||
# logging.info(f"设备 {self.device_id} 断点 '{breakpoint_name}' 重试上传成功")
|
||||
# upload_success_count += 1
|
||||
# break
|
||||
# else:
|
||||
# logging.error(f"设备 {self.device_id} 断点 '{breakpoint_name}' 上传失败,第 {i+1} 次重试")
|
||||
|
||||
# 执行上传配置管理,传入当前断点名称
|
||||
breakpoint_name = global_variable.GLOBAL_CURRENT_PROJECT_NAME
|
||||
line_num = global_variable.GLOBAL_LINE_NUM
|
||||
if self.upload_config_page.upload_config_page_manager(self.results_dir, breakpoint_name, line_num):
|
||||
logging.info(f"设备 {self.device_id} 断点 '{breakpoint_name}' 上传成功")
|
||||
upload_success_count += 1
|
||||
else:
|
||||
logging.error(f"设备 {self.device_id} 断点 '{breakpoint_name}' 上传失败")
|
||||
for i in range(3):
|
||||
if self.upload_config_page.upload_config_page_manager(self.results_dir, breakpoint_name, line_num):
|
||||
logging.info(f"设备 {self.device_id} 断点 '{breakpoint_name}' 重试上传成功")
|
||||
upload_success_count += 1
|
||||
break
|
||||
else:
|
||||
logging.error(f"设备 {self.device_id} 断点 '{breakpoint_name}' 上传失败,第 {i+1} 次重试")
|
||||
|
||||
return task_count > 0
|
||||
# return task_count > 0
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"设备 {self.device_id} 处理应用状态时出错: {str(e)}")
|
||||
@@ -314,6 +277,15 @@ if __name__ == "__main__":
|
||||
logging.info(f"设备 {automation.device_id} 自动化流程执行成功")
|
||||
else:
|
||||
logging.error(f"设备 {automation.device_id} 自动化流程执行失败")
|
||||
|
||||
# 保持脚本运行,不关闭连接
|
||||
logging.info("自动化流程执行完成,保持连接状态...")
|
||||
logging.info("按 Ctrl+C 退出并关闭连接")
|
||||
try:
|
||||
while True:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
logging.info("用户中断,准备关闭连接...")
|
||||
except Exception as e:
|
||||
logging.error(f"设备执行出错: {str(e)}")
|
||||
finally:
|
||||
|
||||
146
add_transition_point.py
Normal file
146
add_transition_point.py
Normal file
@@ -0,0 +1,146 @@
|
||||
"""
|
||||
添加转点脚本
|
||||
使用已建立的 Appium 连接来添加转点
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import logging
|
||||
import sys
|
||||
from selenium.common.exceptions import TimeoutException, NoSuchElementException
|
||||
from appium.webdriver.common.appiumby import AppiumBy
|
||||
from selenium.webdriver.support import expected_conditions as EC
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
|
||||
# Session 信息文件路径
|
||||
SESSION_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "appium_session.json")
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s: %(message)s")
|
||||
|
||||
|
||||
def load_session_info():
|
||||
"""从文件加载 session 信息"""
|
||||
if not os.path.exists(SESSION_FILE):
|
||||
logging.error(f"Session 文件不存在: {SESSION_FILE}")
|
||||
logging.error("请先运行 connect_appium.py 建立连接")
|
||||
return None
|
||||
|
||||
try:
|
||||
with open(SESSION_FILE, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except Exception as e:
|
||||
logging.error(f"读取 session 文件失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def attach_to_session(session_id, device_id):
|
||||
"""Attach 到现有的 Appium session"""
|
||||
from appium import webdriver
|
||||
from appium.options.android import UiAutomator2Options
|
||||
|
||||
try:
|
||||
server_url = "http://127.0.0.1:4723/wd/hub"
|
||||
|
||||
# 创建 options
|
||||
options = UiAutomator2Options()
|
||||
options.platform_name = "Android"
|
||||
options.device_name = device_id
|
||||
options.automation_name = "UiAutomator2"
|
||||
options.udid = device_id
|
||||
|
||||
# 连接到现有 session
|
||||
driver = webdriver.Remote(
|
||||
server_url,
|
||||
options=options
|
||||
)
|
||||
|
||||
# 手动设置 session_id
|
||||
driver.session_id = session_id
|
||||
|
||||
# 验证 session 是否有效
|
||||
try:
|
||||
current_package = driver.current_package
|
||||
logging.info(f"Session 有效,当前应用: {current_package}")
|
||||
except Exception as e:
|
||||
logging.error(f"Session 无效: {e}")
|
||||
return None, None
|
||||
|
||||
wait = WebDriverWait(driver, 20)
|
||||
return driver, wait
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Attach 到 session 失败: {e}")
|
||||
return None, None
|
||||
|
||||
|
||||
def get_driver_from_connection():
|
||||
"""从已建立的连接获取 driver"""
|
||||
session_info = load_session_info()
|
||||
if not session_info:
|
||||
return None, None, None
|
||||
|
||||
session_id = session_info.get("session_id")
|
||||
device_id = session_info.get("device_id")
|
||||
|
||||
if not session_id or not device_id:
|
||||
logging.error("Session 信息不完整")
|
||||
return None, None, None
|
||||
|
||||
driver, wait = attach_to_session(session_id, device_id)
|
||||
return driver, wait, device_id
|
||||
|
||||
|
||||
def add_transition_point(driver, wait, device_id=None):
|
||||
"""
|
||||
添加转点
|
||||
|
||||
参数:
|
||||
driver: WebDriver 实例
|
||||
wait: WebDriverWait 实例
|
||||
device_id: 设备ID(可选)
|
||||
|
||||
返回:
|
||||
bool: 是否成功
|
||||
"""
|
||||
device_str = f"设备 {device_id} " if device_id else ""
|
||||
|
||||
try:
|
||||
# 查找并点击添加转点按钮
|
||||
add_transition_btn = wait.until(
|
||||
EC.element_to_be_clickable((AppiumBy.ID, "com.bjjw.cjgc:id/btn_add_ZPoint"))
|
||||
)
|
||||
add_transition_btn.click()
|
||||
logging.info(f"{device_str}已点击添加转点按钮")
|
||||
return True
|
||||
except TimeoutException:
|
||||
logging.error(f"{device_str}等待添加转点按钮超时")
|
||||
return False
|
||||
except Exception as e:
|
||||
logging.error(f"{device_str}添加转点时出错: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
# 获取连接
|
||||
driver, wait, device_id = get_driver_from_connection()
|
||||
|
||||
if not driver:
|
||||
logging.error("无法获取 Appium 连接,请先运行 connect_appium.py")
|
||||
return False
|
||||
|
||||
logging.info(f"成功连接到设备: {device_id}")
|
||||
|
||||
# 添加转点
|
||||
result = add_transition_point(driver, wait, device_id)
|
||||
|
||||
if result:
|
||||
logging.info("添加转点成功!")
|
||||
else:
|
||||
logging.error("添加转点失败")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = main()
|
||||
sys.exit(0 if success else 1)
|
||||
1064
add_trasition.py
Normal file
1064
add_trasition.py
Normal file
File diff suppressed because it is too large
Load Diff
169
connect_appium.py
Normal file
169
connect_appium.py
Normal file
@@ -0,0 +1,169 @@
|
||||
"""
|
||||
建立 Appium 连接脚本
|
||||
运行此脚本会创建 Appium session 并保存 session 信息到文件
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import logging
|
||||
import globals.driver_utils as driver_utils
|
||||
import globals.global_variable as global_variable
|
||||
|
||||
# Session 信息保存路径
|
||||
SESSION_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "appium_session.json")
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s: %(message)s")
|
||||
|
||||
|
||||
def create_connection():
|
||||
"""创建 Appium 连接并保存 session 信息"""
|
||||
try:
|
||||
# 获取设备ID
|
||||
device_id = driver_utils.get_device_id()
|
||||
if not device_id:
|
||||
logging.error("未找到可用设备")
|
||||
return None, None, None
|
||||
|
||||
logging.info(f"使用设备: {device_id}")
|
||||
global_variable.GLOBAL_DEVICE_ID = device_id
|
||||
|
||||
# 确保 Appium 服务器运行
|
||||
if not driver_utils.check_server_status(4723):
|
||||
logging.info("Appium 服务器未运行,正在启动...")
|
||||
driver_utils.start_appium_server()
|
||||
|
||||
# 授予权限
|
||||
if driver_utils.grant_appium_permissions(device_id):
|
||||
logging.info(f"设备 {device_id} 授予Appium权限成功")
|
||||
else:
|
||||
logging.warning(f"设备 {device_id} 授予Appium权限失败")
|
||||
|
||||
# 初始化驱动
|
||||
driver, wait = driver_utils.init_appium_driver(device_id)
|
||||
|
||||
if not driver:
|
||||
logging.error("驱动初始化失败")
|
||||
return None, None, None
|
||||
|
||||
# 保存 session 信息到文件
|
||||
session_info = {
|
||||
"session_id": driver.session_id,
|
||||
"server_url": driver.command_executor._url,
|
||||
"device_id": device_id,
|
||||
"capabilities": dict(driver.capabilities) if driver.capabilities else {}
|
||||
}
|
||||
|
||||
with open(SESSION_FILE, "w", encoding="utf-8") as f:
|
||||
json.dump(session_info, f, ensure_ascii=False, indent=2)
|
||||
|
||||
logging.info(f"Session 信息已保存到: {SESSION_FILE}")
|
||||
logging.info(f"Session ID: {driver.session_id}")
|
||||
|
||||
return driver, wait, device_id
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"创建连接失败: {e}")
|
||||
return None, None, None
|
||||
|
||||
|
||||
def get_connection():
|
||||
"""获取现有连接(如果存在且有效)"""
|
||||
if not os.path.exists(SESSION_FILE):
|
||||
logging.info("Session 文件不存在,需要创建新连接")
|
||||
return None, None, None
|
||||
|
||||
try:
|
||||
with open(SESSION_FILE, "r", encoding="utf-8") as f:
|
||||
session_info = json.load(f)
|
||||
|
||||
session_id = session_info.get("session_id")
|
||||
device_id = session_info.get("device_id")
|
||||
|
||||
if not session_id or not device_id:
|
||||
logging.warning("Session 文件信息不完整")
|
||||
return None, None, None
|
||||
|
||||
# 尝试 attach 到现有 session
|
||||
driver, wait = attach_to_session(session_id, device_id)
|
||||
|
||||
if driver:
|
||||
global_variable.GLOBAL_DEVICE_ID = device_id
|
||||
logging.info(f"成功连接到现有 Session: {session_id}")
|
||||
return driver, wait, device_id
|
||||
else:
|
||||
logging.warning("无法连接到现有 Session")
|
||||
return None, None, None
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"获取连接失败: {e}")
|
||||
return None, None, None
|
||||
|
||||
|
||||
def attach_to_session(session_id, device_id):
|
||||
"""Attach 到现有的 Appium session"""
|
||||
from appium import webdriver
|
||||
from appium.options.android import UiAutomator2Options
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
|
||||
try:
|
||||
server_url = "http://127.0.0.1:4723/wd/hub"
|
||||
|
||||
# 创建 options,但不启动新应用
|
||||
options = UiAutomator2Options()
|
||||
options.platform_name = "Android"
|
||||
options.device_name = device_id
|
||||
options.automation_name = "UiAutomator2"
|
||||
options.udid = device_id
|
||||
|
||||
# 连接到现有 session
|
||||
driver = webdriver.Remote(
|
||||
server_url,
|
||||
options=options
|
||||
)
|
||||
|
||||
# 手动设置 session_id
|
||||
driver.session_id = session_id
|
||||
driver.command_executor._commands["getSession"] = ("GET", "/session/$sessionId")
|
||||
|
||||
# 验证 session 是否有效
|
||||
try:
|
||||
current_package = driver.current_package
|
||||
logging.info(f"Session 有效,当前应用: {current_package}")
|
||||
except Exception as e:
|
||||
logging.error(f"Session 无效: {e}")
|
||||
return None, None
|
||||
|
||||
wait = WebDriverWait(driver, 20)
|
||||
return driver, wait
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Attach 到 session 失败: {e}")
|
||||
return None, None
|
||||
|
||||
|
||||
def close_connection():
|
||||
"""关闭连接并清理 session 文件"""
|
||||
global_variable.GLOBAL_DEVICE_ID = None
|
||||
|
||||
if os.path.exists(SESSION_FILE):
|
||||
os.remove(SESSION_FILE)
|
||||
logging.info("Session 文件已删除")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 作为独立脚本运行时,创建新连接
|
||||
driver, wait, device_id = create_connection()
|
||||
|
||||
if driver:
|
||||
logging.info("连接建立成功!")
|
||||
logging.info("可以运行 add_transition_point.py 来添加转点")
|
||||
logging.info("按 Ctrl+C 退出并保持连接...")
|
||||
|
||||
try:
|
||||
# 保持连接,等待用户操作
|
||||
import time
|
||||
while True:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
logging.info("用户中断,保持连接状态")
|
||||
else:
|
||||
logging.error("连接建立失败")
|
||||
Binary file not shown.
Binary file not shown.
@@ -191,7 +191,24 @@ def setup_adb_wireless():
|
||||
target_port = 4723
|
||||
print(f"🚀 开始无线 ADB 建链(端口 {target_port})")
|
||||
|
||||
# 获取USB连接的设备
|
||||
# ===== 优先检查是否已有无线连接 =====
|
||||
wireless_connections = check_wireless_connections(target_port)
|
||||
if wireless_connections:
|
||||
print(f"📡 发现 {len(wireless_connections)} 个现有无线连接")
|
||||
for conn_id, ip, port in wireless_connections:
|
||||
if port == target_port:
|
||||
print(f"✅ 已存在目标端口 {target_port} 的无线连接: {conn_id}")
|
||||
# 验证连接是否有效
|
||||
verify_result = run_command("adb devices")
|
||||
if conn_id in verify_result and "device" in verify_result.split(conn_id)[1][:10]:
|
||||
print(f"✅ 连接验证通过")
|
||||
# 启动 Appium(如果未运行)
|
||||
start_appium()
|
||||
return conn_id
|
||||
else:
|
||||
print(f"⚠️ 连接验证失败,准备重新建立")
|
||||
|
||||
# ===== 获取USB连接的设备 =====
|
||||
devices_output = run_command("adb devices")
|
||||
lines = devices_output.splitlines()[1:]
|
||||
|
||||
|
||||
Binary file not shown.
@@ -1106,7 +1106,7 @@ class SectionMileageConfigPage:
|
||||
# if not self.click_system_back_button():
|
||||
# return False
|
||||
|
||||
self.logger.info("断面里程配置完成,正在执行测量")
|
||||
self.logger.info("断面里程配置完成,待执行测量")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user