Files
web-capture-api/test_api.py
2026-07-07 13:23:36 +08:00

122 lines
2.9 KiB
Python
Executable File
Raw Permalink 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.
#!/usr/bin/env python3
"""
测试脚本 - 测试 Web Capture API
"""
import requests
import json
import sys
API_URL = "http://localhost:16025"
def test_health():
"""测试健康检查"""
print("\n🔍 测试健康检查...")
resp = requests.get(f"{API_URL}/health")
print(f"状态: {resp.status_code}")
print(f"响应: {json.dumps(resp.json(), indent=2, ensure_ascii=False)}")
return resp.status_code == 200
def test_screenshot():
"""测试截图功能"""
print("\n📸 测试截图功能...")
data = {
"url": "https://example.com",
"action": "screenshot",
"wait_time": 2000
}
resp = requests.post(
f"{API_URL}/api/capture",
json=data
)
if resp.status_code == 200:
with open("test_screenshot.png", "wb") as f:
f.write(resp.content)
print("✅ 截图成功,已保存为 test_screenshot.png")
return True
else:
print(f"❌ 截图失败: {resp.text}")
return False
def test_html():
"""测试HTML提取"""
print("\n📄 测试HTML提取...")
data = {
"url": "https://example.com",
"action": "html"
}
resp = requests.post(
f"{API_URL}/api/capture",
json=data
)
if resp.status_code == 200:
result = resp.json()
print(f"✅ HTML提取成功长度: {len(result.get('html', ''))}")
print(f"前200字符: {result.get('html', '')[:200]}...")
return True
else:
print(f"❌ HTML提取失败: {resp.text}")
return False
def test_scroll():
"""测试滚动加载"""
print("\n🔄 测试滚动加载...")
data = {
"url": "https://news.ycombinator.com",
"action": "screenshot",
"scroll_times": 3,
"scroll_delay": 1000,
"full_page": True
}
resp = requests.post(
f"{API_URL}/api/capture",
json=data
)
if resp.status_code == 200:
with open("test_scroll.png", "wb") as f:
f.write(resp.content)
print("✅ 滚动截图成功,已保存为 test_scroll.png")
return True
else:
print(f"❌ 滚动截图失败: {resp.text}")
return False
if __name__ == "__main__":
print("🚀 Web Capture API 测试")
print("=" * 50)
tests = [
("健康检查", test_health),
("截图功能", test_screenshot),
("HTML提取", test_html),
("滚动加载", test_scroll)
]
results = []
for name, test_func in tests:
try:
result = test_func()
results.append((name, result))
except Exception as e:
print(f"{name} 异常: {e}")
results.append((name, False))
print("\n" + "=" * 50)
print("测试结果汇总:")
for name, result in results:
status = "✅ 通过" if result else "❌ 失败"
print(f" {name}: {status}")