"""Offline Playwright Chromium verification lab for jEasyUI MenuButton (Ch12)."""
import json
from playwright.sync_api import sync_playwright
HTML_DOC = r"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jEasyUI MenuButton Lab</title>
<style>
body { margin: 12px; font-family: Arial, sans-serif; }
.toolbar { background: #fafafa; padding: 5px; width: 200px; border: 1px solid #ccc; }
a.easyui-menubutton { display: inline-block; padding: 3px 8px; text-decoration: none; color: #333; margin-right: 6px; border: 1px solid transparent; }
a.easyui-menubutton.m-btn-active { border-color: #95b8e7; background: #e0ecff; }
.mb-menu { position: absolute; border: 1px solid #95b8e7; background: #fff; display: none; padding: 2px 0; }
.mb-menu .menu-item { padding: 4px 10px; cursor: pointer; font-size: 13px; }
.mb-menu .menu-sep { height: 1px; background: #ccc; margin: 3px 0; }
</style>
</head>
<body>
<div class="toolbar">
<a id="mb-edit" href="javascript:void(0)" class="easyui-menubutton" menu="#mm1" iconCls="icon-edit">Edit</a>
<a id="mb-help" href="javascript:void(0)" class="easyui-menubutton" menu="#mm2" iconCls="icon-help">Help</a>
</div>
<div id="mm1" 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 iconCls="icon-remove">Delete</div>
<div>Select All</div>
</div>
<div id="mm2" style="width:100px;">
<div>Help</div>
<div>Update</div>
<div>About</div>
</div>
<script>
window.__selectedItems = [];
const allMenus = [];
const allBtns = Array.from(document.querySelectorAll('a.easyui-menubutton'));
function hideAll() {
allMenus.forEach(m => { m.style.display = 'none'; });
allBtns.forEach(b => b.classList.remove('m-btn-active'));
}
allBtns.forEach(btn => {
const targetSel = btn.getAttribute('menu');
const menuEl = document.querySelector(targetSel);
menuEl.classList.add('mb-menu');
allMenus.push(menuEl);
Array.from(menuEl.children).forEach(ch => {
if (!ch.classList.contains('menu-sep')) {
ch.classList.add('menu-item');
ch.addEventListener('click', () => {
window.__selectedItems.push(ch.textContent.trim());
hideAll();
});
}
});
function openMenu() {
hideAll();
const r = btn.getBoundingClientRect();
menuEl.style.left = Math.round(r.left) + 'px';
menuEl.style.top = Math.round(r.bottom) + 'px';
menuEl.style.display = 'block';
btn.classList.add('m-btn-active');
}
btn.addEventListener('mouseenter', openMenu);
btn.addEventListener('click', openMenu);
});
</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: Declarative menu binding & initial hidden state
act1 = page.evaluate("""() => ({
editMenuTarget: document.getElementById('mb-edit').getAttribute('menu'),
helpMenuTarget: document.getElementById('mb-help').getAttribute('menu'),
mm1Items: document.querySelectorAll('#mm1 .menu-item').length,
mm1Seps: document.querySelectorAll('#mm1 .menu-sep').length,
mm2Items: document.querySelectorAll('#mm2 .menu-item').length,
bothInitiallyHidden: getComputedStyle(document.getElementById('mm1')).display === 'none'
&& getComputedStyle(document.getElementById('mm2')).display === 'none'
})""")
exp1 = {
"editMenuTarget": "#mm1",
"helpMenuTarget": "#mm2",
"mm1Items": 7,
"mm1Seps": 2,
"mm2Items": 3,
"bothInitiallyHidden": True,
}
cases.append({
"name": "menubutton_declarative_binding",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
})
# Case 2: Hover/click Edit opens #mm1, clicking Copy logs action and hides menu
page.click("#mb-edit")
mm1_open = page.evaluate("() => getComputedStyle(document.getElementById('mm1')).display === 'block'")
page.locator("#mm1 .menu-item", has_text="Copy").click()
act2 = page.evaluate("""(wasOpen) => ({
mm1OpenedOnClick: wasOpen,
selectedItems: window.__selectedItems.slice(),
mm1HiddenAfterSelect: getComputedStyle(document.getElementById('mm1')).display === 'none'
})""", mm1_open)
exp2 = {
"mm1OpenedOnClick": True,
"selectedItems": ["Copy"],
"mm1HiddenAfterSelect": True,
}
cases.append({
"name": "menubutton_hover_and_click_dropdown",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: Switching from Edit to Help closes #mm1 and opens #mm2
page.hover("#mb-edit")
page.hover("#mb-help")
act3 = page.evaluate("""() => ({
mm1Display: getComputedStyle(document.getElementById('mm1')).display,
mm2Display: getComputedStyle(document.getElementById('mm2')).display,
mm2Texts: Array.from(document.querySelectorAll('#mm2 .menu-item')).map(x => x.textContent.trim())
})""")
exp3 = {
"mm1Display": "none",
"mm2Display": "block",
"mm2Texts": ["Help", "Update", "About"],
}
cases.append({
"name": "menubutton_switch_between_menus",
"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-menubutton-lab/explore-mb-menubutton.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-mb-menubutton.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1009.zip