# coding=utf-8 """ T5_SendText_ActionTest.py - 文本输入与发送功能测试 (Input & Send Action Test) 【核心功能】 - 输入法切换:测试自动切换到微信输入模式逻辑。 - 鲁棒输入:验证通过 UI 元素(EditText)或坐标点进行文字填充。 - 发送动作:模拟点击“发送”按钮并验证发送是否成功。 【使用场景】 - 调试机器人“会写不会发”的问题。 - 验证在不同输入法环境下(如搜狗、系统默认)的输入兼容性。 - 测试输入框定位算法是否能避开键盘遮挡。 """ import os import sys import logging import time # Add project root 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 # Setup Logger logger = logging.getLogger("T5_SendText") # Configure Root Logger to ensure all logs go to file root_logger = logging.getLogger() root_logger.setLevel(logging.INFO) # Ensure Log Directory exists if hasattr(WxUtil, 'LOG_DIR'): log_dir = WxUtil.LOG_DIR else: log_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "Logs") if not os.path.exists(log_dir): os.makedirs(log_dir) # File Handler log_file = os.path.join(log_dir, "T5_SendText.log") fh = logging.FileHandler(log_file, mode='w', encoding='utf-8') formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) root_logger.addHandler(fh) # Console Handler if not any(isinstance(h, logging.StreamHandler) for h in root_logger.handlers): ch = logging.StreamHandler() ch.setFormatter(formatter) root_logger.addHandler(ch) def main(): logger.info("--- T5: Text Input and Send Test ---") # 1. Connect d = WxUtil.connect_device() if not d: logger.error("Device connection failed.") return # 2. Prepare text test_text = "我是AI返回的文本测试" logger.info(f"Test Content: {test_text}") # 3. Find Input Box and Send (Using perform_input_action which encapsulates the steps) # We pass auto_send=True to click the send button # The function will: # - Ensure keyboard mode # - Find input box (UI Tree -> Template -> Fallback) # - Input text # - Find and click Send button logger.info("Step 1: Finding input box and writing text...") # We pass debug_prefix="T5" to perform_input_action to get intermediate screenshots # The function will save: # - T5_1_check_mode.jpg # - T5_2_after_mode.jpg # - T5_3_input_box_found.jpg (if template matched) # - T5_4_after_input.jpg # - T5_5_after_send.jpg # Initial state screenshot initial_path = os.path.join(WxUtil.OUTPUT_DIR, "T5_0_Initial.jpg") d.screenshot(initial_path) logger.info(f"Initial state screenshot saved: {initial_path}") # Use a more robust way to find input_pos for logging input_pos, _ = WxUtil.find_input_box_center(initial_path) logger.info(f"Target input position (estimated): {input_pos}") success = WxUtil.perform_input_action(d, input_pos, test_text, auto_send=True, debug_prefix="T5") if success: logger.info("✅ Step 3: Send action completed successfully.") # Final state screenshot final_path = os.path.join(WxUtil.OUTPUT_DIR, "T5_6_Final.jpg") d.screenshot(final_path) logger.info(f"Final state screenshot saved: {final_path}") else: logger.error("❌ Send action failed.") if __name__ == "__main__": main()