"""Offline Playwright Chromium verification for Bootstrap 5 Forms."""
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; }
.wrap { width: 600px; }
.mb-3 { margin-bottom: 1rem; }
.mt-3 { margin-top: 1rem; }
.form-label { display: inline-block; margin-bottom: 0.5rem; }
.form-control {
display: block; width: 100%; padding: 0.375rem 0.75rem;
font-size: 1rem; font-weight: 400; line-height: 1.5; color: #212529;
background-color: #fff; border: 1px solid #ced4da; border-radius: 0.375rem;
}
.form-control:disabled, .form-control[readonly] { background-color: #e9ecef; opacity: 1; }
.form-control-lg { min-height: calc(1.5em + 1rem + 2px); padding: 0.5rem 1rem; font-size: 1.25rem; border-radius: 0.5rem; }
.form-control-sm { min-height: calc(1.5em + 0.5rem + 2px); padding: 0.25rem 0.5rem; font-size: 0.875rem; border-radius: 0.25rem; }
.form-control-plaintext {
display: block; width: 100%; padding: 0.375rem 0; margin-bottom: 0;
line-height: 1.5; color: #212529; background-color: transparent;
border: solid transparent; border-width: 1px 0;
}
.form-control-color { width: 3rem; height: calc(1.5em + 0.75rem + 2px); padding: 0.375rem; }
.form-check { display: block; min-height: 1.5rem; padding-left: 1.5em; margin-bottom: 0.125rem; }
.form-check-input { float: left; margin-left: -1.5em; width: 1em; height: 1em; margin-top: 0.25em; }
.row { display: flex; flex-wrap: wrap; gap: 16px; }
.col { flex: 1 0 0%; }
.btn { display: inline-block; padding: 0.375rem 0.75rem; border: 1px solid #0d6efd; background: #0d6efd; color: #fff; border-radius: 0.375rem; }
"""
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": 800})
# Case 1: Stacked form + Inline row/col form
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div class="wrap">
<form id="stacked">
<div class="mb-3 mt-3">
<label id="lbl" for="email" class="form-label">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter email">
</div>
<div class="mb-3">
<label for="pwd" class="form-label">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password">
</div>
<div class="form-check mb-3">
<label class="form-check-label">
<input id="rem" class="form-check-input" type="checkbox"> Remember me
</label>
</div>
<button type="submit" class="btn">Submit</button>
</form>
<form id="inline_form">
<div class="row">
<div class="col"><input id="in1" type="text" class="form-control" placeholder="Email"></div>
<div class="col"><input id="in2" type="password" class="form-control" placeholder="Password"></div>
</div>
</form>
</div></body></html>""")
page.click("#rem")
act1 = page.evaluate("""() => {
const rc = id => document.getElementById(id).getBoundingClientRect();
const cs = id => getComputedStyle(document.getElementById(id));
return {
email_width: Math.round(rc('email').width),
label_mb: cs('lbl').marginBottom,
stacked_vertical: rc('pwd').top > rc('email').bottom,
remember_checked: document.getElementById('rem').checked,
inline_same_top: Math.round(rc('in1').top) === Math.round(rc('in2').top),
inline_side_by_side: rc('in2').left > rc('in1').right,
inline_widths: [Math.round(rc('in1').width), Math.round(rc('in2').width)],
};
}""")
exp1 = {
"email_width": 600,
"label_mb": "8px",
"stacked_vertical": True,
"remember_checked": True,
"inline_same_top": True,
"inline_side_by_side": True,
"inline_widths": [292, 292],
}
cases.append({"name": "stacked_and_inline_form", "expected": exp1, "actual": act1, "passed": act1 == exp1})
# Case 2: Textarea and input sizes (lg, default, sm)
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div class="wrap">
<textarea id="ta" class="form-control" rows="5"></textarea>
<input id="lg" type="text" class="form-control form-control-lg" placeholder="Large">
<input id="md" type="text" class="form-control" placeholder="Default">
<input id="sm" type="text" class="form-control form-control-sm" placeholder="Small">
</div></body></html>""")
act2 = page.evaluate("""() => {
const rc = id => document.getElementById(id).getBoundingClientRect();
const cs = id => getComputedStyle(document.getElementById(id));
return {
ta_width: Math.round(rc('ta').width),
ta_taller_than_md: rc('ta').height > rc('md').height * 2,
font_sizes: [cs('lg').fontSize, cs('md').fontSize, cs('sm').fontSize],
heights_strict_order: rc('lg').height > rc('md').height && rc('md').height > rc('sm').height,
heights_px: [Math.round(rc('lg').height), Math.round(rc('md').height), Math.round(rc('sm').height)],
};
}""")
exp2 = {
"ta_width": 600,
"ta_taller_than_md": True,
"font_sizes": ["20px", "16px", "14px"],
"heights_strict_order": True,
"heights_px": [48, 38, 31],
}
cases.append({"name": "textarea_and_input_sizes", "expected": exp2, "actual": act2, "passed": act2 == exp2})
# Case 3: disabled vs readonly
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div class="wrap">
<input id="norm" type="text" class="form-control" value="">
<input id="dis" type="text" class="form-control" value="Locked" disabled>
<input id="ro" type="text" class="form-control" value="Fixed" readonly>
</div></body></html>""")
page.fill("#norm", "hello")
page.focus("#ro")
page.keyboard.type("extra")
act3 = page.evaluate("""() => {
const cs = id => getComputedStyle(document.getElementById(id));
return {
norm_val: document.getElementById('norm').value,
dis_flag: document.getElementById('dis').disabled,
dis_bg: cs('dis').backgroundColor,
ro_flag: document.getElementById('ro').readOnly,
ro_bg: cs('ro').backgroundColor,
ro_focused: document.activeElement.id === 'ro',
ro_val_unchanged: document.getElementById('ro').value === 'Fixed',
};
}""")
exp3 = {
"norm_val": "hello",
"dis_flag": True,
"dis_bg": "rgb(233, 236, 239)",
"ro_flag": True,
"ro_bg": "rgb(233, 236, 239)",
"ro_focused": True,
"ro_val_unchanged": True,
}
cases.append({"name": "disabled_and_readonly_states", "expected": exp3, "actual": act3, "passed": act3 == exp3})
# Case 4: form-control-plaintext and form-control-color
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div class="wrap">
<input id="pt" type="text" readonly class="form-control-plaintext" value="email@example.com">
<input id="std" type="text" class="form-control" value="standard">
<input id="clr" type="color" class="form-control form-control-color" value="#cccccc">
</div></body></html>""")
act4 = page.evaluate("""() => {
const cs = id => getComputedStyle(document.getElementById(id));
const clr = document.getElementById('clr');
const before = clr.value;
clr.value = '#0d6efd';
return {
pt_bg: cs('pt').backgroundColor,
pt_border_left: cs('pt').borderLeftWidth,
pt_padding_left: cs('pt').paddingLeft,
std_border_left: cs('std').borderLeftWidth,
color_width: Math.round(clr.getBoundingClientRect().width),
color_initial: before,
color_updated: clr.value,
};
}""")
exp4 = {
"pt_bg": "rgba(0, 0, 0, 0)",
"pt_border_left": "0px",
"pt_padding_left": "0px",
"std_border_left": "1px",
"color_width": 48,
"color_initial": "#cccccc",
"color_updated": "#0d6efd",
}
cases.append({"name": "plaintext_and_color_picker", "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-forms-lab/explore-forms.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-forms.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a717.zip