1
1
mirror of https://github.com/KenanZhu/AutoLibrary.git synced 2026-06-18 15:33:03 +08:00

refactor(pages): 将 LoginPage 日志回调从方法参数改为构造器注入

消除 login() 方法签名中的 tracer/log_level 参数,通过构造器可选注入
tracer 统一日志模式,避免 Page Object 对外暴露 MsgBase 内部细节。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-26 18:01:25 +08:00
parent a6bc103c73
commit 280028259f
3 changed files with 21 additions and 176 deletions
+1 -3
View File
@@ -164,7 +164,7 @@ class AutoLibPages(MsgBase):
self._showTrace("未配置图书馆参数 !", self.TraceLevel.ERROR)
return False
url: str = lib_config.get("host_url") + lib_config.get("login_url")
self.__login_page = LoginPage(self.__driver)
self.__login_page = LoginPage(self.__driver, tracer=self._showTrace)
self.__driver.set_page_load_timeout(5)
try:
self.__driver.get(url)
@@ -244,8 +244,6 @@ class AutoLibPages(MsgBase):
password,
captcha_solver=self.__captcha_handler.solveCaptcha,
auto_captcha=auto_captcha,
tracer=self._showTrace,
log_level=self.TraceLevel,
max_attempts=login_config.get("max_attempt", 3),
):
return 1
+20 -11
View File
@@ -7,7 +7,7 @@ This software is provided "as is", without any warranty of any kind.
You may use, modify, and distribute this file under the terms of the MIT License.
See the LICENSE file for details.
"""
from typing import Callable
from typing import Callable, Optional
from selenium.common.exceptions import (
ElementNotInteractableException,
@@ -37,9 +37,21 @@ class LoginPage:
def __init__(
self,
driver: WebDriver,
tracer: Optional[Callable[..., None]] = None,
) -> None:
self._driver: WebDriver = driver
self._tracer: Optional[Callable[..., None]] = tracer
def _trace(
self,
msg: str,
level: int = 20,
no_log: bool = False,
) -> None:
if self._tracer:
self._tracer(msg, level, no_log)
def navigate(
self,
@@ -177,16 +189,13 @@ class LoginPage:
password: str,
captcha_solver: Callable[["LoginPage", bool], str],
auto_captcha: bool,
tracer: Callable[..., None],
log_level: type,
max_attempts: int = 5,
) -> bool:
ERR = log_level.ERROR
for attempt in range(max_attempts):
tracer(
self._trace(
f"用户 {username}{attempt + 1} 次尝试登录......",
20, no_log=True,
no_log=True,
)
if not self.fillCredentials(username, password):
continue
@@ -195,16 +204,16 @@ class LoginPage:
continue
if not self.fillCaptcha(captcha_text):
continue
tracer("尝试登录...", 20, no_log=True)
self._trace("尝试登录...", no_log=True)
if not self.clickLogin():
continue
if self.waitLoginSuccess():
tracer(f"用户 {username}{attempt + 1} 次登录成功 !")
self._trace(f"用户 {username}{attempt + 1} 次登录成功 !")
return True
else:
err_msg = (
self._trace(
"登录页面加载失败 ! : "
"用户账号或者密码错误/验证码错误, 具体以页面提示为准"
"用户账号或者密码错误/验证码错误, 具体以页面提示为准",
level=40,
)
tracer(err_msg, ERR)
return False