"""Offline Playwright Chromium verification for Bootstrap 5 Checkboxes, Radios, and Switches."""
import json
from playwright.sync_api import sync_playwright
CSS = """
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; font-size: 16px; line-height: 1.5; }
input { font-family: inherit; font-size: inherit; }
.form-check { display: block; min-height: 1.5rem; padding-left: 1.5em; margin-bottom: 0.125rem; }
.form-check-inline { display: inline-block; margin-right: 1rem; }
.form-check-input {
width: 1em; height: 1em; margin-top: 0.25em; vertical-align: top;
background-color: #fff; border: 1px solid rgba(0, 0, 0, 0.25); appearance: none;
float: left; margin-left: -1.5em;
}
.form-check-input[type="checkbox"] { border-radius: 0.25em; }
.form-check-input[type="radio"] { border-radius: 50%; }
.form-check-input:checked { background-color: #0d6efd; border-color: #0d6efd; }
.form-check-input:disabled { pointer-events: none; opacity: 0.5; }
.form-switch { padding-left: 2.5em; }
.form-switch .form-check-input {
width: 2em; margin-left: -2.5em; border-radius: 2em;
background-position: left center;
}
.form-switch .form-check-input:checked { background-position: right center; }
"""
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": 800, "height": 600})
# Case 1: checkboxes + label binding + disabled
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="check1" name="opt1" value="yes" checked>
<label id="lbl1" class="form-check-label" for="check1">Option 1</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="check2" name="opt2">
<label class="form-check-label" for="check2">Option 2</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="check3" disabled>
<label class="form-check-label" for="check3">Disabled</label>
</div></body></html>""")
before1 = page.is_checked("#check1")
page.click("#lbl1")
page.click("#check2")
act1 = page.evaluate("""(before1) => {
const c1 = document.getElementById('check1');
const c2 = document.getElementById('check2');
const c3 = document.getElementById('check3');
return {
c1_initial: before1,
c1_after_label_click: c1.checked,
c2_checked: c2.checked,
c2_bg: getComputedStyle(c2).backgroundColor,
c3_disabled: c3.disabled,
c3_opacity: getComputedStyle(c3).opacity,
};
}""", before1)
exp1 = {
"c1_initial": True,
"c1_after_label_click": False,
"c2_checked": True,
"c2_bg": "rgb(13, 110, 253)",
"c3_disabled": True,
"c3_opacity": "0.5",
}
cases.append({"name": "checkbox_states_and_label_binding", "expected": exp1, "actual": act1, "passed": act1 == exp1})
# Case 2: radio mutual exclusion
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div class="form-check">
<input type="radio" class="form-check-input" id="r1" name="optradio" value="option1" checked>
<label class="form-check-label" for="r1">Option 1</label>
</div>
<div class="form-check">
<input type="radio" class="form-check-input" id="r2" name="optradio" value="option2">
<label class="form-check-label" for="r2">Option 2</label>
</div>
<div class="form-check">
<input type="radio" class="form-check-input" id="r3" name="optradio" disabled>
<label class="form-check-label" for="r3">Option 3</label>
</div></body></html>""")
page.click("#r2")
act2 = page.evaluate("""() => {
const r1 = document.getElementById('r1');
const r2 = document.getElementById('r2');
const r3 = document.getElementById('r3');
return {
r1_checked: r1.checked,
r2_checked: r2.checked,
r2_radius: getComputedStyle(r2).borderRadius,
r2_bg: getComputedStyle(r2).backgroundColor,
r3_disabled: r3.disabled,
};
}""")
exp2 = {
"r1_checked": False,
"r2_checked": True,
"r2_radius": "50%",
"r2_bg": "rgb(13, 110, 253)",
"r3_disabled": True,
}
cases.append({"name": "radio_mutual_exclusion", "expected": exp2, "actual": act2, "passed": act2 == exp2})
# Case 3: form-switch
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="sw" name="darkmode" value="yes" checked>
<label class="form-check-label" for="sw">Dark Mode</label>
</div></body></html>""")
on_state = page.evaluate("""() => {
const sw = document.getElementById('sw');
const cs = getComputedStyle(sw);
return { checked: sw.checked, width: Math.round(sw.getBoundingClientRect().width), bg: cs.backgroundColor, bgPos: cs.backgroundPosition };
}""")
page.click("#sw")
off_state = page.evaluate("""() => {
const sw = document.getElementById('sw');
const cs = getComputedStyle(sw);
return { checked: sw.checked, bg: cs.backgroundColor, bgPos: cs.backgroundPosition };
}""")
act3 = {"on": on_state, "off": off_state}
exp3 = {
"on": {"checked": True, "width": 32, "bg": "rgb(13, 110, 253)", "bgPos": "100% 50%"},
"off": {"checked": False, "bg": "rgb(255, 255, 255)", "bgPos": "0% 50%"},
}
cases.append({"name": "switch_toggle_styles", "expected": exp3, "actual": act3, "passed": act3 == exp3})
# Case 4: form-check-inline vs stacked form-check
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div id="s1" class="form-check"><input class="form-check-input" type="checkbox"><label>S1</label></div>
<div id="s2" class="form-check"><input class="form-check-input" type="checkbox"><label>S2</label></div>
<div>
<div id="i1" class="form-check form-check-inline"><input class="form-check-input" type="checkbox"><label>I1</label></div>
<div id="i2" class="form-check form-check-inline"><input class="form-check-input" type="checkbox"><label>I2</label></div>
</div></body></html>""")
act4 = page.evaluate("""() => {
const rc = id => document.getElementById(id).getBoundingClientRect();
return {
stacked_vertical: rc('s2').top >= rc('s1').bottom,
inline_display: getComputedStyle(document.getElementById('i1')).display,
inline_same_top: Math.round(rc('i1').top) === Math.round(rc('i2').top),
inline_x2_gt_x1: rc('i2').left > rc('i1').right,
};
}""")
exp4 = {"stacked_vertical": True, "inline_display": "inline-block", "inline_same_top": True, "inline_x2_gt_x1": True}
cases.append({"name": "inline_check_layout", "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/bootstrap5-form-check-radio-lab/explore-form-check-radio.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-form-check-radio.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a711.zip