"""Offline Playwright Chromium verification for Bootstrap 4 List Groups."""
import json
from playwright.sync_api import sync_playwright
CSS = """
*, *::before, *::after { box-sizing: border-box; }
body { margin: 16px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; font-size: 16px; line-height: 1.5; }
.list-group { display: flex; flex-direction: column; padding-left: 0; margin: 0 0 12px; border-radius: 0.25rem; width: 480px; }
.list-group-item {
position: relative; display: block; padding: 0.75rem 1.25rem;
background-color: #fff; border: 1px solid rgba(0, 0, 0, .125); text-decoration: none; color: #212529;
}
.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: #007bff; border-color: #007bff; }
.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, .list-group-item-action:focus { z-index: 1; color: #495057; text-decoration: none; background-color: #f8f9fa; }
.list-group-flush { border-radius: 0; }
.list-group-flush > .list-group-item { border-width: 0 0 1px; border-radius: 0; }
.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:first-child { border-bottom-left-radius: 0.25rem; border-top-right-radius: 0; }
.list-group-horizontal > .list-group-item:last-child { border-top-right-radius: 0.25rem; border-bottom-left-radius: 0; }
.list-group-horizontal > .list-group-item + .list-group-item { border-top-width: 1px; border-left-width: 0; }
.list-group-item-primary { color: #004085; background-color: #b8daff; }
.list-group-item-secondary { color: #383d41; background-color: #d6d8db; }
.list-group-item-success { color: #155724; background-color: #c3e6cb; }
.list-group-item-success.list-group-item-action:hover { color: #155724; background-color: #b1dfbb; }
.list-group-item-info { color: #0c5460; background-color: #bee5eb; }
.list-group-item-warning { color: #856404; background-color: #ffeeba; }
.list-group-item-danger { color: #721c24; background-color: #f5c6cb; }
.list-group-item-light { color: #818182; background-color: #fdfdfe; }
.list-group-item-dark { color: #1b1e21; background-color: #c6c8ca; }
"""
HTML = f"""<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><style>{CSS}</style></head>
<body>
<ul class="list-group" id="lg-basic">
<li class="list-group-item active" id="lg-act">Active item</li>
<li class="list-group-item disabled" id="lg-dis">Disabled item</li>
<li class="list-group-item" id="lg-norm">Third item</li>
</ul>
<div class="list-group" id="lg-links">
<a href="#" class="list-group-item list-group-item-action" id="lga-1">First item</a>
<a href="#" class="list-group-item list-group-item-action" id="lga-2">Second item</a>
</div>
<ul class="list-group list-group-flush" id="lg-flush">
<li class="list-group-item" id="fl-1">First</li>
<li class="list-group-item" id="fl-2">Second</li>
<li class="list-group-item" id="fl-3">Last</li>
</ul>
<ul class="list-group list-group-horizontal" id="lg-horiz">
<li class="list-group-item">First</li>
<li class="list-group-item">Second</li>
<li class="list-group-item">Third</li>
<li class="list-group-item">Fourth</li>
</ul>
<div class="list-group" id="lg-ctx">
<a href="#" class="list-group-item list-group-item-action list-group-item-success" id="ctx-success">成功</a>
<a href="#" class="list-group-item list-group-item-secondary" id="ctx-secondary">次要</a>
<a href="#" class="list-group-item list-group-item-info" id="ctx-info">信息</a>
<a href="#" class="list-group-item list-group-item-warning" id="ctx-warning">警告</a>
<a href="#" class="list-group-item list-group-item-danger" id="ctx-danger">危险</a>
<a href="#" class="list-group-item list-group-item-primary" id="ctx-primary">主要</a>
<a href="#" class="list-group-item list-group-item-dark" id="ctx-dark">深灰</a>
<a href="#" class="list-group-item list-group-item-light" id="ctx-light">浅色</a>
</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)
try:
page = browser.new_page(viewport={"width": 1000, "height": 900})
page.set_content(HTML, wait_until="load")
c1_actual = page.evaluate("""() => {
const ul = document.getElementById('lg-basic');
const act = getComputedStyle(document.getElementById('lg-act'));
const dis = getComputedStyle(document.getElementById('lg-dis'));
const rs = [...ul.children].map(el => el.getBoundingClientRect());
return {
display: getComputedStyle(ul).display,
flex_direction: getComputedStyle(ul).flexDirection,
stacked: rs[1].top > rs[0].top && rs[2].top > rs[1].top,
active_bg: act.backgroundColor,
active_color: act.color,
disabled_pointer_events: dis.pointerEvents,
disabled_color: dis.color
};
}""")
c1_exp = {
"display": "flex",
"flex_direction": "column",
"stacked": True,
"active_bg": "rgb(0, 123, 255)",
"active_color": "rgb(255, 255, 255)",
"disabled_pointer_events": "none",
"disabled_color": "rgb(108, 117, 125)",
}
before_hover = page.evaluate("() => getComputedStyle(document.getElementById('lga-1')).backgroundColor")
page.hover("#lga-1")
after_hover = page.evaluate("""() => {
const st = getComputedStyle(document.getElementById('lga-1'));
return { hover_bg: st.backgroundColor, text_decoration: st.textDecorationLine };
}""")
c2_actual = {"default_bg": before_hover, **after_hover}
c2_exp = {
"default_bg": "rgb(255, 255, 255)",
"hover_bg": "rgb(248, 249, 250)",
"text_decoration": "none",
}
c3_actual = page.evaluate("""() => {
const f1 = getComputedStyle(document.getElementById('fl-1'));
const f3 = getComputedStyle(document.getElementById('fl-3'));
const hz = document.getElementById('lg-horiz');
const hrs = [...hz.children].map(el => el.getBoundingClientRect());
return {
flush_side_borders: [f1.borderLeftWidth, f1.borderRightWidth, f1.borderTopWidth],
flush_last_bottom: f3.borderBottomWidth,
flush_radius: f1.borderTopLeftRadius,
horiz_direction: getComputedStyle(hz).flexDirection,
horiz_single_row: hrs.every((r, i) => Math.abs(r.top - hrs[0].top) < 1 && (i === 0 || r.left > hrs[i-1].left))
};
}""")
c3_exp = {
"flush_side_borders": ["0px", "0px", "0px"],
"flush_last_bottom": "0px",
"flush_radius": "0px",
"horiz_direction": "row",
"horiz_single_row": True,
}
ctx_before = page.evaluate("""() => {
const keys = ['success','secondary','info','warning','danger','primary','dark','light'];
const res = {};
for (const k of keys) {
const st = getComputedStyle(document.getElementById('ctx-'+k));
res[k] = [st.backgroundColor, st.color];
}
return res;
}""")
page.hover("#ctx-success")
ctx_success_hover = page.evaluate("() => getComputedStyle(document.getElementById('ctx-success')).backgroundColor")
c4_actual = {"colors": ctx_before, "success_action_hover_bg": ctx_success_hover}
c4_exp = {
"colors": {
"success": ["rgb(195, 230, 203)", "rgb(21, 87, 36)"],
"secondary": ["rgb(214, 216, 219)", "rgb(56, 61, 65)"],
"info": ["rgb(190, 229, 235)", "rgb(12, 84, 96)"],
"warning": ["rgb(255, 238, 186)", "rgb(133, 100, 4)"],
"danger": ["rgb(245, 198, 203)", "rgb(114, 28, 36)"],
"primary": ["rgb(184, 218, 255)", "rgb(0, 64, 133)"],
"dark": ["rgb(198, 200, 202)", "rgb(27, 30, 33)"],
"light": ["rgb(253, 253, 254)", "rgb(129, 129, 130)"],
},
"success_action_hover_bg": "rgb(177, 223, 187)",
}
cases = [
{"name": "basic_active_and_disabled_items", "input": "ul.list-group with .active & .disabled", "expected": c1_exp, "actual": c1_actual, "passed": c1_actual == c1_exp},
{"name": "action_links_and_hover_state", "input": "a.list-group-item.list-group-item-action hover", "expected": c2_exp, "actual": c2_actual, "passed": c2_actual == c2_exp},
{"name": "flush_and_horizontal_variants", "input": ".list-group-flush & .list-group-horizontal", "expected": c3_exp, "actual": c3_actual, "passed": c3_actual == c3_exp},
{"name": "contextual_colors_and_link_variants", "input": "8 contextual classes & hoverable success link", "expected": c4_exp, "actual": c4_actual, "passed": c4_actual == c4_exp},
]
print(json.dumps({"cases": cases}, ensure_ascii=False))
finally:
browser.close()
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/bootstrap4-list-groups-lab/explore-list-groups.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-list-groups.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a683.zip
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a664.zip