# coding=utf-8 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 & 2: Finding input box and writing text...") # We can get a rough position first for logging, though perform_input_action is more robust screenshot_path = os.path.join(WxUtil.OUTPUT_DIR, "T5_Initial_Check.jpg") d.screenshot(screenshot_path) input_pos, _ = WxUtil.find_input_box_center(screenshot_path) logger.info(f"Initial target input position: {input_pos}") success = WxUtil.perform_input_action(d, input_pos, test_text, auto_send=True) if success: logger.info("✅ Step 3: Send action completed successfully.") else: logger.error("❌ Send action failed.") if __name__ == "__main__": main()