""" 登录页面 Admin后台登录页面的页面对象模型。 """ from playwright.sync_api import Page from ..base_page import BasePage class LoginPage(BasePage): """登录页面""" # 页面路径 PATH = "/login" # 元素定位器 LOCATORS = { "username_input": '[data-testid="username-input"] input, input[data-testid="username-input"]', "password_input": '[data-testid="password-input"] input, input[data-testid="password-input"]', "submit_button": '[data-testid="login-button"], button[type="submit"]', "error_message": ".el-message--error .el-message__content, .el-message--error, .el-notification__content, .error-message", "page_title": ".login-title, h1", } def __init__(self, page: Page, base_url: str = ""): super().__init__(page, base_url) def navigate(self, path: str = "") -> None: """导航到登录页面""" target_path = path if path else self.PATH self.page.goto(f"{self.base_url}{target_path}") self.wait_for_load() def is_loaded(self) -> bool: """检查页面是否加载完成""" return self.is_element_visible(self.LOCATORS["username_input"]) def login(self, username: str, password: str) -> None: """ 执行登录操作 Args: username: 用户名 password: 密码 """ self.fill_username(username) self.fill_password(password) self.click_submit() def fill_username(self, username: str) -> None: """填写用户名""" self.fill_input(self.LOCATORS["username_input"], username) def fill_password(self, password: str) -> None: """填写密码""" self.fill_input(self.LOCATORS["password_input"], password) def click_submit(self) -> None: """点击登录按钮""" self.click_element(self.LOCATORS["submit_button"]) def get_error_message(self) -> str: """获取错误消息""" if self.is_element_visible(self.LOCATORS["error_message"], timeout=3000): return self.get_text(self.LOCATORS["error_message"]) return "" def has_error_message(self) -> bool: """检查是否有错误消息""" self.wait_for_timeout(1000) return self.is_element_visible( self.LOCATORS["error_message"], timeout=5000 ) def clear_form(self) -> None: """清空表单""" self.page.locator(self.LOCATORS["username_input"]).fill('') self.page.locator(self.LOCATORS["password_input"]).fill('') def is_submit_enabled(self) -> bool: """检查提交按钮是否可用""" return self.is_element_enabled(self.LOCATORS["submit_button"]) def get_page_title(self) -> str: """获取页面标题""" return self.get_text(self.LOCATORS["page_title"]) def wait_for_redirect(self, timeout: int = 10000) -> None: """等待页面跳转""" self.page.wait_for_url("**/dashboard**", timeout=timeout)