125 lines
4.6 KiB
Python
125 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
||
"""真实浏览器验收:设备自适应登录、Web/H5 菜单、帮助中心入口。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import shutil
|
||
import subprocess
|
||
import tempfile
|
||
import time
|
||
import urllib.request
|
||
from pathlib import Path
|
||
|
||
from capture_help import CDP
|
||
|
||
|
||
BASE = "http://127.0.0.1:8000"
|
||
CHROME = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
|
||
PORT = 9231
|
||
DESKTOP_UA = (
|
||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
|
||
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0 Safari/537.36"
|
||
)
|
||
MOBILE_UA = (
|
||
"Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) "
|
||
"AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1"
|
||
)
|
||
|
||
|
||
def wait_page() -> str:
|
||
deadline = time.time() + 20
|
||
while time.time() < deadline:
|
||
try:
|
||
with urllib.request.urlopen(f"http://127.0.0.1:{PORT}/json/list", timeout=1) as resp:
|
||
target = next(x for x in json.load(resp) if x.get("type") == "page")
|
||
return target["webSocketDebuggerUrl"]
|
||
except Exception:
|
||
time.sleep(0.2)
|
||
raise RuntimeError("Chrome 调试端口未就绪")
|
||
|
||
|
||
def wait_path(cdp: CDP, path: str) -> None:
|
||
deadline = time.time() + 20
|
||
while time.time() < deadline:
|
||
if cdp.eval("location.pathname") == path:
|
||
time.sleep(0.8)
|
||
return
|
||
time.sleep(0.25)
|
||
raise AssertionError(f"登录后路径应为 {path},实际为 {cdp.eval('location.pathname')}")
|
||
|
||
|
||
def fill_and_login(cdp: CDP, username: str, password: str, mobile: bool) -> str:
|
||
width, height, scale = (390, 844, 2) if mobile else (1440, 960, 1)
|
||
cdp.call("Emulation.setDeviceMetricsOverride", {
|
||
"width": width, "height": height, "deviceScaleFactor": scale, "mobile": mobile,
|
||
})
|
||
cdp.call("Emulation.setUserAgentOverride", {"userAgent": MOBILE_UA if mobile else DESKTOP_UA})
|
||
cdp.navigate("/login", "登录工作台")
|
||
cdp.eval("localStorage.clear()")
|
||
cdp.navigate("/login", "登录工作台")
|
||
cdp.eval(
|
||
f"""(() => {{
|
||
const inputs = document.querySelectorAll('input');
|
||
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set;
|
||
setter.call(inputs[0], {json.dumps(username)});
|
||
inputs[0].dispatchEvent(new Event('input', {{bubbles: true}}));
|
||
setter.call(inputs[1], {json.dumps(password)});
|
||
inputs[1].dispatchEvent(new Event('input', {{bubbles: true}}));
|
||
const button = [...document.querySelectorAll('button')]
|
||
.find(x => x.innerText.replace(/\\s/g, '').includes('登录'));
|
||
button.click();
|
||
return true;
|
||
}})()"""
|
||
)
|
||
return "/m" if mobile else "/dashboard"
|
||
|
||
|
||
def main() -> None:
|
||
profile = Path(tempfile.mkdtemp(prefix="unis-hrms-smoke-"))
|
||
chrome = subprocess.Popen(
|
||
[
|
||
CHROME, "--headless=new", f"--remote-debugging-port={PORT}",
|
||
f"--user-data-dir={profile}", "--no-first-run", "--no-default-browser-check",
|
||
"--disable-gpu", "about:blank",
|
||
],
|
||
stdout=subprocess.DEVNULL,
|
||
stderr=subprocess.DEVNULL,
|
||
)
|
||
try:
|
||
cdp = CDP(wait_page())
|
||
cdp.call("Page.enable")
|
||
cdp.call("Runtime.enable")
|
||
|
||
target = fill_and_login(cdp, "emp", "emp123456", mobile=False)
|
||
wait_path(cdp, target)
|
||
desktop_menus = cdp.eval(
|
||
"[...document.querySelectorAll('.admin-sider .ant-menu-title-content')].map(x => x.innerText.trim())"
|
||
)
|
||
assert desktop_menus == ["首页", "请假", "工资", "福利", "我的"], desktop_menus
|
||
cdp.call("Page.navigate", {"url": f"{BASE}/help"})
|
||
deadline = time.time() + 20
|
||
while time.time() < deadline and not cdp.eval("!!document.querySelector('iframe')"):
|
||
time.sleep(0.25)
|
||
assert cdp.eval("document.querySelector('iframe')?.getAttribute('src')") == "/help/index.html"
|
||
|
||
target = fill_and_login(cdp, "emp", "emp123456", mobile=True)
|
||
wait_path(cdp, target)
|
||
mobile_menus = cdp.eval(
|
||
"[...document.querySelectorAll('.m-tabbar .adm-tab-bar-item-title')].map(x => x.innerText.trim())"
|
||
)
|
||
assert mobile_menus == ["首页", "请假", "工资", "福利", "我的"], mobile_menus
|
||
print("browser smoke passed: desktop /dashboard, mobile /m, menus and help center")
|
||
cdp.ws.close()
|
||
finally:
|
||
chrome.terminate()
|
||
try:
|
||
chrome.wait(timeout=5)
|
||
except subprocess.TimeoutExpired:
|
||
chrome.kill()
|
||
shutil.rmtree(profile, ignore_errors=True)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|