fix: 稳定地址表单后单次保存 (#191)
This commit is contained in:
@@ -1410,19 +1410,9 @@ class U2PddLivePurchaseAdapter(U2PddPurchaseAdapter, PddLivePurchaseAdapter):
|
||||
step="purchase_update_shipping_address",
|
||||
)
|
||||
editor.set_text(new_address)
|
||||
self._sleep(0.2)
|
||||
|
||||
latest_edit_root = _parse_xml(self._dump_hierarchy())
|
||||
latest_editor = _shipping_address_editor(latest_edit_root)
|
||||
if latest_editor is None or latest_editor[0] != new_address:
|
||||
raise PddPurchaseError(
|
||||
"PURCHASE_ADDRESS_INPUT_MISMATCH",
|
||||
"详细地址输入后回读不一致,未保存也未下单",
|
||||
step="purchase_update_shipping_address",
|
||||
)
|
||||
save_targets = _clickable_text_targets(latest_edit_root, "保存")
|
||||
save_target = self._wait_for_stable_address_save(new_address)
|
||||
self._click_unique_address_target(
|
||||
save_targets,
|
||||
[save_target],
|
||||
"PURCHASE_ADDRESS_SAVE_AMBIGUOUS",
|
||||
"修改页没有唯一可靠的保存按钮",
|
||||
)
|
||||
@@ -1552,6 +1542,90 @@ class U2PddLivePurchaseAdapter(U2PddPurchaseAdapter, PddLivePurchaseAdapter):
|
||||
diagnostics=diagnostics,
|
||||
)
|
||||
|
||||
def _wait_for_stable_address_save(self, expected_address: str) -> Bounds:
|
||||
"""地址和唯一保存目标连续稳定后,返回最终复核过的点击位置。"""
|
||||
|
||||
deadline = self._monotonic() + self._panel_timeout
|
||||
stable_target: Optional[Bounds] = None
|
||||
stable_reads = 0
|
||||
candidate_count = 0
|
||||
observed_page = "unknown"
|
||||
address_matches = False
|
||||
|
||||
while self._monotonic() < deadline:
|
||||
self._check_cancelled("purchase_update_shipping_address")
|
||||
root = _parse_xml(self._dump_hierarchy())
|
||||
observed_page = _address_page_kind(root)
|
||||
editor = _shipping_address_editor(root)
|
||||
address_matches = editor is not None and editor[0] == expected_address
|
||||
save_targets = _clickable_text_targets(root, "保存")
|
||||
candidate_count = len(save_targets)
|
||||
|
||||
ready = (
|
||||
observed_page == "edit"
|
||||
and address_matches
|
||||
and candidate_count == 1
|
||||
)
|
||||
if ready and save_targets[0] == stable_target:
|
||||
stable_reads += 1
|
||||
elif ready:
|
||||
stable_target = save_targets[0]
|
||||
stable_reads = 1
|
||||
else:
|
||||
stable_target = None
|
||||
stable_reads = 0
|
||||
|
||||
if stable_reads >= 2 and stable_target is not None:
|
||||
# 给表单一次短暂提交准备时间,然后必须用最新控件树再验证。
|
||||
self._sleep(0.3)
|
||||
latest_root = _parse_xml(self._dump_hierarchy())
|
||||
latest_editor = _shipping_address_editor(latest_root)
|
||||
latest_targets = _clickable_text_targets(latest_root, "保存")
|
||||
latest_ready = (
|
||||
_address_page_kind(latest_root) == "edit"
|
||||
and latest_editor is not None
|
||||
and latest_editor[0] == expected_address
|
||||
and latest_targets == [stable_target]
|
||||
)
|
||||
if latest_ready:
|
||||
return stable_target
|
||||
stable_target = None
|
||||
stable_reads = 0
|
||||
observed_page = _address_page_kind(latest_root)
|
||||
address_matches = (
|
||||
latest_editor is not None
|
||||
and latest_editor[0] == expected_address
|
||||
)
|
||||
candidate_count = len(latest_targets)
|
||||
|
||||
self._sleep(0.2)
|
||||
|
||||
diagnostics: dict[str, Any] = {
|
||||
"expected_page": "edit_ready_to_save",
|
||||
"observed_page": observed_page,
|
||||
"address_matches": address_matches,
|
||||
"save_candidate_count": candidate_count,
|
||||
"stable_reads": stable_reads,
|
||||
}
|
||||
artifact = self._save_last_xml("purchase-address-save-not-ready")
|
||||
if artifact is not None:
|
||||
diagnostics["artifacts"] = [artifact]
|
||||
input_mismatch = observed_page == "edit" and not address_matches
|
||||
raise PddPurchaseError(
|
||||
(
|
||||
"PURCHASE_ADDRESS_INPUT_MISMATCH"
|
||||
if input_mismatch
|
||||
else "PURCHASE_ADDRESS_SAVE_NOT_READY"
|
||||
),
|
||||
(
|
||||
"详细地址输入后回读不一致,未保存也未下单"
|
||||
if input_mismatch
|
||||
else "地址编辑页尚未稳定,未点击保存也未下单"
|
||||
),
|
||||
step="purchase_update_shipping_address",
|
||||
diagnostics=diagnostics,
|
||||
)
|
||||
|
||||
def _wait_for_address_page(
|
||||
self, kind: str, *, expected_address: str = ""
|
||||
) -> ET.Element:
|
||||
@@ -1641,9 +1715,18 @@ class U2PddLivePurchaseAdapter(U2PddPurchaseAdapter, PddLivePurchaseAdapter):
|
||||
artifact = self._save_last_xml("purchase-address-saved-timeout")
|
||||
if artifact is not None:
|
||||
diagnostics["artifacts"] = [artifact]
|
||||
save_not_effective = observed_page == "edit"
|
||||
raise PddPurchaseError(
|
||||
"PURCHASE_ADDRESS_PAGE_TIMEOUT",
|
||||
"收货地址页面切换或保存确认超时,未进入不可逆下单阶段",
|
||||
(
|
||||
"PURCHASE_ADDRESS_SAVE_NOT_EFFECTIVE"
|
||||
if save_not_effective
|
||||
else "PURCHASE_ADDRESS_PAGE_TIMEOUT"
|
||||
),
|
||||
(
|
||||
"点击保存后仍停留在地址编辑页,未进入不可逆下单阶段"
|
||||
if save_not_effective
|
||||
else "收货地址页面切换或保存确认超时,未进入不可逆下单阶段"
|
||||
),
|
||||
step="purchase_update_shipping_address",
|
||||
diagnostics=diagnostics,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user