101 lines
3.7 KiB
Python
101 lines
3.7 KiB
Python
"""
|
|
scripts/rpa/dsv_login_smoke.py
|
|
|
|
用途:本地手动验证 DSV 360 自动登录(含密码方式第二步、首页 ready)。
|
|
依赖:Playwright、SHIPPING_NOTICE_RPA_WORKER_DSV_USERNAME / SHIPPING_NOTICE_RPA_WORKER_DSV_PASSWORD。
|
|
逻辑:打开可见浏览器 → _ensure_dsv_session → 保持窗口供人工观察,按 Enter 关闭。
|
|
|
|
用法(仓库根目录):
|
|
$env:SHIPPING_NOTICE_RPA_WORKER_HEADLESS = "false"
|
|
backend\\venv\\Scripts\\python.exe scripts\\rpa\\dsv_login_smoke.py
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import traceback
|
|
from pathlib import Path
|
|
|
|
|
|
def _load_worker():
|
|
repo_root = Path(__file__).resolve().parents[2]
|
|
module_path = repo_root / "scripts" / "rpa" / "freight_rpa_worker.py"
|
|
spec = importlib.util.spec_from_file_location("freight_rpa_worker_smoke", module_path)
|
|
assert spec and spec.loader
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
def main() -> int:
|
|
os.environ.setdefault("SHIPPING_NOTICE_RPA_WORKER_HEADLESS", "false")
|
|
os.environ.setdefault("SHIPPING_NOTICE_RPA_WORKER_MOCK", "false")
|
|
os.environ.setdefault(
|
|
"SHIPPING_NOTICE_RPA_WORKER_BROWSER_PROFILE_DIR",
|
|
"backend/data/shipping_notice/freight_rpa/browser_profile",
|
|
)
|
|
# Playwright 自带 Chromium 未安装时,回退到本机 Edge(Windows 开发机常见情况)。
|
|
if not os.environ.get("SHIPPING_NOTICE_RPA_WORKER_BROWSER_CHANNEL") and not os.environ.get("SHIPPING_NOTICE_RPA_WORKER_BROWSER_EXECUTABLE_PATH"):
|
|
os.environ.setdefault("SHIPPING_NOTICE_RPA_WORKER_BROWSER_CHANNEL", "msedge")
|
|
|
|
worker = _load_worker()
|
|
username = worker._env("SHIPPING_NOTICE_RPA_WORKER_DSV_USERNAME")
|
|
password = worker._env("SHIPPING_NOTICE_RPA_WORKER_DSV_PASSWORD")
|
|
if not username or not password:
|
|
print("[smoke] 缺少 SHIPPING_NOTICE_RPA_WORKER_DSV_USERNAME / SHIPPING_NOTICE_RPA_WORKER_DSV_PASSWORD,请先配置环境变量。")
|
|
return 1
|
|
|
|
drop_dir = Path(tempfile.mkdtemp(prefix="dsv_smoke_drop_"))
|
|
playwright = context = runtime_profile_dir = None
|
|
entry_url = "https://s360.dbschenker.com/"
|
|
|
|
print(f"[smoke] headless={worker._env('SHIPPING_NOTICE_RPA_WORKER_HEADLESS', 'false')}")
|
|
print(f"[smoke] user={username[:3]}*** entry={entry_url}")
|
|
print("[smoke] 正在打开浏览器并执行 DSV 自动登录…")
|
|
|
|
try:
|
|
playwright, context, page, runtime_profile_dir = worker._open_browser(drop_dir)
|
|
worker._ensure_dsv_session(page, entry_url)
|
|
print(f"[smoke] SUCCESS — DSV session ready, url={page.url}")
|
|
print("[smoke] 请在浏览器中确认 Reports 导航可见。按 Enter 关闭浏览器…")
|
|
try:
|
|
input()
|
|
except EOFError:
|
|
import time
|
|
|
|
print("[smoke] 非交互终端,保持浏览器 300 秒后自动关闭。")
|
|
time.sleep(300)
|
|
return 0
|
|
except Exception as exc:
|
|
print(f"[smoke] FAILED — {exc}")
|
|
traceback.print_exc()
|
|
print("[smoke] 浏览器将保持打开 120 秒以便排查,按 Enter 提前关闭…")
|
|
try:
|
|
input()
|
|
except EOFError:
|
|
import time
|
|
|
|
time.sleep(120)
|
|
return 2
|
|
finally:
|
|
if context is not None:
|
|
try:
|
|
context.close()
|
|
except Exception:
|
|
pass
|
|
if playwright is not None:
|
|
try:
|
|
playwright.stop()
|
|
except Exception:
|
|
pass
|
|
if runtime_profile_dir is not None:
|
|
shutil.rmtree(runtime_profile_dir, ignore_errors=True)
|
|
shutil.rmtree(drop_dir, ignore_errors=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|