fix: Playwright API camelCase → snake_case (Python 版)

- getByPlaceholder → get_by_placeholder
- getByRole → get_by_role
- getByText → get_by_text
- getByTestId → get_by_test_id
This commit is contained in:
xst
2026-07-15 10:52:16 +08:00
parent 7b4a54f1ce
commit 7c5a4c5de7
6 changed files with 18 additions and 18 deletions
+3 -3
View File
@@ -138,9 +138,9 @@ def ensure_login(web_config, page, config):
# ── skip_login: false,执行 UI 登录(可能有验证码,会失败) ── # ── skip_login: false,执行 UI 登录(可能有验证码,会失败) ──
page.goto(f"{base_url}/login") page.goto(f"{base_url}/login")
page.getByPlaceholder("请输入用户名").fill(account["phone"]) page.get_by_placeholder("请输入用户名").fill(account["phone"])
page.getByPlaceholder("请输入密码").fill(account["password"]) page.get_by_placeholder("请输入密码").fill(account["password"])
page.getByRole("button", name="登录").click() page.get_by_role("button", name="登录").click()
# 如果有图形验证码,这里会超时 # 如果有图形验证码,这里会超时
try: try:
+3 -3
View File
@@ -6,8 +6,8 @@ Usage:
class LoginPage(BasePage): class LoginPage(BasePage):
def __init__(self, page): def __init__(self, page):
super().__init__(page) super().__init__(page)
self.phone_input = page.getByPlaceholder("请输入用户名") self.phone_input = page.get_by_placeholder("请输入用户名")
self.login_btn = page.getByRole("button", name="登录") self.login_btn = page.get_by_role("button", name="登录")
""" """
from datetime import datetime from datetime import datetime
@@ -38,7 +38,7 @@ class BasePage:
def wait_for_text(self, text: str, timeout_ms: Optional[int] = None) -> None: def wait_for_text(self, text: str, timeout_ms: Optional[int] = None) -> None:
"""等待页面上出现指定文本。""" """等待页面上出现指定文本。"""
expect(self.page.getByText(text)).to_be_visible(timeout=timeout_ms or self.timeout) expect(self.page.get_by_text(text)).to_be_visible(timeout=timeout_ms or self.timeout)
# ── 通用操作 ── # ── 通用操作 ──
+3 -3
View File
@@ -21,7 +21,7 @@ class NavBar(BasePage):
super().__init__(page) super().__init__(page)
# ⚠️ 以下定位器需要根据实际 DOM 调整 # ⚠️ 以下定位器需要根据实际 DOM 调整
self.sidebar = page.locator(".ant-layout-sider") self.sidebar = page.locator(".ant-layout-sider")
self.logout_btn = page.getByText("退出登录") self.logout_btn = page.get_by_text("退出登录")
def go_to(self, menu_name: str) -> None: def go_to(self, menu_name: str) -> None:
"""点击侧边栏菜单项展开/跳转。 """点击侧边栏菜单项展开/跳转。
@@ -29,7 +29,7 @@ class NavBar(BasePage):
Args: Args:
menu_name: 菜单名称文本(如 "运输管理""基础信息" menu_name: 菜单名称文本(如 "运输管理""基础信息"
""" """
menu_item = self.page.getByText(menu_name).first menu_item = self.page.get_by_text(menu_name).first
self.click_when_ready(menu_item) self.click_when_ready(menu_item)
def go_to_submenu(self, parent_menu: str, sub_menu: str) -> None: def go_to_submenu(self, parent_menu: str, sub_menu: str) -> None:
@@ -40,7 +40,7 @@ class NavBar(BasePage):
sub_menu: 子菜单文本(如 "运单管理" sub_menu: 子菜单文本(如 "运单管理"
""" """
self.go_to(parent_menu) self.go_to(parent_menu)
sub_item = self.page.getByText(sub_menu).first sub_item = self.page.get_by_text(sub_menu).first
self.click_when_ready(sub_item) self.click_when_ready(sub_item)
def logout(self) -> None: def logout(self) -> None:
+3 -3
View File
@@ -18,9 +18,9 @@ class LoginPage(BasePage):
def __init__(self, page: Page): def __init__(self, page: Page):
super().__init__(page) super().__init__(page)
# ── 定位器声明 ── # ── 定位器声明 ──
self.username_input = page.getByPlaceholder("请输入用户名") self.username_input = page.get_by_placeholder("请输入用户名")
self.password_input = page.getByPlaceholder("请输入密码") self.password_input = page.get_by_placeholder("请输入密码")
self.login_btn = page.getByRole("button", name="登录") self.login_btn = page.get_by_role("button", name="登录")
self.error_tip = page.locator(".ant-form-item-explain-error") self.error_tip = page.locator(".ant-form-item-explain-error")
def login(self, phone: str, password: str, expect_success: bool = True) -> None: def login(self, phone: str, password: str, expect_success: bool = True) -> None:
+2 -2
View File
@@ -90,8 +90,8 @@ def main():
page.goto(login_url) page.goto(login_url)
# ── 填入账号密码 ── # ── 填入账号密码 ──
page.getByPlaceholder("请输入用户名").fill(account["phone"]) page.get_by_placeholder("请输入用户名").fill(account["phone"])
page.getByPlaceholder("请输入密码").fill(account["password"]) page.get_by_placeholder("请输入密码").fill(account["password"])
print(f"👤 已填入账号: {account['phone']}(角色: {args.role}") print(f"👤 已填入账号: {account['phone']}(角色: {args.role}")
print("📝 请在浏览器中手动完成验证码并点击登录...") print("📝 请在浏览器中手动完成验证码并点击登录...")
print(" (登录成功后脚本会自动检测并保存)\n") print(" (登录成功后脚本会自动检测并保存)\n")
+4 -4
View File
@@ -64,15 +64,15 @@ class TestLoginPageDirect:
"""验证登录页可直接访问并正常渲染。""" """验证登录页可直接访问并正常渲染。"""
page.goto(f"{web_config['base_url']}/login") page.goto(f"{web_config['base_url']}/login")
# 登录页应存在用户名输入框 # 登录页应存在用户名输入框
assert page.getByPlaceholder("请输入用户名").is_visible() assert page.get_by_placeholder("请输入用户名").is_visible()
@pytest.mark.p1 @pytest.mark.p1
def test_login_page_elements(self, page: Page, web_config): def test_login_page_elements(self, page: Page, web_config):
"""验证登录页所有关键元素存在。""" """验证登录页所有关键元素存在。"""
page.goto(f"{web_config['base_url']}/login") page.goto(f"{web_config['base_url']}/login")
assert page.getByPlaceholder("请输入用户名").is_visible() assert page.get_by_placeholder("请输入用户名").is_visible()
assert page.getByPlaceholder("请输入密码").is_visible() assert page.get_by_placeholder("请输入密码").is_visible()
assert page.getByRole("button", name="登录").is_visible() assert page.get_by_role("button", name="登录").is_visible()
@pytest.mark.p1 @pytest.mark.p1
def test_double_login_session_redirect(self, page: Page, web_config, ensure_login): def test_double_login_session_redirect(self, page: Page, web_config, ensure_login):