"""Offline Playwright Chromium verification for Bootstrap 5 List Groups (Ch 17)."""
import json
from playwright.sync_api import sync_playwright
HTML = """<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<style>
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 16px; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; line-height: 1.5; }
.list-group {
display: flex; flex-direction: column; padding-left: 0; margin: 0 0 12px 0;
border-radius: 0.375rem; list-style: none; width: 420px;
}
.list-group-item {
position: relative; display: block; padding: 0.5rem 1rem; color: #212529;
text-decoration: none; background-color: #fff; border: 1px solid rgba(0, 0, 0, 0.125);
}
.list-group-item:first-child { border-top-left-radius: inherit; border-top-right-radius: inherit; }
.list-group-item:last-child { border-bottom-right-radius: inherit; border-bottom-left-radius: inherit; }
.list-group-item + .list-group-item { border-top-width: 0; }
.list-group-item.active { z-index: 2; color: #fff; background-color: #0d6efd; border-color: #0d6efd; }
.list-group-item.disabled { color: #6c757d; pointer-events: none; background-color: #fff; }
.list-group-item-action { width: 100%; color: #495057; text-align: inherit; }
.list-group-item-action:hover { z-index: 1; color: #495057; background-color: #f8f9fa; }
.list-group-flush { border-radius: 0; }
.list-group-flush > .list-group-item { border-width: 0 0 1px; }
.list-group-flush > .list-group-item:last-child { border-bottom-width: 0; }
.list-group-horizontal { flex-direction: row; width: auto; }
.list-group-horizontal > .list-group-item + .list-group-item { border-top-width: 1px; border-left-width: 0; }
.list-group-item-primary { color: #084298; background-color: #cfe2ff; }
.list-group-item-secondary { color: #41464b; background-color: #e2e3e5; }
.list-group-item-success { color: #0f5132; background-color: #d1e7dd; }
.list-group-item-danger { color: #842029; background-color: #f8d7da; }
.list-group-item-warning { color: #664d03; background-color: #fff3cd; }
.list-group-item-info { color: #055160; background-color: #cff4fc; }
.list-group-item-light { color: #636464; background-color: #fefefe; }
.list-group-item-dark { color: #141619; background-color: #d3d3d4; }
.d-flex { display: flex; }
.gap-2 { gap: 0.5rem; }
</style>
</head>
<body>
<ul id="lg-basic" class="list-group">
<li id="li-active" class="list-group-item active">激活列表项</li>
<li id="li-disabled" class="list-group-item disabled">禁用项</li>
<li id="li-third" class="list-group-item">第三项</li>
</ul>
<div id="lg-links" class="list-group">
<a id="a-hover" href="#" class="list-group-item list-group-item-action">第一项</a>
<a href="#" class="list-group-item list-group-item-action">第二项</a>
</div>
<ul id="lg-flush" class="list-group list-group-flush">
<li id="fl-1" class="list-group-item">第一项</li>
<li id="fl-2" class="list-group-item">第二项</li>
</ul>
<ul id="lg-horiz" class="list-group list-group-horizontal">
<li id="hz-1" class="list-group-item">第一项</li>
<li id="hz-2" class="list-group-item">第二项</li>
<li id="hz-3" class="list-group-item">第三项</li>
<li id="hz-4" class="list-group-item">第四项</li>
</ul>
<ul id="lg-colors" class="list-group">
<li id="co-success" class="list-group-item list-group-item-success">成功列表项</li>
<li id="co-secondary" class="list-group-item list-group-item-secondary">次要列表项</li>
<li id="co-info" class="list-group-item list-group-item-info">信息列表项</li>
<li id="co-warning" class="list-group-item list-group-item-warning">警告列表项</li>
<li id="co-danger" class="list-group-item list-group-item-danger">危险列表项</li>
<li id="co-primary" class="list-group-item list-group-item-primary">主要列表项</li>
<li id="co-dark" class="list-group-item list-group-item-dark">深灰色列表项</li>
<li id="co-light" class="list-group-item list-group-item-light">浅色列表项</li>
</ul>
<div id="lg-case" class="list-group">
<label class="list-group-item d-flex gap-2">
<input id="cb-1" type="checkbox" checked> <span>First checkbox</span>
</label>
<label class="list-group-item d-flex gap-2">
<input id="cb-2" type="checkbox"> <span>Second checkbox</span>
</label>
</div>
<div id="lg-radio" class="list-group">
<label class="list-group-item d-flex gap-2">
<input id="rd-1" name="lgRadio" type="radio" checked> <span>First radio</span>
</label>
<label class="list-group-item d-flex gap-2">
<input id="rd-2" name="lgRadio" type="radio"> <span>Second radio</span>
</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():
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 900, "height": 900})
page.set_content(HTML, wait_until="load")
c1_actual = page.evaluate("""() => {
const g = getComputedStyle(document.getElementById("lg-basic"));
const act = getComputedStyle(document.getElementById("li-active"));
const dis = getComputedStyle(document.getElementById("li-disabled"));
const r1 = document.getElementById("li-active").getBoundingClientRect();
const r2 = document.getElementById("li-disabled").getBoundingClientRect();
return {
flex_direction: g.flexDirection,
stacked_vertically: r2.top >= r1.bottom - 1,
active_bg: act.backgroundColor,
active_color: act.color,
disabled_pointer_events: dis.pointerEvents,
disabled_color: dis.color,
second_border_top_width: dis.borderTopWidth,
};
}""")
c1_expected = {
"flex_direction": "column",
"stacked_vertically": True,
"active_bg": "rgb(13, 110, 253)",
"active_color": "rgb(255, 255, 255)",
"disabled_pointer_events": "none",
"disabled_color": "rgb(108, 117, 125)",
"second_border_top_width": "0px",
}
hover_before = page.evaluate('getComputedStyle(document.getElementById("a-hover")).backgroundColor')
page.locator("#a-hover").hover()
hover_after = page.evaluate('getComputedStyle(document.getElementById("a-hover")).backgroundColor')
c2_actual = page.evaluate("""([hb, ha]) => {
const fl = getComputedStyle(document.getElementById("lg-flush"));
const fl1 = getComputedStyle(document.getElementById("fl-1"));
const fl2 = getComputedStyle(document.getElementById("fl-2"));
const hz = getComputedStyle(document.getElementById("lg-horiz"));
const r1 = document.getElementById("hz-1").getBoundingClientRect();
const r4 = document.getElementById("hz-4").getBoundingClientRect();
return {
action_hover_before: hb,
action_hover_after: ha,
flush_radius: fl.borderTopLeftRadius,
flush_left_border: fl1.borderLeftWidth,
flush_last_bottom_border: fl2.borderBottomWidth,
horizontal_flex_dir: hz.flexDirection,
horizontal_same_row: Math.round(r1.top) === Math.round(r4.top) && r4.left > r1.right,
};
}""", [hover_before, hover_after])
c2_expected = {
"action_hover_before": "rgb(255, 255, 255)",
"action_hover_after": "rgb(248, 249, 250)",
"flush_radius": "0px",
"flush_left_border": "0px",
"flush_last_bottom_border": "0px",
"horizontal_flex_dir": "row",
"horizontal_same_row": True,
}
c3_actual = page.evaluate("""() => {
const bg = id => getComputedStyle(document.getElementById(id)).backgroundColor;
return {
success: bg("co-success"),
secondary: bg("co-secondary"),
info: bg("co-info"),
warning: bg("co-warning"),
danger: bg("co-danger"),
primary: bg("co-primary"),
dark: bg("co-dark"),
light: bg("co-light"),
};
}""")
c3_expected = {
"success": "rgb(209, 231, 221)",
"secondary": "rgb(226, 227, 229)",
"info": "rgb(207, 244, 252)",
"warning": "rgb(255, 243, 205)",
"danger": "rgb(248, 215, 218)",
"primary": "rgb(207, 226, 255)",
"dark": "rgb(211, 211, 212)",
"light": "rgb(254, 254, 254)",
}
page.click("#cb-2")
page.click("#rd-2")
c4_actual = page.evaluate("""() => ({
cb1_checked: document.getElementById("cb-1").checked,
cb2_checked: document.getElementById("cb-2").checked,
rd1_checked: document.getElementById("rd-1").checked,
rd2_checked: document.getElementById("rd-2").checked,
})""")
c4_expected = {
"cb1_checked": True,
"cb2_checked": True,
"rd1_checked": False,
"rd2_checked": True,
}
browser.close()
cases = [
dict(name="basic_active_disabled_items", description="核对 .list-group 纵向布局、边框合并及 .active/.disabled 项", expected=c1_expected, actual=c1_actual, passed=c1_actual == c1_expected),
dict(name="action_links_flush_and_horizontal", description="核对 .list-group-item-action 悬停背景、.list-group-flush 去外框及 .list-group-horizontal 横向排列", expected=c2_expected, actual=c2_actual, passed=c2_actual == c2_expected),
dict(name="contextual_item_colors", description="核对 8 种语义 .list-group-item-* 柔和背景色", expected=c3_expected, actual=c3_actual, passed=c3_actual == c3_expected),
dict(name="interactive_rich_list_group_case", description="核对复选框多选与单选框互斥列表组交互状态", expected=c4_expected, actual=c4_actual, passed=c4_actual == c4_expected),
]
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-list-groups-lab/explore-list-groups.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-list-groups.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a722.zip