"""Offline Playwright Chromium lab for Bootstrap 4 Chapter 24 (forms-inputs)."""
import json
from playwright.sync_api import sync_playwright
BS4_INPUTS_CSS = """
* { box-sizing: border-box; }
body { margin: 16px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; font-size: 16px; line-height: 1.5; }
.form-group { margin-bottom: 1rem; width: 480px; }
.form-control {
display: block; width: 100%; height: 38px; padding: 0.375rem 0.75rem;
font-size: 1rem; line-height: 1.5; color: #495057; background-color: #fff;
border: 1px solid #ced4da; border-radius: 0.25rem;
}
textarea.form-control, select[multiple].form-control { height: auto; }
.form-check { position: relative; display: block; padding-left: 1.25rem; }
.form-check-input { position: absolute; margin-top: 0.3rem; margin-left: -1.25rem; }
.form-check-inline { display: inline-flex; align-items: center; padding-left: 0; margin-right: 0.75rem; }
.form-check-inline .form-check-input { position: static; margin-top: 0; margin-right: 0.3125rem; margin-left: 0; }
.radio { position: relative; display: block; margin-top: 0.25rem; margin-bottom: 0.25rem; }
.radio-inline { display: inline-block; padding-right: 0.75rem; vertical-align: middle; }
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def run():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 800, "height": 700})
page.set_content(f"""<!DOCTYPE html><html><head><style>{BS4_INPUTS_CSS}</style></head><body>
<div class="form-group">
<label for="usr">用户名:</label>
<input type="text" class="form-control" id="usr">
</div>
<div class="form-group">
<label for="pwd">密码:</label>
<input type="password" class="form-control" id="pwd">
</div>
<div class="form-group">
<label for="comment">评论:</label>
<textarea class="form-control" rows="5" id="comment"></textarea>
</div>
<div id="cb-stacked">
<div class="form-check"><label class="form-check-label"><input id="c1" type="checkbox" class="form-check-input" value="1">Option 1</label></div>
<div class="form-check"><label class="form-check-label"><input id="c2" type="checkbox" class="form-check-input" value="2">Option 2</label></div>
<div class="form-check disabled"><label class="form-check-label"><input id="c3" type="checkbox" class="form-check-input" value="3" disabled>Option 3</label></div>
</div>
<div id="cb-inline">
<div class="form-check form-check-inline"><label class="form-check-label"><input type="checkbox" class="form-check-input">Option 1</label></div>
<div class="form-check form-check-inline"><label class="form-check-label"><input type="checkbox" class="form-check-input">Option 2</label></div>
<div class="form-check form-check-inline disabled"><label class="form-check-label"><input type="checkbox" class="form-check-input" disabled>Option 3</label></div>
</div>
<div id="rd-stacked">
<div class="radio"><label><input id="r1" type="radio" name="optradio" value="1">Option 1</label></div>
<div class="radio"><label><input id="r2" type="radio" name="optradio" value="2">Option 2</label></div>
<div class="radio disabled"><label><input id="r3" type="radio" name="optradio" value="3" disabled>Option 3</label></div>
</div>
<div id="rd-inline">
<label class="radio-inline"><input type="radio" name="optinline">Option 1</label>
<label class="radio-inline"><input type="radio" name="optinline">Option 2</label>
<label class="radio-inline"><input type="radio" name="optinline" disabled>Option 3</label>
</div>
<div class="form-group">
<label for="sel1">下拉菜单:</label>
<select class="form-control" id="sel1"><option>1</option><option>2</option><option>3</option><option>4</option></select>
</div>
<div class="form-group">
<label for="sel2">多选下拉:</label>
<select multiple class="form-control" id="sel2"><option>1</option><option>2</option><option>3</option><option>4</option></select>
</div>
</body></html>""")
# Case 1: text/password inputs and textarea
actual1 = page.evaluate("""() => {
const usr = document.querySelector('#usr');
const pwd = document.querySelector('#pwd');
const ta = document.querySelector('#comment');
return {
usr_type: usr.type,
pwd_type: pwd.type,
usr_height_px: Math.round(usr.getBoundingClientRect().height),
usr_width_px: Math.round(usr.getBoundingClientRect().width),
textarea_rows: ta.rows,
textarea_taller_than_input: ta.getBoundingClientRect().height > usr.getBoundingClientRect().height,
};
}""")
expected1 = {
"usr_type": "text",
"pwd_type": "password",
"usr_height_px": 38,
"usr_width_px": 480,
"textarea_rows": 5,
"textarea_taller_than_input": True,
}
cases.append(dict(
name="input_types_and_textarea",
expected=expected1,
actual=actual1,
passed=(actual1 == expected1),
))
# Case 2: checkbox stacked & inline + multi-select + disabled
page.check("#c1")
page.check("#c2")
actual2 = page.evaluate("""() => {
const s = Array.from(document.querySelectorAll('#cb-stacked .form-check'));
const inl = Array.from(document.querySelectorAll('#cb-inline .form-check-inline'));
return {
stacked_vertical: s[1].getBoundingClientRect().top > s[0].getBoundingClientRect().top,
inline_display: getComputedStyle(inl[0]).display,
inline_same_row: Math.abs(inl[0].getBoundingClientRect().top - inl[1].getBoundingClientRect().top) <= 1,
multi_checked: [document.querySelector('#c1').checked, document.querySelector('#c2').checked],
third_disabled_unchecked: document.querySelector('#c3').disabled && !document.querySelector('#c3').checked,
};
}""")
expected2 = {
"stacked_vertical": True,
"inline_display": "inline-flex",
"inline_same_row": True,
"multi_checked": [True, True],
"third_disabled_unchecked": True,
}
cases.append(dict(
name="checkbox_stacked_and_inline",
expected=expected2,
actual=actual2,
passed=(actual2 == expected2),
))
# Case 3: radio mutual exclusion, stacked & inline
page.check("#r1")
first_pick = [page.is_checked("#r1"), page.is_checked("#r2")]
page.check("#r2")
actual3 = page.evaluate("""(firstPick) => {
const s = Array.from(document.querySelectorAll('#rd-stacked .radio'));
const inl = Array.from(document.querySelectorAll('#rd-inline .radio-inline'));
return {
first_pick: firstPick,
after_second_pick: [document.querySelector('#r1').checked, document.querySelector('#r2').checked],
third_disabled: document.querySelector('#r3').disabled,
stacked_vertical: s[1].getBoundingClientRect().top > s[0].getBoundingClientRect().top,
inline_same_row: Math.abs(inl[0].getBoundingClientRect().top - inl[1].getBoundingClientRect().top) <= 1,
};
}""", first_pick)
expected3 = {
"first_pick": [True, False],
"after_second_pick": [False, True],
"third_disabled": True,
"stacked_vertical": True,
"inline_same_row": True,
}
cases.append(dict(
name="radio_stacked_and_inline",
expected=expected3,
actual=actual3,
passed=(actual3 == expected3),
))
# Case 4: select single and multiple
page.select_option("#sel1", "3")
page.select_option("#sel2", ["1", "3"])
actual4 = page.evaluate("""() => {
const s1 = document.querySelector('#sel1');
const s2 = document.querySelector('#sel2');
return {
single_height_px: Math.round(s1.getBoundingClientRect().height),
single_value: s1.value,
multiple_selected: Array.from(s2.selectedOptions).map(o => o.value),
multiple_taller: s2.getBoundingClientRect().height > s1.getBoundingClientRect().height,
};
}""")
expected4 = {
"single_height_px": 38,
"single_value": "3",
"multiple_selected": ["1", "3"],
"multiple_taller": True,
}
cases.append(dict(
name="select_single_and_multiple",
expected=expected4,
actual=actual4,
passed=(actual4 == expected4),
))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
run()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/bootstrap4-forms-inputs-lab/explore-forms-inputs.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-forms-inputs.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a678.zip
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a664.zip