"""Offline Playwright Chromium lab for AngularJS Ch 14: html-dom."""
import json
from playwright.sync_api import sync_playwright
HTML_DOC = r"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>AngularJS HTML DOM Lab</title>
<style>.ng-hide { display: none !important; }</style>
</head>
<body>
<div id="sec-disabled">
<p><button id="btn-switch">点我!</button></p>
<p><input id="chk-switch" type="checkbox">按钮</p>
<p id="out-switch"></p>
</div>
<div id="sec-show-bool">
<p id="show-true">我是可见的。</p>
<p id="show-false">我是不可见的。</p>
</div>
<div id="sec-show-expr">
<p id="show-hour">我是可见的。</p>
</div>
<div id="sec-hide-bool">
<p id="hide-true">我是不可见的。</p>
<p id="hide-false">我是可见的。</p>
</div>
<script>
(function () {
const scope = { mySwitch: true, hour: 13 };
const btn = document.getElementById("btn-switch");
const chk = document.getElementById("chk-switch");
const out = document.getElementById("out-switch");
function applyNgShow(el, val) {
el.classList.toggle("ng-hide", !val);
}
function applyNgHide(el, val) {
el.classList.toggle("ng-hide", !!val);
}
function digest() {
btn.disabled = !!scope.mySwitch;
chk.checked = !!scope.mySwitch;
out.textContent = String(scope.mySwitch);
applyNgShow(document.getElementById("show-true"), true);
applyNgShow(document.getElementById("show-false"), false);
applyNgShow(document.getElementById("show-hour"), scope.hour > 12);
applyNgHide(document.getElementById("hide-true"), true);
applyNgHide(document.getElementById("hide-false"), false);
}
chk.addEventListener("change", () => {
scope.mySwitch = chk.checked;
digest();
});
window.setHour = (h) => { scope.hour = h; digest(); };
digest();
})();
</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)
try:
page = browser.new_page()
page.set_content(HTML_DOC)
# Case 1: ng-disabled bound to checkbox mySwitch
init_state = page.evaluate("""() => ({
disabled: document.getElementById("btn-switch").disabled,
hasDisabledAttr: document.getElementById("btn-switch").hasAttribute("disabled"),
checked: document.getElementById("chk-switch").checked,
text: document.getElementById("out-switch").textContent
})""")
page.click("#chk-switch")
toggled_state = page.evaluate("""() => ({
disabled: document.getElementById("btn-switch").disabled,
hasDisabledAttr: document.getElementById("btn-switch").hasAttribute("disabled"),
checked: document.getElementById("chk-switch").checked,
text: document.getElementById("out-switch").textContent
})""")
actual1 = {"initial": init_state, "after_uncheck": toggled_state}
expected1 = {
"initial": {"disabled": True, "hasDisabledAttr": True, "checked": True, "text": "true"},
"after_uncheck": {"disabled": False, "hasDisabledAttr": False, "checked": False, "text": "false"},
}
cases.append({"name": "ng_disabled_checkbox_binding", "expected": expected1, "actual": actual1, "passed": actual1 == expected1})
# Case 2: ng-show with boolean literals and expression hour > 12
expr_13 = page.evaluate("""() => ({
showTrueDisplay: getComputedStyle(document.getElementById("show-true")).display,
showFalseDisplay: getComputedStyle(document.getElementById("show-false")).display,
hour13Display: getComputedStyle(document.getElementById("show-hour")).display
})""")
page.evaluate("window.setHour(10)")
hour_10 = page.evaluate('getComputedStyle(document.getElementById("show-hour")).display')
actual2 = {**expr_13, "hour10Display": hour_10}
expected2 = {
"showTrueDisplay": "block",
"showFalseDisplay": "none",
"hour13Display": "block",
"hour10Display": "none",
}
cases.append({"name": "ng_show_boolean_and_expression", "expected": expected2, "actual": actual2, "passed": actual2 == expected2})
# Case 3: ng-hide boolean inverse visibility
actual3 = page.evaluate("""() => ({
hideTrueDisplay: getComputedStyle(document.getElementById("hide-true")).display,
hideTrueClass: document.getElementById("hide-true").classList.contains("ng-hide"),
hideFalseDisplay: getComputedStyle(document.getElementById("hide-false")).display,
hideFalseClass: document.getElementById("hide-false").classList.contains("ng-hide")
})""")
expected3 = {
"hideTrueDisplay": "none",
"hideTrueClass": True,
"hideFalseDisplay": "block",
"hideFalseClass": False,
}
cases.append({"name": "ng_hide_boolean_visibility", "expected": expected3, "actual": actual3, "passed": actual3 == expected3})
finally:
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/angularjs-html-dom-lab/explore-html-dom.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-html-dom.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联26章) · 含当前章节或实例 · mbskycom202612a851.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a860.zip