跟 dacli/tests/e2e/_helpers/ 一一对应 (TS 已 v17 提交), API 对齐:
- selectors.py: SEL/formItemByLabel/rowAction (147 行)
- wait.py: waitSpaReady/waitTableReady/waitFormModalReady/waitModalClose/waitRowCountChange
- nav.py: clickMenu/expandCategory (精确匹配 + SPA 路由触发)
- form.py: fillByLabel/selectByLabel/fillDateByLabel/fillTextareaByLabel/
fillNumberByLabel/saveModal/closeModal
- table.py: countRows/findRow/clickRowAction/getTotalText/getTotalCount/clickAdd
- modal.py: waitOpen/waitClose/getErrors/isOpen/getTitle
- __init__.py: 聚合导出, camelCase API 与 TS 版一致
Python 528 行, TS 800 行 (TS 多 ~50% 因为类型注解 + 显式 interface)
用法 (Python 脚本):
from lib import clickMenu, fillByLabel, saveModal, countRows, clickAdd
用法 (TypeScript spec):
import { clickMenu, fillByLabel, ... } from './_helpers'
后续 (留待 v18+):
- 用新库重写 jnpf_browser_e2e.py / jnpf_client.py (去重)
- ssw-e2e-test/SKILL.md 加 "标准库" 段 + 示例
77 lines
2.7 KiB
Python
77 lines
2.7 KiB
Python
"""
|
|
nav.py — 侧边栏菜单导航 (Python 版)
|
|
|
|
jnpf 6.2.0 菜单特点 (盲点 42 防御):
|
|
- **扁平** 结构 (jnpf-menu-item 全在根), 不是 antd 嵌套
|
|
- 部分文本会匹配多个项目, 必须精确匹配
|
|
- 隐藏元素不能 force click, 用 page.evaluate JS click 触发 SPA 路由
|
|
"""
|
|
|
|
from .selectors import SEL
|
|
|
|
|
|
def clickMenu(page, menuName, selector=None, waitFor=None, timeout=8000):
|
|
"""
|
|
点击侧边栏菜单 (精确匹配, 避免部分匹配冲突)
|
|
走 page.evaluate JS click, 隐藏元素也能触发 SPA 路由
|
|
|
|
@param page: Playwright Page
|
|
@param menuName: 菜单名 (e.g. '员工请假申请')
|
|
@param selector: 指定选择器 (None=按 SEL.MENU_ITEM_CANDIDATES 优先级)
|
|
@param waitFor: 等待路由后新元素出现 (e.g. '.vxe-table')
|
|
@param timeout: 等待超时 (ms), 默认 8s
|
|
@return dict {ok, matched, selector, url?}
|
|
"""
|
|
result = page.evaluate(
|
|
"""({name, selector}) => {
|
|
const candidates = selector
|
|
? [selector]
|
|
: ['.jnpf-menu-item', '.ant-menu-item', '[class*="menu-item"]']
|
|
for (const sel of candidates) {
|
|
const items = Array.from(document.querySelectorAll(sel))
|
|
for (const item of items) {
|
|
const text = (item.textContent || '').trim()
|
|
if (text === name) {
|
|
item.click()
|
|
return { ok: true, matched: items.length, selector: sel }
|
|
}
|
|
}
|
|
}
|
|
const all = document.querySelectorAll('.jnpf-menu-item')
|
|
const sample = Array.from(all).slice(0, 5).map(e => (e.textContent || '').trim())
|
|
return {
|
|
ok: false, matched: all.length, selector: '.jnpf-menu-item', candidates: sample
|
|
}
|
|
}""",
|
|
{"name": menuName, "selector": selector},
|
|
)
|
|
|
|
if not result.get("ok"):
|
|
sample = result.get("candidates", [])
|
|
raise RuntimeError(
|
|
f"clickMenu: 找不到菜单 \"{menuName}\" (matched={result.get('matched')})\n"
|
|
f" 候选样本: {sample}"
|
|
)
|
|
|
|
if waitFor:
|
|
page.wait_for_selector(waitFor, timeout=timeout)
|
|
else:
|
|
# 默认给 SPA 路由 + 组件渲染 800ms
|
|
page.wait_for_timeout(800)
|
|
|
|
return {**result, "url": page.url}
|
|
|
|
|
|
def expandCategory(page, categoryName):
|
|
"""
|
|
展开分类菜单 (嵌套结构才用, jnpf 6.2.0 通常扁平不需要)
|
|
兜底: 找不到时静默返回
|
|
"""
|
|
handle = page.locator(
|
|
f'{SEL["MENU_SUB_CONTENT"]}:has-text("{categoryName}"), '
|
|
f'{SEL["MENU_SUB"]}:has-text("{categoryName}")'
|
|
).first
|
|
if handle.count() > 0:
|
|
handle.click()
|
|
page.wait_for_timeout(300)
|