初始化项目: Web Capture API v1.0.0
功能: - 网页截图(全页或视口) - HTML代码提取 - 自动滚动加载动态内容 - 自定义视口大小 - Web界面和RESTful API - 支持两种后端: agent-browser 和 Playwright - Docker部署支持 包含: - Flask REST API - 前端Web界面 - 测试脚本 - 安装脚本 - Dockerfile和docker-compose - systemd服务配置
This commit is contained in:
122
test_api.py
Executable file
122
test_api.py
Executable file
@@ -0,0 +1,122 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
测试脚本 - 测试 Web Capture API
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
import sys
|
||||
|
||||
API_URL = "http://localhost:5000"
|
||||
|
||||
|
||||
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}")
|
||||
Reference in New Issue
Block a user