"""Offline Playwright Chromium verification lab for jEasyUI SplitButton (Ch13)."""
import json
from playwright.sync_api import sync_playwright
HTML_DOC = r"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jEasyUI SplitButton Lab</title>
<style>
body { margin: 12px; font-family: Arial, sans-serif; }
.toolbar { border: 1px solid #ccc; background: #fafafa; padding: 5px; width: 160px; display: flex; align-items: center; gap: 6px; }
.s-btn-wrap { display: inline-flex; border: 1px solid #bbb; background: #f2f2f2; border-radius: 3px; }
.s-btn-main { padding: 3px 8px; cursor: pointer; font-size: 13px; }
.s-btn-downarrow { padding: 3px 6px; border-left: 1px solid #ccc; cursor: pointer; font-size: 11px; }
.sb-menu { position: absolute; border: 1px solid #95b8e7; background: #fff; display: none; padding: 2px 0; }
.sb-menu .menu-item { padding: 4px 10px; cursor: pointer; position: relative; font-size: 13px; }
.sb-menu .menu-sep { height: 1px; background: #ccc; margin: 3px 0; }
.sb-menu .submenu { position: absolute; left: 100%; top: 0; border: 1px solid #95b8e7; background: #fff; display: none; }
</style>
</head>
<body>
<div class="toolbar">
<a id="sb-edit" href="javascript:void(0)" class="easyui-splitbutton" menu="#mm" iconCls="icon-edit">Edit</a>
<a id="lb-help" href="javascript:void(0)" class="easyui-linkbutton" plain="true" iconCls="icon-help"></a>
</div>
<div id="mm" style="width:150px;">
<div iconCls="icon-undo">Undo</div>
<div iconCls="icon-redo">Redo</div>
<div class="menu-sep"></div>
<div>Cut</div>
<div>Copy</div>
<div>Paste</div>
<div class="menu-sep"></div>
<div id="item-open">
<span>Open</span>
<div id="sub-open" style="width:150px;">
<div>Firefox</div>
<div>Internet Explorer</div>
<div class="menu-sep"></div>
<div>Select Program...</div>
</div>
</div>
<div iconCls="icon-remove">Delete</div>
<div>Select All</div>
</div>
<script>
window.__mainClicks = 0;
window.__menuPicks = [];
const sb = document.getElementById('sb-edit');
const menuEl = document.querySelector(sb.getAttribute('menu'));
menuEl.classList.add('sb-menu');
// Enhance declarative splitbutton into dual zones
const wrap = document.createElement('span');
wrap.className = 's-btn-wrap';
const mainZone = document.createElement('span');
mainZone.className = 's-btn-main';
mainZone.textContent = sb.textContent;
const arrowZone = document.createElement('span');
arrowZone.className = 's-btn-downarrow';
arrowZone.textContent = '▼';
wrap.appendChild(mainZone);
wrap.appendChild(arrowZone);
sb.innerHTML = '';
sb.appendChild(wrap);
mainZone.addEventListener('click', (e) => {
e.stopPropagation();
window.__mainClicks += 1;
});
function showMenu() {
const r = wrap.getBoundingClientRect();
menuEl.style.left = Math.round(r.left) + 'px';
menuEl.style.top = Math.round(r.bottom) + 'px';
menuEl.style.display = 'block';
}
arrowZone.addEventListener('click', (e) => { e.stopPropagation(); showMenu(); });
arrowZone.addEventListener('mouseenter', showMenu);
Array.from(menuEl.children).forEach(ch => {
if (ch.classList.contains('menu-sep')) return;
ch.classList.add('menu-item');
const sub = ch.querySelector(':scope > div');
if (sub) {
sub.classList.add('submenu');
ch.addEventListener('mouseenter', () => { sub.style.display = 'block'; });
ch.addEventListener('mouseleave', () => { sub.style.display = 'none'; });
Array.from(sub.children).forEach(subItem => {
if (!subItem.classList.contains('menu-sep')) {
subItem.classList.add('menu-item');
subItem.addEventListener('click', (e) => {
e.stopPropagation();
window.__menuPicks.push(subItem.textContent.trim());
sub.style.display = 'none';
menuEl.style.display = 'none';
});
}
});
} else {
ch.addEventListener('click', () => {
window.__menuPicks.push(ch.textContent.trim());
menuEl.style.display = 'none';
});
}
});
</script>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page()
page.set_content(HTML_DOC)
# Case 1: Dual-zone splitbutton structure + adjacent plain linkbutton
act1 = page.evaluate("""() => ({
mainZoneText: document.querySelector('#sb-edit .s-btn-main').textContent.trim(),
hasArrowZone: Boolean(document.querySelector('#sb-edit .s-btn-downarrow')),
menuAttr: document.getElementById('sb-edit').getAttribute('menu'),
helpLinkbuttonPlain: document.getElementById('lb-help').getAttribute('plain') === 'true',
helpLinkbuttonIcon: document.getElementById('lb-help').getAttribute('iconCls')
})""")
exp1 = {
"mainZoneText": "Edit",
"hasArrowZone": True,
"menuAttr": "#mm",
"helpLinkbuttonPlain": True,
"helpLinkbuttonIcon": "icon-help",
}
cases.append({
"name": "splitbutton_dual_zone_structure",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
})
# Case 2: Main zone click fires primary action without opening menu; arrow opens menu
page.click("#sb-edit .s-btn-main")
after_main = page.evaluate("""() => ({
mainClicks: window.__mainClicks,
menuDisplayAfterMain: getComputedStyle(document.getElementById('mm')).display
})""")
page.click("#sb-edit .s-btn-downarrow")
after_arrow = page.evaluate("""() => ({
mainClicksAfterArrow: window.__mainClicks,
menuDisplayAfterArrow: getComputedStyle(document.getElementById('mm')).display
})""")
act2 = dict(after_main, **after_arrow)
exp2 = {
"mainClicks": 1,
"menuDisplayAfterMain": "none",
"mainClicksAfterArrow": 1,
"menuDisplayAfterArrow": "block",
}
cases.append({
"name": "splitbutton_main_click_vs_arrow_menu",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: Hover Open reveals nested submenu; click Firefox records selection and hides menu
page.hover("#item-open")
sub_visible = page.evaluate("() => getComputedStyle(document.getElementById('sub-open')).display === 'block'")
page.locator("#sub-open .menu-item", has_text="Firefox").click()
act3 = page.evaluate("""(subVis) => ({
submenuVisibleOnHover: subVis,
pickedItems: window.__menuPicks.slice(),
menuClosedAfterPick: getComputedStyle(document.getElementById('mm')).display === 'none'
})""", sub_visible)
exp3 = {
"submenuVisibleOnHover": True,
"pickedItems": ["Firefox"],
"menuClosedAfterPick": True,
}
cases.append({
"name": "splitbutton_nested_submenu_selection",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/jeasyui-mb-splitbutton-lab/explore-mb-splitbutton.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-mb-splitbutton.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1010.zip