Files
QaAutomationHub/tests/web/pages/login_page.py
T
xst 7c5a4c5de7 fix: Playwright API camelCase → snake_case (Python 版)
- getByPlaceholder → get_by_placeholder
- getByRole → get_by_role
- getByText → get_by_text
- getByTestId → get_by_test_id
2026-07-15 10:52:16 +08:00

53 lines
1.8 KiB
Python

"""登录页面 — Page Object。
Usage:
from tests.web.pages.login_page import LoginPage
login_page = LoginPage(page)
login_page.login(phone="super_admin", password="951260684NiAn..")
"""
from playwright.sync_api import Page, expect
from tests.web.pages.base_page import BasePage
class LoginPage(BasePage):
"""运八管理端登录页。"""
def __init__(self, page: Page):
super().__init__(page)
# ── 定位器声明 ──
self.username_input = page.get_by_placeholder("请输入用户名")
self.password_input = page.get_by_placeholder("请输入密码")
self.login_btn = page.get_by_role("button", name="登录")
self.error_tip = page.locator(".ant-form-item-explain-error")
def login(self, phone: str, password: str, expect_success: bool = True) -> None:
"""执行登录流程。
Args:
phone: 用户名/手机号
password: 密码
expect_success: True=预期成功跳转,False=预期留在登录页并有错误提示
"""
self.fill_field(self.username_input, phone)
self.fill_field(self.password_input, password)
self.click_when_ready(self.login_btn)
if expect_success:
# 等待登录成功后页面跳转
self.page.wait_for_url("**/admin/**", timeout=10_000)
else:
# 预期登录失败,停留在当前页
expect(self.page).to_have_url("**/login**", timeout=5_000)
def get_error_message(self) -> str:
"""获取登录失败时的错误提示文本。"""
self.wait_visible(self.error_tip)
return self.error_tip.inner_text()
def is_on_login_page(self) -> bool:
"""检查是否仍在登录页。"""
return "/login" in self.page.url