"""Offline Playwright Chromium lab for Foundation Ch 25 (switches)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 16px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; }
.switch { position: relative; margin-bottom: 16px; padding: 0; border: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch label {
display: block; position: relative; cursor: pointer;
width: 64px; height: 32px; background: #cccccc; border-radius: 0px;
}
.switch label::after {
content: ""; display: block; position: absolute;
top: 4px; left: 4px; width: 24px; height: 24px;
background: #ffffff; border-radius: 0px;
}
.switch input:checked + label { background: #008cba; }
.switch input:checked + label::after { left: 36px; }
.switch.large label { width: 80px; height: 40px; }
.switch.large label::after { width: 32px; height: 32px; top: 4px; left: 4px; }
.switch.large input:checked + label::after { left: 44px; }
.switch.small label { width: 56px; height: 28px; }
.switch.small label::after { width: 20px; height: 20px; top: 4px; left: 4px; }
.switch.small input:checked + label::after { left: 32px; }
.switch.tiny label { width: 48px; height: 24px; }
.switch.tiny label::after { width: 16px; height: 16px; top: 4px; left: 4px; }
.switch.tiny input:checked + label::after { left: 28px; }
.switch.radius label { border-radius: 4px; }
.switch.radius label::after { border-radius: 3px; }
.switch.round label { border-radius: 1000px; }
.switch.round label::after { border-radius: 1000px; }
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_CSS}</style></head>
<body>
<div class="switch"><input id="mySwitch" type="checkbox"><label id="lbl-basic" for="mySwitch"></label></div>
<div class="switch large"><input id="swLarge" type="checkbox"><label id="lbl-large" for="swLarge"></label></div>
<div class="switch"><input id="swDef" type="checkbox"><label id="lbl-def" for="swDef"></label></div>
<div class="switch small"><input id="swSmall" type="checkbox"><label id="lbl-small" for="swSmall"></label></div>
<div class="switch tiny"><input id="swTiny" type="checkbox"><label id="lbl-tiny" for="swTiny"></label></div>
<div class="switch radius"><input id="swRad" type="checkbox"><label id="lbl-rad" for="swRad"></label></div>
<div class="switch round"><input id="swRnd" type="checkbox"><label id="lbl-rnd" for="swRnd"></label></div>
<div class="switch"><input id="mySwitch1" type="radio" name="myGroup"><label id="lbl-r1" for="mySwitch1"></label></div>
<div class="switch"><input id="mySwitch2" type="radio" name="myGroup" checked><label id="lbl-r2" for="mySwitch2"></label></div>
</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(viewport={"width": 600, "height": 600})
page.set_content(HTML_PAGE)
# Case 1: basic checkbox switch toggle via label for/id
def read_basic():
return page.evaluate("""() => {
const inp = document.getElementById('mySwitch');
const lbl = document.getElementById('lbl-basic');
return {
checked: inp.checked,
track_bg: getComputedStyle(lbl).backgroundColor,
knob_left: getComputedStyle(lbl, '::after').left,
};
}""")
off_state = read_basic()
page.click("#lbl-basic")
on_state = read_basic()
page.click("#lbl-basic")
off_again = read_basic()
c1 = {"off": off_state, "on": on_state, "off_again": off_again}
exp1 = {
"off": {"checked": False, "track_bg": "rgb(204, 204, 204)", "knob_left": "4px"},
"on": {"checked": True, "track_bg": "rgb(0, 140, 186)", "knob_left": "36px"},
"off_again": {"checked": False, "track_bg": "rgb(204, 204, 204)", "knob_left": "4px"},
}
cases.append(dict(name="checkbox_switch_toggle_and_label_for", passed=(c1 == exp1), expected=exp1, actual=c1))
# Case 2: four switch sizes
c2 = page.evaluate("""() => {
const ids = ['lbl-large', 'lbl-def', 'lbl-small', 'lbl-tiny'];
const dims = {};
for (const id of ids) {
const r = document.getElementById(id).getBoundingClientRect();
dims[id.replace('lbl-', '')] = [Math.round(r.width), Math.round(r.height)];
}
dims.monotonic = dims.large[0] > dims.def[0] && dims.def[0] > dims.small[0] && dims.small[0] > dims.tiny[0];
return dims;
}""")
exp2 = {
"large": [80, 40],
"def": [64, 32],
"small": [56, 28],
"tiny": [48, 24],
"monotonic": True,
}
cases.append(dict(name="switch_four_sizes", passed=(c2 == exp2), expected=exp2, actual=c2))
# Case 3: default vs radius vs round corners
c3 = page.evaluate("""() => ({
default_track: getComputedStyle(document.getElementById('lbl-def')).borderRadius,
default_knob: getComputedStyle(document.getElementById('lbl-def'), '::after').borderRadius,
radius_track: getComputedStyle(document.getElementById('lbl-rad')).borderRadius,
radius_knob: getComputedStyle(document.getElementById('lbl-rad'), '::after').borderRadius,
round_track: getComputedStyle(document.getElementById('lbl-rnd')).borderRadius,
round_knob: getComputedStyle(document.getElementById('lbl-rnd'), '::after').borderRadius,
})""")
exp3 = {
"default_track": "0px",
"default_knob": "0px",
"radius_track": "4px",
"radius_knob": "3px",
"round_track": "1000px",
"round_knob": "1000px",
}
cases.append(dict(name="switch_radius_and_round_corners", passed=(c3 == exp3), expected=exp3, actual=c3))
# Case 4: radio switch group mutual exclusion
init_r = page.evaluate("() => [document.getElementById('mySwitch1').checked, document.getElementById('mySwitch2').checked]")
page.click("#lbl-r1")
after_r1 = page.evaluate("() => [document.getElementById('mySwitch1').checked, document.getElementById('mySwitch2').checked]")
page.click("#lbl-r1")
after_r1_again = page.evaluate("() => [document.getElementById('mySwitch1').checked, document.getElementById('mySwitch2').checked]")
c4 = {
"shared_name": page.evaluate("() => document.getElementById('mySwitch1').name === document.getElementById('mySwitch2').name && document.getElementById('mySwitch1').name === 'myGroup'"),
"initial": init_r,
"after_click_1": after_r1,
"after_click_1_again": after_r1_again,
}
exp4 = {
"shared_name": True,
"initial": [False, True],
"after_click_1": [True, False],
"after_click_1_again": [True, False],
}
cases.append(dict(name="radio_switch_group_mutual_exclusion", passed=(c4 == exp4), expected=exp4, actual=c4))
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/foundation-switches-lab/explore-switches.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-switches.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a769.zip