"""scripts/rpa/dsv_report_parameters.py 单元测试。""" from __future__ import annotations import importlib.util from pathlib import Path def _load_module(): path = Path(__file__).resolve().parents[2] / "scripts" / "rpa" / "dsv_report_parameters.py" spec = importlib.util.spec_from_file_location("dsv_report_parameters_test", path) assert spec and spec.loader module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def test_fill_dsv_owner_filter_defaults_to_reportviewer_owner_input(): """Owner 未配置 selector 时,应写入 ReportViewer Owner 文本框。""" module = _load_module() filled: dict[str, str] = {} class _FakeText: first = None def __init__(self) -> None: self.first = self def is_visible(self, timeout: int | None = None) -> bool: return False class _FakeFrame: def evaluate(self, script: str, payload: dict[str, str], timeout: int | None = None) -> bool: assert "disabled = false" in script filled[payload["selector"]] = payload["value"] return True class _FakePage: frames = [_FakeFrame()] def get_by_text(self, text: str, exact: bool = False) -> _FakeText: return _FakeText() module.fill_dsv_filter_if_needed( _FakePage(), "FREIGHT_RPA_DSV_OWNER_SELECTOR", "Owner", "CADU-0000000100", env=lambda name, default="": default, browser_timeout_ms=lambda: 3_000, ) assert filled == {"#ReportViewerMain_ctl08_ctl03_txtValue": "CADU-0000000100"} def test_fill_dsv_warehouse_select_uses_default_ddvalue(): """Warehouse 未配置 selector 时,应对 ReportViewer select 执行 select_option。""" module = _load_module() selected: dict[str, str] = {} class _FakeLocator: first = None def __init__(self) -> None: self.first = self def wait_for(self, *args, **kwargs) -> None: return None def evaluate(self, script: str, value: str | None = None): if value is not None and "removeAttribute" in script: selected["value"] = value return True if "realOptionCount" in script: return { "hasTarget": True, "enabled": False, "optionCount": 26, "realOptionCount": 25, } if "aspNetDisabled" in script and "hasTarget" not in script: return True return False def select_option(self, *, label: str, timeout: int) -> None: selected["label"] = label class _FakeReportRootLocator: def count(self) -> int: return 1 class _FakeFrame: def locator(self, selector: str) -> _FakeLocator | _FakeReportRootLocator: if selector == "#ReportViewerMain": return _FakeReportRootLocator() assert selector == "#ReportViewerMain_ctl08_ctl17_ddValue" return _FakeLocator() class _FakePage: frames = [_FakeFrame()] def get_by_text(self, text: str, exact: bool = False): raise RuntimeError("not visible") module.fill_dsv_filter_if_needed( _FakePage(), "FREIGHT_RPA_DSV_WAREHOUSE_SELECTOR", "Warehouse", "BYAWSATL01", env=lambda name, default="": default, browser_timeout_ms=lambda: 3_000, ) assert selected.get("value") == "BYAWSATL01" or selected.get("label") == "BYAWSATL01"