Files
2026Technology-Competition/tests/test_web_e2e.py
T
hangshuo652 21040bfcc2 fix: 修复测试导入错误、playwright配置、pytest配置
- 修复orchestrator.py check_coverage导入路径
- 修复test_golden.py/test_e2e.py/test_design.py导入错误
- 删除过时test_preprocessor.py
- 修复test_confidence.py compare_coverage导入路径
- 修复pytest模块命名冲突(hina/e2e添加__init__.py)
- 配置pytest.ini跳过e2e目录(asyncio冲突)
- 修复playwright测试使用firefox浏览器
- 修复playwright测试expect导入
- 添加skip标记(playwright/外部数据依赖)
- 更新pyproject.toml setuptools配置
- 更新README.md/AGENTS.md/test-report.md文档
2026-08-31 21:33:32 +08:00

152 lines
5.2 KiB
Python

"""
Playwright E2E tests for COBOL-Java Migration Platform Web UI.
Server must be running: python -m uvicorn web.api:app --host 127.0.0.1 --port $TEST_PORT
(端口经 TEST_PORT 环境变量配置, 默认 8000)
Run standalone: python -m pytest tests/test_web_e2e.py -v
"""
from __future__ import annotations
import os
import pytest
try:
from playwright.sync_api import Page, expect, sync_playwright
HAS_PLAYWRIGHT = True
except ImportError:
HAS_PLAYWRIGHT = False
TEST_PORT = int(os.environ.get("TEST_PORT", "8000"))
BASE_URL = f"http://127.0.0.1:{TEST_PORT}"
@pytest.fixture(scope="module")
def browser(request):
if not HAS_PLAYWRIGHT:
pytest.skip("playwright not installed")
try:
import asyncio
loop = asyncio.get_event_loop()
if loop.is_running():
pytest.skip("sync_playwright cannot run inside a running asyncio loop")
except (RuntimeError, AttributeError):
pass
with sync_playwright() as p:
browser = p.firefox.launch(headless=True)
yield browser
browser.close()
@pytest.fixture
def page(browser):
page = browser.new_page()
yield page
page.close()
def test_upload_page_loads(page: Page):
"""验证上传页面正常加载"""
page.goto(BASE_URL)
expect(page).to_have_title("COBOL → Java Migration Verification")
# 标题包含 verify 文字
expect(page.locator("h1")).to_contain_text("verify")
# 表单存在
form = page.locator("#verify-form")
expect(form).to_be_visible()
def test_form_elements_present(page: Page):
"""验证所有表单元素存在"""
page.goto(BASE_URL)
# 4 个文件输入
expect(page.locator("input[name=copybook]")).to_be_visible()
expect(page.locator("input[name=cobol_src]")).to_be_visible()
expect(page.locator("input[name=java_src]")).to_be_visible()
expect(page.locator("input[name=mapping]")).to_be_visible()
# Runner 下拉框
expect(page.locator("select[name=runner]")).to_be_visible()
expect(page.locator("select[name=runner]")).to_have_value("native")
# 提交按钮
expect(page.locator("button[type=submit]")).to_be_visible()
expect(page.locator("button[type=submit]")).to_contain_text("verify")
def test_submit_empty_form(page: Page):
"""验证空表单提交返回 422 (缺少必填字段)"""
page.goto(BASE_URL)
js = """
(async () => {
const fd = new FormData();
const r = await fetch('__BASE_URL__/verify', { method: 'POST', body: fd });
return r.status;
})()
""".replace("__BASE_URL__", BASE_URL)
result = page.evaluate(js)
assert result == 422
def test_submit_with_files(page: Page):
"""验证上传测试文件后表单正常响应"""
page.goto(BASE_URL)
page.set_input_files("input[name=copybook]",
"tests/fixtures/simple.cpy")
page.set_input_files("input[name=cobol_src]",
"tests/fixtures/simple.cbl")
page.set_input_files("input[name=mapping]",
"tests/fixtures/simple.yaml")
# 用 evaluate 直接调 API 绕过 webkitdirectory 限制
js = """
(async () => {
const fd = new FormData();
fd.append('copybook', new Blob(['test'], {type:'text/plain'}), 'test.cpy');
fd.append('cobol_src', new Blob(['test'], {type:'text/plain'}), 'test.cbl');
fd.append('java_src', new Blob(['test'], {type:'text/plain'}), 'test.java');
fd.append('mapping', new Blob(['test'], {type:'text/plain'}), 'test.yaml');
fd.append('runner', 'native');
const r = await fetch('__BASE_URL__/verify', { method: 'POST', body: fd });
return { status: r.status, body: await r.json() };
})()
""".replace("__BASE_URL__", BASE_URL)
result = page.evaluate(js)
assert result["status"] == 202
assert "task_id" in result["body"]
def test_runner_selector_options(page: Page):
"""验证 Runner 下拉框有两个选项"""
page.goto(BASE_URL)
expect(page.locator("select[name=runner]")).to_be_visible()
count = page.locator("select[name=runner] option").count()
assert count == 2
native_val = page.locator("select[name=runner] option").nth(0).get_attribute("value")
spark_val = page.locator("select[name=runner] option").nth(1).get_attribute("value")
assert native_val == "native"
assert spark_val == "spark"
def test_status_endpoint(page: Page):
"""验证 /status/ 端点返回 JSON"""
page.goto(f"{BASE_URL}/status/nonexistent")
body = page.locator("body").inner_text()
assert "404" in body or "not found" in body.lower()
def test_result_endpoint_404(page: Page):
"""验证 /result/ 端点对不存在任务返回 404"""
page.goto(f"{BASE_URL}/result/nonexistent")
body = page.locator("body").inner_text()
assert "404" in body or "not found" in body.lower()
def test_dark_theme_rendered(page: Page):
"""验证 Terminal Dark 主题渲染"""
page.goto(BASE_URL)
expect(page.locator(".badge")).to_be_visible()
expect(page.locator("footer")).to_be_visible()
def test_page_title(page: Page):
"""验证页面标题"""
page.goto(BASE_URL)
expect(page).to_have_title("COBOL → Java Migration Verification")