fix: 售罄临时页仅下拉刷新一次 (#265)
This commit is contained in:
@@ -234,6 +234,29 @@ class FakeDevice:
|
||||
raise AssertionError(f"未预期的 selector: {selector}")
|
||||
|
||||
|
||||
class SoldOutRefreshPurchaseDevice(FakeDevice):
|
||||
"""模拟采购深链先显示售罄,下拉一次后恢复或继续售罄。"""
|
||||
|
||||
def __init__(self, sold_out_xml: str, *, persistent: bool = False) -> None:
|
||||
super().__init__(sold_out_xml)
|
||||
self.sold_out_xml = sold_out_xml
|
||||
self.persistent = persistent
|
||||
self.refreshed = False
|
||||
self.swipes = []
|
||||
|
||||
def dump_hierarchy(self):
|
||||
if not self.has_opened:
|
||||
return '<hierarchy><node package="com.xunmeng.pinduoduo" /></hierarchy>'
|
||||
if not self.refreshed or self.persistent:
|
||||
return self.sold_out_xml
|
||||
return home_xml()
|
||||
|
||||
def swipe(self, x1, y1, x2, y2, duration=0.0):
|
||||
self.swipes.append((x1, y1, x2, y2, duration))
|
||||
if y2 > y1 and x1 == x2:
|
||||
self.refreshed = True
|
||||
|
||||
|
||||
class VerticallyScrolledColorPanelDevice(FakeDevice):
|
||||
"""模拟 PDD 记住规格面板纵向位置。"""
|
||||
|
||||
@@ -2143,6 +2166,46 @@ class U2PddPurchaseAdapterTest(unittest.TestCase):
|
||||
self.assertEqual(raised.exception.code, "PDD_PAGE_CAPTCHA")
|
||||
self.assertEqual(device.clicks, [])
|
||||
|
||||
def test_sold_out_page_pulls_down_once_then_recovers_goods(self):
|
||||
sold_out_xml = (
|
||||
FIXTURES / "pdd_sold_out_recommendations.xml"
|
||||
).read_text(encoding="utf-8")
|
||||
device = SoldOutRefreshPurchaseDevice(sold_out_xml)
|
||||
adapter = self._adapter(device, [])
|
||||
|
||||
adapter.open_goods(GOODS_URL)
|
||||
|
||||
self.assertEqual(len(device.swipes), 1)
|
||||
x1, y1, x2, y2, duration = device.swipes[0]
|
||||
self.assertEqual(x1, x2)
|
||||
self.assertGreater(y2, y1)
|
||||
self.assertEqual(duration, 0.5)
|
||||
self.assertEqual(device.clicks, [])
|
||||
adapter.close()
|
||||
|
||||
def test_sold_out_after_one_refresh_is_unavailable(self):
|
||||
sold_out_xml = (
|
||||
FIXTURES / "pdd_sold_out_recommendations.xml"
|
||||
).read_text(encoding="utf-8")
|
||||
device = SoldOutRefreshPurchaseDevice(
|
||||
sold_out_xml,
|
||||
persistent=True,
|
||||
)
|
||||
adapter = self._adapter(device, [])
|
||||
|
||||
with self.assertRaises(PddPurchaseError) as raised:
|
||||
adapter.open_goods(GOODS_URL)
|
||||
|
||||
self.assertEqual(raised.exception.code, "PDD_GOODS_UNAVAILABLE")
|
||||
self.assertFalse(raised.exception.retryable)
|
||||
self.assertEqual(
|
||||
raised.exception.diagnostics,
|
||||
{"page_reason": "out_of_stock", "refresh_attempts": 1},
|
||||
)
|
||||
self.assertEqual(len(device.swipes), 1)
|
||||
self.assertEqual(device.clicks, [])
|
||||
adapter.close()
|
||||
|
||||
def test_stable_home_reopens_once_then_stops_before_click(self):
|
||||
device = FakeDevice(
|
||||
(FIXTURES / "pdd_home_page.xml").read_text(encoding="utf-8")
|
||||
|
||||
Reference in New Issue
Block a user