Files
claude-config/skills/ssw-e2e-test/scripts/lib/form.py
T
ssw 54b3e01d13 v18: 6 进阶组件 helper Python 镜像 (form.py + __init__.py)
镜像 TS 版 6 新 helper:
- fillDateRangeByLabel (dateRange picker)
- toggleSwitchByLabel (开关 on/off/toggle, aria-checked 检测)
- selectRadioByLabel (radio group 按 index)
- checkCheckboxByLabel (checkbox group 按 index)
- selectTreeByLabel (treeSelect 按节点文字)
- uploadFileByLabel (file upload via set_input_files)

Python lib form.py: 147 → 280 行 (+133)
__init__.py: 40 → 46 行 (+6 exports)

typecheck 干净 (ast.parse OK)
2026-06-15 18:00:35 +08:00

281 lines
9.9 KiB
Python

"""
form.py — 表单操作 helpers (Python 版, v18 含 6 进阶组件)
核心模式:
- **按 label 找 input/select/textarea** — jnpf 6.2.0 label 是稳定契约
- **select 必用 mouse.click 触发** (page.evaluate click 不行, antd v-model 内部监听 mouse 事件)
- **保存/取消走 modal 内主按钮** (优先级: confirm-btns primary > modal primary > "保存" > "确定")
"""
from .selectors import SEL, formItemByLabel
def fillByLabel(page, label, value, scope=None, timeout=5000):
"""
按 label 文本找到 input 并填值
优先 placeholder 包含 label, 兜底 form-item 找 input
"""
if scope is None:
scope = SEL["MODAL"]
byPh = page.locator(
f'{scope} input[placeholder*="{label}"]'
).first
if byPh.count() > 0:
byPh.fill(value, timeout=timeout)
return
item = formItemByLabel(page, label, scope).first
if item.count() == 0:
raise RuntimeError(f'fillByLabel: 找不到 label="{label}" 的 input')
item.locator("input").first.fill(value, timeout=timeout)
def selectByLabel(page, label, optionIndex=0, scope=None):
"""
按 label 找到 select 并选第 N 个 option (0=第一个)
关键: ant-select 必须 mouse.click 触发, page.evaluate click 不行
"""
if scope is None:
scope = SEL["MODAL"]
item = formItemByLabel(page, label, scope).first
if item.count() == 0:
raise RuntimeError(f'selectByLabel: 找不到 label="{label}"')
select = item.locator(SEL["SELECT"]).first
if select.count() == 0:
raise RuntimeError(f'selectByLabel: "{label}" 不是 select 类型')
box = select.bounding_box()
if not box:
raise RuntimeError(f'selectByLabel: "{label}" 无 bounding box')
# mouse.click 触发 antd dropdown
page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2)
page.wait_for_selector(SEL["SELECT_DROPDOWN"], timeout=5000)
page.wait_for_selector(
f'{SEL["SELECT_DROPDOWN"]} {SEL["SELECT_ITEM"]}', timeout=5000
)
options = page.locator(
f'{SEL["SELECT_DROPDOWN"]} {SEL["SELECT_ITEM"]}'
)
count = options.count()
if count <= optionIndex:
raise RuntimeError(
f'selectByLabel: "{label}" 只有 {count} 个 option, 索引 {optionIndex} 越界'
)
options.nth(optionIndex).click()
page.wait_for_timeout(200)
def fillDateByLabel(page, label, date, scope=None):
"""
按 label 找到 date picker 并填日期 (YYYY-MM-DD)
jnpf 用 .ant-picker, 内部 <input>, fill 后 Tab 触发 onChange
"""
if scope is None:
scope = SEL["MODAL"]
item = formItemByLabel(page, label, scope).first
if item.count() == 0:
raise RuntimeError(f'fillDateByLabel: 找不到 label="{label}"')
input_ = item.locator(SEL["PICKER"]).first
if input_.count() == 0:
raise RuntimeError(f'fillDateByLabel: "{label}" 不是日期类型')
input_.fill(date)
page.keyboard.press("Tab")
page.wait_for_timeout(200)
def fillTextareaByLabel(page, label, value, scope=None):
"""按 label 找到 textarea 并填值"""
if scope is None:
scope = SEL["MODAL"]
item = formItemByLabel(page, label, scope).first
if item.count() == 0:
raise RuntimeError(f'fillTextareaByLabel: 找不到 label="{label}"')
ta = item.locator("textarea").first
if ta.count() == 0:
raise RuntimeError(f'fillTextareaByLabel: "{label}" 不是 textarea 类型')
ta.fill(value)
def fillNumberByLabel(page, label, value, scope=None):
"""按 label 找到 inputNumber (jnpf 用 .ant-input-number) 并填数值"""
if scope is None:
scope = SEL["MODAL"]
byPh = page.locator(
f'{scope} .ant-input-number input[placeholder*="{label}"]'
).first
if byPh.count() > 0:
byPh.fill(str(value))
page.keyboard.press("Tab")
return
item = formItemByLabel(page, label, scope).first
if item.count() == 0:
raise RuntimeError(f'fillNumberByLabel: 找不到 label="{label}"')
input_ = item.locator(".ant-input-number input").first
input_.fill(str(value))
page.keyboard.press("Tab")
def saveModal(page, scope=None):
"""
点击 modal 内的主按钮 (保存/确定)
优先级: confirm-btns primary > modal primary > "保存" > "确定"
"""
if scope is None:
scope = SEL["MODAL"]
btn = page.locator(
f'{scope} button:has-text("保 存"), '
f'{scope} button:has-text("确 定"), '
f'{scope} button.ant-btn-primary'
).first
if btn.count() == 0:
raise RuntimeError("saveModal: modal 内找不到保存按钮")
btn.click()
def closeModal(page, scope=None):
"""关闭 modal (优先"关闭", 再"取消", 最后 Escape)"""
if scope is None:
scope = SEL["MODAL"]
for sel in [
f'{scope} {SEL["MODAL_CLOSE"]}',
f'{scope} {SEL["MODAL_CANCEL"]}',
]:
btn = page.locator(sel).first
if btn.count() > 0:
btn.click()
page.wait_for_timeout(300)
return
page.keyboard.press("Escape")
page.wait_for_timeout(300)
# =============================================================
# v18+ 进阶组件
# =============================================================
def fillDateRangeByLabel(page, label, start_date, end_date, scope=None):
"""
按 label 找到 date range picker, 填两个日期 (YYYY-MM-DD)
antd RangePicker 用 .ant-picker-input 两个 input, fill 后 Tab
"""
if scope is None:
scope = SEL["MODAL"]
item = formItemByLabel(page, label, scope).first
if item.count() == 0:
raise RuntimeError(f'fillDateRangeByLabel: 找不到 label="{label}"')
inputs = item.locator(SEL["DATE_PICKER_INPUT"])
count = inputs.count()
if count < 2:
raise RuntimeError(
f'fillDateRangeByLabel: "{label}" 不是 range picker (只有 {count} input)'
)
inputs.nth(0).fill(start_date)
page.keyboard.press("Tab")
page.wait_for_timeout(200)
inputs.nth(1).fill(end_date)
page.keyboard.press("Tab")
page.wait_for_timeout(200)
def toggleSwitchByLabel(page, label, to_state="toggle", scope=None):
"""
按 label 找到 switch, 点击切换开/关
to_state: 'on' | 'off' | 'toggle'
"""
if scope is None:
scope = SEL["MODAL"]
item = formItemByLabel(page, label, scope).first
if item.count() == 0:
raise RuntimeError(f'toggleSwitchByLabel: 找不到 label="{label}"')
sw = item.locator(SEL["SWITCH"]).first
if sw.count() == 0:
raise RuntimeError(f'toggleSwitchByLabel: "{label}" 不是 switch 类型')
is_on = sw.get_attribute("aria-checked") == "true"
need_click = (
to_state == "toggle"
or (to_state == "on" and not is_on)
or (to_state == "off" and is_on)
)
if need_click:
sw.click()
page.wait_for_timeout(200)
def selectRadioByLabel(page, label, option_index=0, scope=None):
"""按 label 找到 radio group, 点第 N 个选项 (0=第一个)"""
if scope is None:
scope = SEL["MODAL"]
item = formItemByLabel(page, label, scope).first
if item.count() == 0:
raise RuntimeError(f'selectRadioByLabel: 找不到 label="{label}"')
radios = item.locator(SEL["RADIO_ITEM"])
count = radios.count()
if count <= option_index:
raise RuntimeError(
f'selectRadioByLabel: "{label}" 只有 {count} 个 radio, 索引 {option_index} 越界'
)
radios.nth(option_index).click()
page.wait_for_timeout(200)
def checkCheckboxByLabel(page, label, option_index=0, scope=None):
"""按 label 找到 checkbox group, 勾选第 N 个选项 (0=第一个)"""
if scope is None:
scope = SEL["MODAL"]
item = formItemByLabel(page, label, scope).first
if item.count() == 0:
raise RuntimeError(f'checkCheckboxByLabel: 找不到 label="{label}"')
boxes = item.locator(SEL["CHECKBOX_ITEM"])
count = boxes.count()
if count <= option_index:
raise RuntimeError(
f'checkCheckboxByLabel: "{label}" 只有 {count} 个 checkbox, 索引 {option_index} 越界'
)
boxes.nth(option_index).click()
page.wait_for_timeout(200)
def selectTreeByLabel(page, label, node_text, scope=None):
"""
按 label 找到 tree-select, 按节点文字精确匹配
"""
if scope is None:
scope = SEL["MODAL"]
item = formItemByLabel(page, label, scope).first
if item.count() == 0:
raise RuntimeError(f'selectTreeByLabel: 找不到 label="{label}"')
trigger = item.locator(SEL["TREE_SELECT"]).first
if trigger.count() == 0:
raise RuntimeError(f'selectTreeByLabel: "{label}" 不是 tree-select 类型')
box = trigger.bounding_box()
if not box:
raise RuntimeError(f'selectTreeByLabel: "{label}" 无 bounding box')
page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2)
page.wait_for_selector(SEL["TREE_SELECT_DROPDOWN"], timeout=5000)
page.wait_for_timeout(300)
nodes = page.locator(SEL["TREE_SELECT_NODE"])
n = nodes.count()
for i in range(n):
t = (nodes.nth(i).text_content() or "").strip()
if t == node_text:
nodes.nth(i).click()
page.wait_for_timeout(300)
return
raise RuntimeError(
f'selectTreeByLabel: "{label}" 找不到节点 "{node_text}" (共 {n} 节点)'
)
def uploadFileByLabel(page, label, file_path, scope=None):
"""
按 label 找到 upload, setInputFiles 触发文件选择
"""
if scope is None:
scope = SEL["MODAL"]
item = formItemByLabel(page, label, scope).first
if item.count() == 0:
raise RuntimeError(f'uploadFileByLabel: 找不到 label="{label}"')
input_ = item.locator(SEL["UPLOAD_INPUT"]).first
if input_.count() == 0:
raise RuntimeError(f'uploadFileByLabel: "{label}" 不是 upload 类型')
input_.set_input_files(file_path)
page.wait_for_timeout(500)