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
This commit is contained in:
+24
-86
@@ -1,10 +1,10 @@
|
||||
"""跨端 E2E 测试 fixture — 同时持有 Playwright (管理端) 和 Appium (司机安卓) 两个 driver。
|
||||
"""跨端 E2E fixtures — 同时持有 Playwright (管理端) 和 Appium (司机安卓) 两个 driver。
|
||||
|
||||
核心挑战:
|
||||
pytest 的单端 conftest 各自拥有 page / driver fixture,
|
||||
跨端测试需要同时注入两者而不冲突。
|
||||
解决方案: 使用显式命名的 fixture (admin_page / app_driver),
|
||||
各自独立创建/销毁生命周期。
|
||||
各自独立创建/销毁生命周期,内部统一复用 core 层工厂。
|
||||
|
||||
前置条件:
|
||||
- 管理端: python tests/web/admin/save_auth.py --role super_admin --browser chromium
|
||||
@@ -15,12 +15,18 @@
|
||||
pytest tests/cross_end/ -m cross_end -v
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
SCREENSHOTS_DIR = Path("output/screenshots")
|
||||
from tests.core.appium_support import (
|
||||
create_driver,
|
||||
make_ensure_logged_in,
|
||||
save_failure_screenshot,
|
||||
)
|
||||
from tests.core.playwright_support import (
|
||||
admin_browser_page,
|
||||
save_web_failure_screenshot,
|
||||
)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════
|
||||
# Admin Page (Playwright)
|
||||
@@ -30,51 +36,11 @@ SCREENSHOTS_DIR = Path("output/screenshots")
|
||||
def admin_page(admin_config, request):
|
||||
"""管理端 Playwright Page — 仅 chromium,加载 super_admin 登录态。
|
||||
|
||||
与 tests/web/admin/conftest.py 的 page fixture 核心逻辑一致,
|
||||
但不参数化浏览器 (跨端测试已经够慢,不必跑 browser matrix)。
|
||||
跨端测试已经够慢,不参数化浏览器矩阵。
|
||||
"""
|
||||
try:
|
||||
from playwright.sync_api import sync_playwright
|
||||
except ImportError:
|
||||
pytest.skip("Playwright 未安装,跳过跨端 E2E 测试。")
|
||||
|
||||
viewport = admin_config.get("viewport", {"width": 1920, "height": 1080})
|
||||
timeout = admin_config.get("timeout_seconds", 120) * 1000
|
||||
storage_state_dir = Path(admin_config.get("storage_state_dir", "tests/web/auth"))
|
||||
browser_name = "chromium"
|
||||
|
||||
with sync_playwright() as p:
|
||||
browser = p.chromium.launch(headless=True)
|
||||
|
||||
context_kwargs = {
|
||||
"viewport": {"width": viewport["width"], "height": viewport["height"]},
|
||||
"locale": "zh-CN",
|
||||
}
|
||||
|
||||
# 加载 super_admin 登录态
|
||||
auth_file = storage_state_dir / "super_admin_chromium.json"
|
||||
if auth_file.exists():
|
||||
context_kwargs["storage_state"] = str(auth_file)
|
||||
|
||||
context = browser.new_context(**context_kwargs)
|
||||
page = context.new_page()
|
||||
page.set_default_timeout(timeout)
|
||||
|
||||
with admin_browser_page(admin_config, browser_name="chromium", role="super_admin") as (page, _):
|
||||
yield page
|
||||
|
||||
# 失败自动截图
|
||||
if hasattr(request.node, "rep_call") and request.node.rep_call.failed:
|
||||
scenario_dir = SCREENSHOTS_DIR / request.node.name
|
||||
scenario_dir.mkdir(parents=True, exist_ok=True)
|
||||
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
path = str(scenario_dir / f"FAIL_admin_chromium_{ts}.png")
|
||||
try:
|
||||
page.screenshot(path=path, full_page=True)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
context.close()
|
||||
browser.close()
|
||||
save_web_failure_screenshot(page, request, tag="admin_chromium")
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════
|
||||
@@ -83,15 +49,7 @@ def admin_page(admin_config, request):
|
||||
|
||||
@pytest.fixture
|
||||
def app_driver(driver_android_config, request):
|
||||
"""司机安卓端 Appium driver。
|
||||
|
||||
复用 tests.app._shared.create_driver() 和 save_failure_screenshot()。
|
||||
"""
|
||||
try:
|
||||
from tests.app._shared import create_driver, save_failure_screenshot
|
||||
except ImportError:
|
||||
pytest.skip("Appium-Python-Client 未安装,跳过跨端 E2E 测试。")
|
||||
|
||||
"""司机安卓端 Appium driver。"""
|
||||
cfg = driver_android_config
|
||||
d = None
|
||||
try:
|
||||
@@ -112,11 +70,7 @@ def app_driver(driver_android_config, request):
|
||||
|
||||
@pytest.fixture
|
||||
def ensure_admin_login(admin_config, admin_page, config):
|
||||
"""确保管理端已登录为 super_admin。
|
||||
|
||||
复用 tests/web/admin/conftest.py 的登录态验证逻辑,
|
||||
但硬编码 role="super_admin" (跨端流程始终用超管)。
|
||||
"""
|
||||
"""确保管理端已登录为 super_admin(跨端流程始终用超管)。"""
|
||||
|
||||
def _ensure_login(role: str = "super_admin") -> None:
|
||||
account = config["accounts"].get(role)
|
||||
@@ -136,27 +90,22 @@ def ensure_admin_login(admin_config, admin_page, config):
|
||||
)
|
||||
|
||||
# skip_login: false — 实时 UI 登录
|
||||
from tests.pages.admin.login_page import LoginPage
|
||||
|
||||
admin_page.goto(f"{base_url}/#/login")
|
||||
admin_page.get_by_placeholder("请输入用户名").fill(account["phone"])
|
||||
admin_page.get_by_placeholder("请输入密码").fill(account["password"])
|
||||
admin_page.get_by_role("button", name="登录").click()
|
||||
admin_page.wait_for_timeout(3000)
|
||||
if "#/login" in admin_page.url or "/login" in admin_page.url:
|
||||
try:
|
||||
LoginPage(admin_page).login(account["phone"], account["password"])
|
||||
except AssertionError as e:
|
||||
raise RuntimeError(
|
||||
"登录失败!可能存在图形验证码。请改为手动登录: python tests/web/admin/save_auth.py"
|
||||
)
|
||||
) from e
|
||||
|
||||
return _ensure_login
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ensure_driver_login(driver_android_config, config):
|
||||
"""确保司机安卓端已登录。
|
||||
|
||||
复用 tests.app._shared.make_ensure_logged_in() 工厂。
|
||||
"""
|
||||
from tests.app._shared import make_ensure_logged_in
|
||||
|
||||
"""确保司机安卓端已登录。"""
|
||||
fn = make_ensure_logged_in(driver_android_config, config, account_role="driver")
|
||||
return lambda d: fn(d)
|
||||
|
||||
@@ -184,14 +133,3 @@ def cross_end_context() -> dict:
|
||||
"accounting_done": False,
|
||||
"review_done": False,
|
||||
}
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════
|
||||
# Report hook — 让 request.node.rep_call.failed 可用于失败截图
|
||||
# ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
|
||||
def pytest_runtest_makereport(item, call):
|
||||
outcome = yield
|
||||
rep = outcome.get_result()
|
||||
setattr(item, f"rep_{rep.when}", rep)
|
||||
|
||||
Reference in New Issue
Block a user