Files
Yb-QaAutomationHub/tests/web/admin/conftest.py
T
xst 18890c0437 refactor(tests): restructure into layered POM architecture with Allure reporting
- Split framework into core/ (config, logger, db, driver factories, screenshot,
  allure integration) and pages/ (PO layer: base/admin/driver); flatten test
  files per platform (web/admin, app/driver_android, cross_end)
- Slim root conftest.py 656 -> ~100 lines: drop inline CN HTML report, add
  Allure severity mapping (p0/p1/p2 -> blocker/normal/minor) and
  environment.properties; support both tests.*-prefixed and bare imports
- Preserve TC-D2S-001/002 cross-end E2E cases with business logic unchanged,
  add allure epic/feature/story/step annotations
- Deduplicate: single makereport hook, shared failure-screenshot helpers,
  single SCREENSHOTS_DIR constant; deterministic auth-state role loading
- Move DB credentials from db_helper.py into test_config.yaml (database section)
- Move pytest.ini to repo root; switch reporting to allure-pytest
  (drop pytest-html/junitxml); update requirements.txt accordingly
- Delete deprecated scaffolding: test_freight_full_flow + 6 stale page
  objects, iOS/shipper empty shells, dead utils
- Fix flaky vxe-table action-row wait: wait for a visible td instead of
  hidden placeholder rows (fixed shipper-payment timeouts)
- Verified end-to-end against live env: both E2E cases pass (2 passed 3m12s);
  run artifacts (logs/screenshots/allure) included per repo convention
2026-08-17 14:29:52 +08:00

89 lines
3.2 KiB
Python

"""管理端 fixtures — Playwright 浏览器实例 + 登录态复用。
核心机制:
skip_login: true → 加载 storage_state,跳过验证码
skip_login: false → 执行完整 UI 登录
初始化:
python tests/web/admin/save_auth.py
"""
from pathlib import Path
import pytest
from playwright.sync_api import Page
from tests.core.playwright_support import (
admin_browser_page,
pick_available_role,
save_web_failure_screenshot,
)
@pytest.fixture(params=["chromium", "firefox"])
def browser_name(admin_config, request):
"""每个浏览器执行一轮。"""
browsers = admin_config.get("browsers", ["chromium", "firefox"])
if request.param not in browsers:
pytest.skip(f"浏览器 {request.param} 未在配置 browsers 中启用")
return request.param
@pytest.fixture
def page(admin_config, browser_name, request) -> Page:
"""Playwright Page,加载可用登录态,失败自动截图。"""
storage_state_dir = Path(admin_config.get("storage_state_dir", "tests/web/auth"))
skip_login = admin_config.get("skip_login", True)
role = pick_available_role(storage_state_dir, browser_name) if skip_login else None
with admin_browser_page(admin_config, browser_name, role=role) as (p, loaded_role):
p._auth_role = loaded_role
yield p
save_web_failure_screenshot(p, request, tag=browser_name)
@pytest.fixture
def ensure_login(admin_config, page, config):
"""确保管理端已登录。"""
def _ensure_login(role: str = "super_admin") -> None:
account = config["accounts"].get(role)
if not account:
raise ValueError(f"未知角色 '{role}',可用: {list(config['accounts'].keys())}")
base_url = admin_config["base_url"]
skip_login = admin_config.get("skip_login", True)
if skip_login:
auth_role = getattr(page, "_auth_role", None)
if auth_role == role:
page.goto(base_url)
page.wait_for_timeout(2000)
if "#/login" not in page.url and "/login" not in page.url:
return
raise RuntimeError(
f"登录态已过期!请运行: python tests/web/admin/save_auth.py --role {role}"
)
if auth_role and auth_role != role:
raise RuntimeError(
f"当前 storage_state 角色为 '{auth_role}',请求 '{role}'。\n"
f"请运行: python tests/web/admin/save_auth.py --role {role}"
)
raise RuntimeError(
f"未找到角色 '{role}' 的登录态文件!请运行:\n"
f" python tests/web/admin/save_auth.py --role {role}"
)
# skip_login: false — 实时 UI 登录
from tests.pages.admin.login_page import LoginPage
page.goto(f"{base_url}/#/login")
try:
LoginPage(page).login(account["phone"], account["password"])
except AssertionError as e:
raise RuntimeError(
"登录失败!可能存在图形验证码。请改为手动登录: python tests/web/admin/save_auth.py"
) from e
return _ensure_login