Files
smart-management-auto-test/zhyy/test_case/Resource/UI/login_page.py

48 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from playwright.sync_api import Page
from zhyy.test_case.Resource.UI.base_page import BasePage
class LoginPage(BasePage):
"""
登录页对象。
注意:
当前用例未提供登录页侦察结果,因此以下 selector 使用 TODO 占位。
落地执行前请通过页面侦察补充真实 selector。
"""
USERNAME_INPUT = "TODO_LOGIN_USERNAME_INPUT_SELECTOR"
PASSWORD_INPUT = "TODO_LOGIN_PASSWORD_INPUT_SELECTOR"
LOGIN_BUTTON = "TODO_LOGIN_BUTTON_SELECTOR"
def __init__(self, page: Page):
super().__init__(page)
def is_login_page(self) -> bool:
"""
判断当前是否处于登录页。
由于缺少真实登录页 selector这里通过 TODO selector 判断。
若系统已通过 SSO 或已有登录态直接进入业务页,可返回 False。
"""
if "TODO" in self.USERNAME_INPUT:
# 未补充登录页定位时,不主动执行登录,避免误判。
return False
try:
return self.page.locator(self.USERNAME_INPUT).first.is_visible(timeout=3_000)
except Exception:
return False
def login_if_needed(self, username: str, password: str):
"""
如当前页面需要登录,则执行登录。
"""
if not self.is_login_page():
return
self.fill(self.USERNAME_INPUT, username)
self.fill(self.PASSWORD_INPUT, password)
self.click(self.LOGIN_BUTTON)
self.wait_for_network_idle()