feat(schedule): 增加库存源选项描述字段与站点ID支持
This commit is contained in:
1 parent
f4e74f7b07
commit
8b1efbedb9
37 files changed
+8488
-4126
No files matched your search
@@ -0,0 +1,166 @@
|
||||
"""crane_session 登录页识别与自动填写测试。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
def _load_crane_session():
|
||||
module_path = Path(__file__).resolve().parents[2] / "scripts" / "rpa" / "crane_session.py"
|
||||
spec = importlib.util.spec_from_file_location("crane_session_test", module_path)
|
||||
assert spec and spec.loader
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
class _FakeLocator:
|
||||
def __init__(self, *, visible: bool = True, enabled: bool = True) -> None:
|
||||
self.visible = visible
|
||||
self.enabled = enabled
|
||||
self.filled: list[str] = []
|
||||
self.clicked = False
|
||||
|
||||
@property
|
||||
def first(self) -> "_FakeLocator":
|
||||
return self
|
||||
|
||||
def is_visible(self, timeout: int = 0) -> bool:
|
||||
return self.visible
|
||||
|
||||
def is_enabled(self, timeout: int = 0) -> bool:
|
||||
return self.enabled
|
||||
|
||||
def fill(self, value: str, timeout: int = 0) -> None:
|
||||
self.filled.append(value)
|
||||
|
||||
def click(self, timeout: int = 0, force: bool = False) -> None:
|
||||
self.clicked = True
|
||||
|
||||
def evaluate(self, script: str) -> bool:
|
||||
return self.enabled
|
||||
|
||||
|
||||
class _FakePage:
|
||||
def __init__(self) -> None:
|
||||
self.frames: list[_FakePage] = []
|
||||
self.url = "https://wms-portal.craneww.com/"
|
||||
|
||||
def locator(self, selector: str) -> _FakeLocator:
|
||||
lowered = selector.lower()
|
||||
if "user name" in lowered or "password" in lowered:
|
||||
return _FakeLocator(visible=True)
|
||||
if "is-primary" in lowered or "sign in" in lowered:
|
||||
return _FakeLocator(visible=True, enabled=True)
|
||||
return _FakeLocator(visible=False)
|
||||
|
||||
def get_by_label(self, label: str, exact: bool = False) -> _FakeLocator:
|
||||
return _FakeLocator(visible=label in {"User Name", "Password"})
|
||||
|
||||
def get_by_placeholder(self, placeholder: str, exact: bool = False) -> _FakeLocator:
|
||||
return _FakeLocator(visible=placeholder.startswith("Enter "))
|
||||
|
||||
def get_by_role(self, role: str, name: str, exact: bool = False) -> _FakeLocator:
|
||||
return _FakeLocator(visible=role == "button" and name == "Sign in", enabled=True)
|
||||
|
||||
def wait_for_load_state(self, state: str, timeout: int = 0) -> None:
|
||||
return None
|
||||
|
||||
|
||||
def test_is_crane_login_page_detects_username_placeholder():
|
||||
"""登录页应通过 Enter user name... / Enter password... placeholder 识别。"""
|
||||
module = _load_crane_session()
|
||||
page = _FakePage()
|
||||
assert module.is_crane_login_page(page) is True
|
||||
|
||||
|
||||
def test_maybe_auto_login_crane_fills_username_password_and_clicks_sign_in(monkeypatch):
|
||||
"""自动登录应依次填写用户名、密码并点击 Sign in。"""
|
||||
module = _load_crane_session()
|
||||
page = _FakePage()
|
||||
calls: list[tuple[str, str]] = []
|
||||
|
||||
def _fake_fill(
|
||||
page_arg,
|
||||
*,
|
||||
selector_env,
|
||||
value,
|
||||
env,
|
||||
browser_timeout_ms,
|
||||
fallback_selectors,
|
||||
label=None,
|
||||
placeholder=None,
|
||||
):
|
||||
calls.append((selector_env, value))
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(module, "_fill_crane_field", _fake_fill)
|
||||
login_page_states = iter([True, False])
|
||||
monkeypatch.setattr(
|
||||
module,
|
||||
"is_crane_login_page",
|
||||
lambda page_arg: next(login_page_states, False),
|
||||
)
|
||||
monkeypatch.setattr(module, "_click_crane_login_submit", lambda *args, **kwargs: True)
|
||||
|
||||
module.maybe_auto_login_crane(
|
||||
page,
|
||||
env=lambda name, default="": {
|
||||
"FREIGHT_RPA_CRANE_USERNAME": "crane-user",
|
||||
"FREIGHT_RPA_CRANE_PASSWORD": "crane-pass",
|
||||
}.get(name, default),
|
||||
browser_timeout_ms=5_000,
|
||||
)
|
||||
|
||||
assert calls == [
|
||||
("FREIGHT_RPA_CRANE_USERNAME_SELECTOR", "crane-user"),
|
||||
("FREIGHT_RPA_CRANE_PASSWORD_SELECTOR", "crane-pass"),
|
||||
]
|
||||
|
||||
|
||||
def test_find_crane_sign_in_button_waits_for_enabled_state(monkeypatch):
|
||||
"""Sign in 初始 disabled,填写凭据后应能定位到可点击按钮。"""
|
||||
module = _load_crane_session()
|
||||
page = _FakePage()
|
||||
states = iter([False, False, True])
|
||||
|
||||
def _fake_enabled(locator):
|
||||
return next(states, True)
|
||||
|
||||
monkeypatch.setattr(module, "_is_crane_button_enabled", _fake_enabled)
|
||||
|
||||
locator = module._find_crane_sign_in_button(
|
||||
page,
|
||||
selector_env="FREIGHT_RPA_CRANE_LOGIN_BUTTON_SELECTOR",
|
||||
env=lambda name, default="": default,
|
||||
browser_timeout_ms=3_000,
|
||||
)
|
||||
|
||||
assert locator is not None
|
||||
assert locator.enabled is True
|
||||
|
||||
|
||||
def test_ensure_crane_session_skips_login_when_session_active(monkeypatch):
|
||||
"""batch 内 session 有效时不应重复 goto + login。"""
|
||||
worker_path = Path(__file__).resolve().parents[2] / "scripts" / "rpa" / "freight_rpa_worker.py"
|
||||
spec = importlib.util.spec_from_file_location("freight_rpa_worker_crane_test", worker_path)
|
||||
assert spec and spec.loader
|
||||
worker = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(worker)
|
||||
|
||||
page = MagicMock()
|
||||
login = MagicMock()
|
||||
ready = MagicMock()
|
||||
goto = MagicMock()
|
||||
monkeypatch.setattr(worker, "_is_crane_session_active", MagicMock(return_value=True))
|
||||
monkeypatch.setattr(worker, "_goto_crane_entry_url", goto)
|
||||
monkeypatch.setattr(worker, "_maybe_auto_login_crane", login)
|
||||
monkeypatch.setattr(worker, "_wait_for_crane_app_ready", ready)
|
||||
|
||||
worker._ensure_crane_session(page, "https://wms-portal.craneww.com/#/on-hand-details-query")
|
||||
|
||||
goto.assert_not_called()
|
||||
login.assert_not_called()
|
||||
ready.assert_called_once()
|
||||
Reference in new issue
Block a user