# coding=utf-8 import uiautomator2 as u2 import time import os import sys import logging # 添加项目根目录到 sys.path 以便导入 WxUtil project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if project_root not in sys.path: sys.path.append(project_root) from WeiXin import WxUtil # 配置日志 LOG_FILE = os.path.join(WxUtil.LOG_DIR, "T6_SendAudio.log") logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler(LOG_FILE, encoding='utf-8'), logging.StreamHandler(sys.stdout) ] ) logger = logging.getLogger("T6_SendAudio") def test_send_audio_action(): logger.info("开始测试 T6: 语音发送流程压力测试 (包含多次模式切换)") # 1. 连接设备 d = WxUtil.connect_device() if not d: logger.error("无法连接设备,测试终止") return # 测试场景 1: 多次切换并最终发送语音 for i in range(1, 3): # 进行 2 轮切换测试 logger.info(f"\n--- 第 {i} 轮切换测试 ---") # 1.1 确保当前是键盘模式 logger.info("步骤 1: 确保切换到键盘模式...") WxUtil.switch_to_keyboard_mode(d) time.sleep(2) # 1.2 切换回语音模式并发送 logger.info("步骤 2: 切换回语音模式并尝试发送...") success = WxUtil.perform_voice_input(d, duration=2.0, debug_prefix=f"T6_Round{i}") if success: logger.info(f"第 {i} 轮测试成功。") else: logger.error(f"第 {i} 轮测试失败。") time.sleep(2) logger.info("\n--- 压力测试结束 ---") if __name__ == '__main__': # 确保目录存在 if not os.path.exists(WxUtil.LOG_DIR): os.makedirs(WxUtil.LOG_DIR) if not os.path.exists(WxUtil.OUTPUT_DIR): os.makedirs(WxUtil.OUTPUT_DIR) test_send_audio_action()