"""Offline Playwright Chromium lab for jQuery UI Ch 03 (download)."""
import json
from playwright.sync_api import sync_playwright
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(viewport={"width": 960, "height": 700})
# Case 1: Download Builder component dependency auto-selection
page.set_content("""<!DOCTYPE html><html><body>
<form id="builder">
<fieldset data-group="ui-core">
<input type="checkbox" value="core" />
<input type="checkbox" value="widget" />
<input type="checkbox" value="mouse" />
<input type="checkbox" value="position" />
</fieldset>
<fieldset data-group="interactions">
<input type="checkbox" value="draggable" />
<input type="checkbox" value="droppable" />
<input type="checkbox" value="resizable" />
<input type="checkbox" value="selectable" />
<input type="checkbox" value="sortable" />
</fieldset>
<fieldset data-group="widgets">
<input type="checkbox" value="accordion" />
<input type="checkbox" value="datepicker" />
<input type="checkbox" value="dialog" />
<input type="checkbox" value="slider" />
</fieldset>
<fieldset data-group="effects">
<input type="checkbox" value="effects-core" />
<input type="checkbox" value="fade" />
</fieldset>
</form>
<script>
const DEPS = {
mouse: ['core', 'widget'],
draggable: ['core', 'widget', 'mouse'],
droppable: ['draggable'],
resizable: ['core', 'widget', 'mouse'],
accordion: ['core', 'widget'],
datepicker: ['core'],
dialog: ['core', 'widget', 'position', 'draggable', 'resizable'],
fade: ['effects-core']
};
function selectWithDeps(name) {
const cb = document.querySelector(`input[value="${name}"]`);
if (!cb) return;
cb.checked = true;
(DEPS[name] || []).forEach(selectWithDeps);
}
document.querySelectorAll('#builder input[type="checkbox"]').forEach(cb => {
cb.addEventListener('change', () => { if (cb.checked) selectWithDeps(cb.value); });
});
</script></body></html>""")
page.check('input[value="droppable"]')
after_drop = page.evaluate("""() => [...document.querySelectorAll('#builder input:checked')].map(i => i.value).sort()""")
page.check('input[value="dialog"]')
after_dialog = page.evaluate("""() => [...document.querySelectorAll('#builder input:checked')].map(i => i.value).sort()""")
act1 = {"after_droppable": after_drop, "after_dialog": after_dialog, "group_count": page.locator("fieldset").count()}
exp1 = {
"after_droppable": ["core", "draggable", "droppable", "mouse", "widget"],
"after_dialog": ["core", "dialog", "draggable", "droppable", "mouse", "position", "resizable", "widget"],
"group_count": 4,
}
cases.append({
"name": "component_dependency_auto_select",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
})
# Case 2: Advanced Theme Settings - CSS Scope multi-theme isolation
page.set_content("""<!DOCTYPE html><html><head><style>
.theme-a .ui-widget-header { background: rgb(33, 145, 192); color: rgb(255, 255, 255); }
.theme-b .ui-widget-header { background: rgb(246, 168, 40); color: rgb(34, 34, 34); }
</style></head><body>
<div class="theme-a"><div id="h1" class="ui-widget-header">Scope A</div></div>
<div class="theme-b"><div id="h2" class="ui-widget-header">Scope B</div></div>
</body></html>""")
act2 = page.evaluate("""() => ({
scope_a_bg: getComputedStyle(document.getElementById('h1')).backgroundColor,
scope_a_color: getComputedStyle(document.getElementById('h1')).color,
scope_b_bg: getComputedStyle(document.getElementById('h2')).backgroundColor,
scope_b_color: getComputedStyle(document.getElementById('h2')).color
})""")
exp2 = {
"scope_a_bg": "rgb(33, 145, 192)",
"scope_a_color": "rgb(255, 255, 255)",
"scope_b_bg": "rgb(246, 168, 40)",
"scope_b_color": "rgb(34, 34, 34)",
}
cases.append({
"name": "scoped_multi_theme_css_isolation",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: Version selector & jQuery minimum requirement check
page.set_content("""<!DOCTYPE html><html><body>
<input type="radio" name="ver" value="1.10.2" data-min-jq="1.6" checked />
<input type="radio" name="ver" value="1.9.2" data-min-jq="1.6" />
<script>
function verGte(a, b) {
const pa = a.split('.').map(Number), pb = b.split('.').map(Number);
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
const da = pa[i] || 0, db = pb[i] || 0;
if (da !== db) return da > db;
}
return true;
}
window.checkCompat = (jqVer) => {
const sel = document.querySelector('input[name="ver"]:checked');
return { ui: sel.value, min_jq: sel.dataset.minJq, ok: verGte(jqVer, sel.dataset.minJq) };
};
</script></body></html>""")
c152 = page.evaluate("() => window.checkCompat('1.5.2')")
c164 = page.evaluate("() => window.checkCompat('1.6.4')")
page.check('input[value="1.9.2"]')
c192 = page.evaluate("() => window.checkCompat('1.10.2')")
act3 = {"jq_1_5_2": c152, "jq_1_6_4": c164, "ui_1_9_2_with_jq_1_10_2": c192}
exp3 = {
"jq_1_5_2": {"ui": "1.10.2", "min_jq": "1.6", "ok": False},
"jq_1_6_4": {"ui": "1.10.2", "min_jq": "1.6", "ok": True},
"ui_1_9_2_with_jq_1_10_2": {"ui": "1.9.2", "min_jq": "1.6", "ok": True},
}
cases.append({
"name": "version_jquery_compatibility_matrix",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
})
# Case 4: Custom zip package manifest verification
page.set_content("""<!DOCTYPE html><html><body>
<button id="dlBtn">Download</button><pre id="manifest"></pre>
<script>
document.getElementById('dlBtn').addEventListener('click', () => {
const theme = 'smoothness';
const manifest = [
`css/${theme}/jquery-ui.custom.css`,
'development-bundle/',
'js/jquery.min.js',
'js/jquery-ui.custom.min.js',
'index.html'
];
document.getElementById('manifest').textContent = JSON.stringify(manifest);
});
</script></body></html>""")
page.click("#dlBtn")
act4 = json.loads(page.locator("#manifest").inner_text())
exp4 = [
"css/smoothness/jquery-ui.custom.css",
"development-bundle/",
"js/jquery.min.js",
"js/jquery-ui.custom.min.js",
"index.html",
]
cases.append({
"name": "custom_bundle_manifest_generation",
"expected": exp4,
"actual": act4,
"passed": act4 == exp4,
})
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/jqueryui-download-lab/explore-download.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-download.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联43章) · 含当前章节或实例 · mbskycom202612a916.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a918.zip