fix(login): use text selectors for bottom nav tabs 首页/货源/我的
Replace coordinate tap with text-based UiSelector for reliability. Add back-press loop to ensure app is on main page before checking login.
This commit is contained in:
+39
-15
@@ -54,8 +54,10 @@ def make_ensure_logged_in(
|
||||
):
|
||||
"""创建一个 ensure_logged_in 闭包。
|
||||
|
||||
策略:坐标点击底部"我的"tab(第4个图标)→ 检查"登录/注册"文本。
|
||||
无法通过 resourceId 或 text 定位底部导航时用坐标点击兜底。
|
||||
策略:
|
||||
1. 反复点击返回键,直到底部导航栏(首页/货源/我的)全部可见
|
||||
2. 点击"我的"tab,检查"登录/注册"文本是否存在
|
||||
3. 不存在 → 已登录;存在 → 未登录
|
||||
|
||||
Args:
|
||||
section_config: App 端配置
|
||||
@@ -71,18 +73,35 @@ def make_ensure_logged_in(
|
||||
skip_login = section_config.get("skip_login", True)
|
||||
wait = WebDriverWait(driver, timeout=timeout_s)
|
||||
|
||||
driver.implicitly_wait(3)
|
||||
# ── 底部导航栏三个 tab 定位(使用 text 定位,更可靠)──
|
||||
home_tab = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("首页")')
|
||||
freight_tab = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("货源")')
|
||||
mine_tab = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("我的")')
|
||||
|
||||
# 坐标点击底部"我的"tab(4 tab 均分宽度,第4个中心 x≈0.875*w, y≈h-50)
|
||||
# ── 步骤1: 返回到一级页面 ──
|
||||
# 不断点返回键,直到三个底部导航 tab 都可见
|
||||
max_back_presses = 20
|
||||
for _ in range(max_back_presses):
|
||||
home_visible = _is_displayed(driver, home_tab)
|
||||
freight_visible = _is_displayed(driver, freight_tab)
|
||||
mine_visible = _is_displayed(driver, mine_tab)
|
||||
|
||||
if home_visible and freight_visible and mine_visible:
|
||||
break
|
||||
|
||||
# 不在一级页面 → 点击返回
|
||||
try:
|
||||
w = driver.get_window_size()["width"]
|
||||
h = driver.get_window_size()["height"]
|
||||
driver.tap([(int(w * 0.875), h - 50)])
|
||||
driver.back()
|
||||
driver.implicitly_wait(1)
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"无法点击底部导航栏: {e}")
|
||||
except Exception:
|
||||
break
|
||||
|
||||
# 判断登录态:检查"登录/注册"文本
|
||||
# ── 步骤2: 点击"我的"tab ──
|
||||
wait.until(EC.visibility_of_element_located(mine_tab))
|
||||
driver.find_element(*mine_tab).click()
|
||||
driver.implicitly_wait(1)
|
||||
|
||||
# ── 步骤3: 判断登录态 ──
|
||||
login_check = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("登录/注册")')
|
||||
try:
|
||||
el = driver.find_element(*login_check)
|
||||
@@ -99,11 +118,7 @@ def make_ensure_logged_in(
|
||||
except Exception:
|
||||
pass # 没找到"登录/注册" → 已登录
|
||||
|
||||
# 回到首页
|
||||
home_tab = (
|
||||
AppiumBy.ANDROID_UIAUTOMATOR,
|
||||
'new UiSelector().resourceId("com.arpa.ynchenggangdriver:id/fixed_bottom_navigation_icon").instance(0)',
|
||||
)
|
||||
# ── 步骤4: 回到首页 ──
|
||||
try:
|
||||
wait.until(EC.visibility_of_element_located(home_tab))
|
||||
driver.find_element(*home_tab).click()
|
||||
@@ -113,6 +128,15 @@ def make_ensure_logged_in(
|
||||
return _ensure_logged_in
|
||||
|
||||
|
||||
def _is_displayed(driver, locator) -> bool:
|
||||
"""检查元素是否可见(不抛异常)。"""
|
||||
try:
|
||||
el = driver.find_element(*locator)
|
||||
return el.is_displayed()
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def _perform_ui_login(driver, account: dict, wait) -> None:
|
||||
"""执行 UI 自动登录(skip_login: false 时使用)。"""
|
||||
from appium.webdriver.common.appiumby import AppiumBy
|
||||
|
||||
Reference in New Issue
Block a user