import pytest
import os
import sys
from typing import Generator
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from api_test_framework import CrawlrsAPIClient
@pytest.fixture(scope="session", autouse=True)
def geo_restrictions_disabled(api_client) -> Generator[None, None, None]:
result = api_client.update_team_geo_restrictions({"enable_geo_restrictions": False})
if not result.success:
print(
f"\n[conftest] 警告: 关闭地理限制未成功 "
f"(status={result.response.status_code}),真实爬取用例可能受 geo 校验影响"
)
yield
@pytest.fixture(scope="session")
def api_client() -> Generator[CrawlrsAPIClient, None, None]:
base_url = os.getenv("CRAWLRS_API_URL", "http://localhost:8899")
api_key = os.getenv("CRAWLRS_API_KEY", os.getenv("TEST_API_KEY", "test-api-key"))
client = CrawlrsAPIClient(base_url=base_url, api_key=api_key)
yield client
report_path = os.getenv("TEST_REPORT_PATH", "test-results/report.json")
os.makedirs(os.path.dirname(report_path), exist_ok=True)
client.generate_report(report_path)
@pytest.fixture
def search_test_params():
return {"query": "test query", "engines": ["bing"], "limit": 5}
@pytest.fixture
def crawl_test_params():
return {
"url": "https://text.npr.org",
"options": {"timeout": 30000, "extract_text": True},
}
@pytest.fixture
def scrape_test_params():
return {
"url": "https://text.npr.org",
"options": {"wait_for": 1000, "timeout": 30},
}
@pytest.fixture
def extract_test_params():
return {
"url": "https://text.npr.org",
"selectors": {"title": "h1", "paragraphs": "p"},
}
@pytest.fixture
def webhook_test_params():
return {
"url": "https://httpbin.org/post",
"events": ["task.completed", "task.failed"],
"secret": os.getenv("TEST_WEBHOOK_SECRET", "test-secret"),
}