#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试截图功能的脚本 验证wkhtmltoimage安装和截图API功能 """ import os import sys import requests import time def test_wkhtmltoimage(): """测试wkhtmltoimage命令行工具""" print("--- 测试wkhtmltoimage命令行工具 ---") try: import subprocess result = subprocess.run(["wkhtmltoimage", "--version"], capture_output=True, text=True, timeout=5) if result.returncode == 0: print(f"[OK] wkhtmltoimage已安装: {result.stdout.strip()}") return True else: print("[FAIL] wkhtmltoimage命令执行失败") return False except Exception as e: print(f"[FAIL] 测试wkhtmltoimage失败: {e}") return False def test_direct_screenshot(): """测试直接截图功能""" print("\n--- 测试直接截图功能 ---") try: import subprocess # 创建截图目录 os.makedirs("screenshots", exist_ok=True) # 截图本地应用 cmd = [ "wkhtmltoimage", "--width", "1920", "--height", "1080", "--quality", "95", "--js-delay", "3000", "http://127.0.0.1:51233", "screenshots/test_direct.png", ] result = subprocess.run(cmd, capture_output=True, text=True, timeout=30) if result.returncode == 0: if os.path.exists("screenshots/test_direct.png"): file_size = os.path.getsize("screenshots/test_direct.png") print(f"[OK] 直接截图成功: screenshots/test_direct.png ({file_size} bytes)") return True else: print("[FAIL] 截图文件未生成") return False else: print(f"[FAIL] 直接截图失败: {result.stderr}") return False except Exception as e: print(f"[FAIL] 直接截图测试失败: {e}") return False def test_api_screenshot(): """测试API截图功能""" print("\n--- 测试API截图功能 ---") # 检查应用是否运行 try: response = requests.get("http://127.0.0.1:51233/health", timeout=5) if response.status_code == 200: print("[OK] 应用正在运行") else: print(f"[FAIL] 应用响应异常: {response.status_code}") return False except Exception as e: print(f"[FAIL] 应用连接失败: {e}") return False # 尝试访问截图相关的API api_endpoints = ["/api/screenshots", "/yuyx/api/browser_pool/stats", "/yuyx/api/screenshots"] for endpoint in api_endpoints: try: response = requests.get(f"http://127.0.0.1:51233{endpoint}", timeout=5) print(f"API {endpoint}: {response.status_code}") if response.status_code == 401: print(f" [WARN] 需要认证 - 这是正常的") elif response.status_code == 404: print(f" [WARN] 端点不存在 - 需要检查路由配置") elif response.status_code == 200: print(f" [OK] API正常工作") except Exception as e: print(f" [FAIL] API调用失败: {e}") return True def check_logs(): """检查应用日志中的截图相关信息""" print("\n--- 检查应用日志 ---") log_file = "app_new.log" if os.path.exists(log_file): print(f"[OK] 发现应用日志: {log_file}") try: with open(log_file, "r", encoding="utf-8", errors="ignore") as f: lines = f.readlines() # 查找截图相关的日志 screenshot_lines = [] for i, line in enumerate(lines[-20:]): # 最后20行 if any(keyword in line.lower() for keyword in ["截图", "screenshot", "wkhtmltoimage"]): screenshot_lines.append(f"第{len(lines) - 20 + i + 1}行: {line.strip()}") if screenshot_lines: print("发现截图相关日志:") for line in screenshot_lines: print(f" {line}") else: print("未发现截图相关日志") except Exception as e: print(f"读取日志失败: {e}") else: print(f"[FAIL] 未找到应用日志: {log_file}") def main(): print("[TEST] 截图功能测试工具") print("=" * 50) # 测试wkhtmltoimage wkhtmltoimage_ok = test_wkhtmltoimage() # 测试直接截图 if wkhtmltoimage_ok: direct_ok = test_direct_screenshot() else: direct_ok = False # 测试API api_ok = test_api_screenshot() # 检查日志 check_logs() # 总结 print("\n" + "=" * 50) print("[STATS] 测试结果总结:") print(f" wkhtmltoimage: {'[OK]' if wkhtmltoimage_ok else '[FAIL]'}") print(f" 直接截图: {'[OK]' if direct_ok else '[FAIL]'}") print(f" API连接: {'[OK]' if api_ok else '[FAIL]'}") if wkhtmltoimage_ok and direct_ok: print("\n[SUCCESS] 截图功能基础测试通过!") print("现在可以测试Web界面的截图功能了。") print("\n下一步:") print("1. 访问 http://127.0.0.1:51233/yuyx 登录管理员后台") print("2. 使用admin/admin123登录") print("3. 找到截图功能进行测试") else: print("\n[WARN] 截图功能存在问题,需要进一步调试") if __name__ == "__main__": main()