# 登录页面操作 # page_objects/login_page.py from appium.webdriver.common.appiumby import AppiumBy from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException, NoSuchElementException import logging import time import subprocess import globals.ids as ids import globals.global_variable as global_variable # 导入全局变量模块 import globals.apis as apis class LoginPage: def __init__(self, driver, wait): self.driver = driver self.wait = wait def navigate_to_login_page(self, driver, device_id): """ 补充的跳转页面函数:当设备处于未知状态时,尝试跳转到登录页面 参数: driver: 已初始化的Appium WebDriver对象 device_id: 设备ID,用于日志记录 """ try: target_package = 'com.bjjw.cjgc' target_activity = '.activity.LoginActivity' # 使用ADB命令启动Activity try: logging.info(f"尝试使用ADB命令启动LoginActivity: {target_package}/{target_activity}") adb_command = f"adb -s {device_id} shell am start -n {target_package}/{target_activity}" result = subprocess.run(adb_command, shell=True, capture_output=True, text=True) if result.returncode == 0: logging.info(f"使用ADB命令启动LoginActivity成功") time.sleep(2) # 等待Activity启动 return True else: logging.warning(f"ADB命令执行失败: {result.stderr}") except Exception as adb_error: logging.warning(f"执行ADB命令时出错: {adb_error}") except Exception as e: logging.error(f"跳转到登录页面过程中发生未预期错误: {e}") # 所有尝试都失败 return False def is_login_page(self): """检查当前是否为登录页面""" try: return self.driver.find_element(AppiumBy.ID, ids.LOGIN_BTN).is_displayed() except NoSuchElementException: return False def login(self, username=None): """执行登录操作""" try: logging.info("正在执行登录操作...") # # 获取文本框中已有的用户名 # username_field = self.wait.until( # EC.element_to_be_clickable((AppiumBy.ID, ids.LOGIN_USERNAME)) # ) # 获取用户名输入框 username_field = self.wait.until( EC.element_to_be_clickable((AppiumBy.ID, ids.LOGIN_USERNAME)) ) # 填写用户名 if username: # 清空用户名输入框 try: username_field.clear() except: pass # 填写传入的用户名 username_field.send_keys(username) existing_username = username logging.info(f"已填写用户名: {username}") else: # 获取文本框中已有的用户名 existing_username = username_field.text # 日志记录获取到的已有用户名(若为空,也需明确记录,避免后续误解) if existing_username.strip(): # 去除空格后判断是否有有效内容 logging.info(f"已获取文本框中的已有用户名: {existing_username}") else: logging.info("文本框中未检测到已有用户名(内容为空)") # 将用户名写入全局变量中 global_variable.GLOBAL_USERNAME = existing_username # 关键:给全局变量赋值 # global_variable.set_username(existing_username) # # 读取文本框内已有的用户名(.text属性获取元素显示的文本内容) # existing_username = username_field.text # # 3. 将获取到的用户名写入全局变量中 # # global_variable.GLOBAL_USERNAME = existing_username # 关键:给全局变量赋值 # global_variable.set_username(existing_username) # # 日志记录获取到的已有用户名(若为空,也需明确记录,避免后续误解) # if existing_username.strip(): # 去除空格后判断是否有有效内容 # logging.info(f"已获取文本框中的已有用户名: {existing_username}") # else: # logging.info("文本框中未检测到已有用户名(内容为空)") # 1. 定位密码输入框 password_field = self.wait.until( EC.element_to_be_clickable((AppiumBy.ID, ids.LOGIN_PASSWORD)) ) # 2. 清空密码框(如果需要) try: password_field.clear() # time.sleep(0.5) # 等待清除完成 except: # 如果clear方法不可用,尝试其他方式 pass accounts = apis.get_accounts_from_server("68ef0e02b0138d25e2ac9918") matches = [acc for acc in accounts if acc.get("username") == existing_username] if matches: password = matches[0].get("password") password_field.send_keys(password) # 4. 可选:隐藏键盘 try: self.driver.hide_keyboard() except: pass # 点击登录按钮 max_retries = 3 retry_count = 0 while retry_count < max_retries: login_btn = self.wait.until( EC.element_to_be_clickable((AppiumBy.ID, ids.LOGIN_BTN)) ) login_btn.click() logging.info(f"已点击登录按钮 (尝试 {retry_count + 1}/{max_retries})") # 等待登录完成 time.sleep(3) # 检查是否登录成功 if self.is_login_successful(): logging.info("登录成功") return True else: logging.warning("登录后未检测到主页面元素,准备重试") retry_count += 1 if retry_count < max_retries: logging.info(f"等待2秒后重新尝试登录...") time.sleep(2) logging.error(f"登录失败,已尝试 {max_retries} 次") return False except Exception as e: logging.error(f"登录过程中出错: {str(e)}") return False def is_login_successful(self): """检查登录是否成功""" try: # 等待主页面元素出现 self.wait.until( EC.presence_of_element_located((AppiumBy.ID, ids.DOWNLOAD_TABBAR_ID)) ) return True except TimeoutException: return False