08ea5fbe98
添加用户管理视图、API和状态管理文件
194 lines
5.5 KiB
Python
194 lines
5.5 KiB
Python
"""
|
|
配置管理器模块
|
|
|
|
提供统一的测试环境配置管理,支持多环境配置。
|
|
"""
|
|
|
|
import os
|
|
import yaml
|
|
from typing import Dict, Any, Optional
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
|
|
|
|
@dataclass
|
|
class TimeoutConfig:
|
|
"""超时配置"""
|
|
default: int = 30000
|
|
navigation: int = 30000
|
|
element: int = 10000
|
|
network: int = 30000
|
|
|
|
|
|
@dataclass
|
|
class ScreenshotConfig:
|
|
"""截图配置"""
|
|
enabled: bool = True
|
|
on_failure: bool = True
|
|
path: str = "reports/screenshots"
|
|
|
|
|
|
@dataclass
|
|
class VideoConfig:
|
|
"""录像配置"""
|
|
enabled: bool = False
|
|
on_failure: bool = True
|
|
path: str = "reports/videos"
|
|
|
|
|
|
@dataclass
|
|
class TraceConfig:
|
|
"""追踪配置"""
|
|
enabled: bool = False
|
|
on_failure: bool = True
|
|
path: str = "reports/traces"
|
|
|
|
|
|
@dataclass
|
|
class BrowserConfig:
|
|
"""浏览器配置"""
|
|
name: str = "chromium"
|
|
headless: bool = False
|
|
viewport_width: int = 1920
|
|
viewport_height: int = 1080
|
|
slow_mo: int = 0
|
|
|
|
|
|
@dataclass
|
|
class TestConfig:
|
|
"""测试配置"""
|
|
# 环境信息
|
|
env: str = "dev"
|
|
base_url: str = ""
|
|
api_url: str = ""
|
|
|
|
# 超时配置
|
|
timeout: TimeoutConfig = field(default_factory=TimeoutConfig)
|
|
|
|
# 截图配置
|
|
screenshot: ScreenshotConfig = field(default_factory=ScreenshotConfig)
|
|
|
|
# 录像配置
|
|
video: VideoConfig = field(default_factory=VideoConfig)
|
|
|
|
# 追踪配置
|
|
trace: TraceConfig = field(default_factory=TraceConfig)
|
|
|
|
# 浏览器配置
|
|
browser: BrowserConfig = field(default_factory=BrowserConfig)
|
|
|
|
# 重试配置
|
|
retries: int = 2
|
|
|
|
# 并行配置
|
|
workers: int = 1
|
|
parallel: bool = False
|
|
|
|
# 测试数据
|
|
test_data: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
# 用户数据
|
|
users: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
class ConfigManager:
|
|
"""配置管理器"""
|
|
|
|
_instance: Optional["ConfigManager"] = None
|
|
_config: Optional[TestConfig] = None
|
|
|
|
def __new__(cls) -> "ConfigManager":
|
|
if cls._instance is None:
|
|
cls._instance = super().__new__(cls)
|
|
return cls._instance
|
|
|
|
def __init__(self):
|
|
if self._config is None:
|
|
self._config = self._load_config()
|
|
|
|
def _load_config(self) -> TestConfig:
|
|
"""加载配置"""
|
|
# 获取环境变量
|
|
env = os.getenv("TEST_ENV", "dev")
|
|
|
|
# 配置文件路径
|
|
config_path = Path(__file__).parent.parent / "config" / "config.yaml"
|
|
|
|
# 默认配置
|
|
config = TestConfig(env=env)
|
|
|
|
# 从文件加载配置
|
|
if config_path.exists():
|
|
with open(config_path, "r", encoding="utf-8") as f:
|
|
data = yaml.safe_load(f)
|
|
|
|
if data and "environments" in data:
|
|
env_config = data["environments"].get(env, {})
|
|
|
|
# 基础配置 - 支持嵌套的admin/uniapp配置
|
|
if "admin" in env_config:
|
|
config.base_url = env_config["admin"].get("base_url", "")
|
|
config.api_url = env_config["admin"].get("api_url", "")
|
|
else:
|
|
config.base_url = env_config.get("base_url", "")
|
|
config.api_url = env_config.get("api_url", "")
|
|
|
|
# 超时配置
|
|
if "timeout" in env_config:
|
|
timeout = env_config["timeout"]
|
|
config.timeout = TimeoutConfig(
|
|
default=timeout.get("default", 30000),
|
|
navigation=timeout.get("navigation", 30000),
|
|
element=timeout.get("element", 10000),
|
|
network=timeout.get("network", 30000),
|
|
)
|
|
|
|
# 浏览器配置
|
|
if "browser" in env_config:
|
|
browser = env_config["browser"]
|
|
config.browser = BrowserConfig(
|
|
name=browser.get("name", "chromium"),
|
|
headless=browser.get("headless", False),
|
|
viewport_width=browser.get("viewport_width", 1920),
|
|
viewport_height=browser.get("viewport_height", 1080),
|
|
slow_mo=browser.get("slow_mo", 0),
|
|
)
|
|
|
|
# 用户数据
|
|
if "users" in data:
|
|
config.users = data["users"]
|
|
|
|
# 测试数据
|
|
if "test_data" in data:
|
|
config.test_data = data["test_data"]
|
|
|
|
# 从环境变量覆盖配置
|
|
config.base_url = os.getenv("TEST_BASE_URL", config.base_url)
|
|
config.api_url = os.getenv("TEST_API_URL", config.api_url)
|
|
config.browser.headless = os.getenv("TEST_HEADLESS", "false").lower() == "true"
|
|
|
|
return config
|
|
|
|
@property
|
|
def config(self) -> TestConfig:
|
|
"""获取配置"""
|
|
return self._config
|
|
|
|
def get_config(self) -> TestConfig:
|
|
"""获取配置(兼容方法)"""
|
|
return self._config
|
|
|
|
def reload(self) -> None:
|
|
"""重新加载配置"""
|
|
self._config = self._load_config()
|
|
|
|
def update_config(self, **kwargs) -> None:
|
|
"""更新配置"""
|
|
for key, value in kwargs.items():
|
|
if hasattr(self._config, key):
|
|
setattr(self._config, key, value)
|
|
|
|
|
|
# 全局配置管理器实例
|
|
config_manager = ConfigManager()
|