Files
aiData/WeiXin/T5_SendText_ActionTest.py
HuangHai 54f1b2fb91 'commit'
2026-01-31 07:40:07 +08:00

94 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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 & 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()