"""Offline Playwright Chromium verification for Bootstrap 4 Navs, Tabs & Pills."""
import json
from playwright.sync_api import sync_playwright
CSS = """
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; font-size: 16px; line-height: 1.5; }
.wrap { width: 800px; }
.nav { display: flex; flex-wrap: wrap; padding-left: 0; margin: 0 0 12px; list-style: none; }
.nav-link { display: block; padding: 0.5rem 1rem; color: #007bff; text-decoration: none; cursor: pointer; }
.nav-link.disabled { color: #6c757d; pointer-events: none; cursor: default; }
.justify-content-center { justify-content: center !important; }
.justify-content-end { justify-content: flex-end !important; }
.flex-column { flex-direction: column !important; }
.nav-tabs { border-bottom: 1px solid #dee2e6; }
.nav-tabs .nav-item { margin-bottom: -1px; }
.nav-tabs .nav-link { border: 1px solid transparent; border-top-left-radius: 0.25rem; border-top-right-radius: 0.25rem; }
.nav-tabs .nav-link.active { color: #495057; background-color: #fff; border-color: #dee2e6 #dee2e6 #fff; }
.nav-pills .nav-link { border-radius: 0.25rem; }
.nav-pills .nav-link.active { color: #fff; background-color: #007bff; }
.nav-justified .nav-item { flex-basis: 0; flex-grow: 1; text-align: center; }
.dropdown { position: relative; }
.dropdown-menu {
position: absolute; top: 100%; left: 0; z-index: 1000; display: none; min-width: 10rem;
padding: 0.5rem 0; background-color: #fff; border: 1px solid rgba(0,0,0,.15); border-radius: 0.25rem;
}
.dropdown-menu.show { display: block; }
.dropdown-item { display: block; padding: 0.25rem 1.5rem; color: #212529; text-decoration: none; }
.tab-content > .tab-pane { display: none; }
.tab-content > .active { display: block; }
.fade:not(.show) { opacity: 0; }
.fade.show { opacity: 1; }
"""
HTML = f"""<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><style>{CSS}</style></head>
<body>
<div class="wrap">
<ul class="nav" id="n-basic">
<li class="nav-item"><a class="nav-link" href="#">Link 1</a></li>
<li class="nav-item"><a class="nav-link disabled" id="n-dis" href="#">Disabled</a></li>
</ul>
<ul class="nav justify-content-center" id="n-center">
<li class="nav-item"><a class="nav-link" href="#">A</a></li>
<li class="nav-item"><a class="nav-link" href="#">B</a></li>
</ul>
<ul class="nav justify-content-end" id="n-end">
<li class="nav-item"><a class="nav-link" href="#">A</a></li>
<li class="nav-item"><a class="nav-link" href="#">B</a></li>
</ul>
<ul class="nav flex-column" id="n-col">
<li class="nav-item"><a class="nav-link" href="#">V1</a></li>
<li class="nav-item"><a class="nav-link" href="#">V2</a></li>
</ul>
<ul class="nav nav-tabs" id="n-tabs">
<li class="nav-item"><a class="nav-link active" id="tab-act" href="#">Active</a></li>
<li class="nav-item"><a class="nav-link" href="#">Link</a></li>
</ul>
<ul class="nav nav-pills" id="n-pills">
<li class="nav-item"><a class="nav-link active" id="pill-act" href="#">Active</a></li>
<li class="nav-item"><a class="nav-link" href="#">Link</a></li>
</ul>
<ul class="nav nav-pills nav-justified" id="n-just">
<li class="nav-item"><a class="nav-link active" href="#">Short</a></li>
<li class="nav-item"><a class="nav-link" href="#">Much Longer Label</a></li>
<li class="nav-item"><a class="nav-link" href="#">Mid</a></li>
<li class="nav-item"><a class="nav-link disabled" href="#">Off</a></li>
</ul>
<ul class="nav nav-pills" id="n-pill-dd" style="margin-bottom:130px;">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="t-pill-dd" data-toggle="dropdown" href="#">Dropdown</a>
<div class="dropdown-menu" id="m-pill-dd">
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 2</a>
<a class="dropdown-item" href="#">Link 3</a>
</div>
</li>
</ul>
<ul class="nav nav-tabs" id="n-tab-dd" style="margin-bottom:130px;">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="t-tab-dd" data-toggle="dropdown" href="#">Dropdown</a>
<div class="dropdown-menu" id="m-tab-dd">
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 2</a>
<a class="dropdown-item" href="#">Link 3</a>
</div>
</li>
</ul>
<ul class="nav nav-tabs" id="dyn-tabs">
<li class="nav-item"><a class="nav-link active" id="dt-home" data-toggle="tab" href="#tp-home">Home</a></li>
<li class="nav-item"><a class="nav-link" id="dt-m1" data-toggle="tab" href="#tp-m1">Menu 1</a></li>
<li class="nav-item"><a class="nav-link" id="dt-m2" data-toggle="tab" href="#tp-m2">Menu 2</a></li>
</ul>
<div class="tab-content" id="tc-tabs">
<div class="tab-pane fade active show" id="tp-home">Home Pane</div>
<div class="tab-pane fade" id="tp-m1">Menu 1 Pane</div>
<div class="tab-pane fade" id="tp-m2">Menu 2 Pane</div>
</div>
<ul class="nav nav-pills" id="dyn-pills">
<li class="nav-item"><a class="nav-link active" id="dp-home" data-toggle="pill" href="#pp-home">Home</a></li>
<li class="nav-item"><a class="nav-link" id="dp-m1" data-toggle="pill" href="#pp-m1">Menu 1</a></li>
</ul>
<div class="tab-content" id="tc-pills">
<div class="tab-pane active" id="pp-home">Pill Home</div>
<div class="tab-pane" id="pp-m1">Pill Menu 1</div>
</div>
</div>
<script>
document.querySelectorAll('[data-toggle="dropdown"]').forEach(el => {{
el.addEventListener('click', e => {{
e.preventDefault();
el.nextElementSibling.classList.toggle('show');
}});
}});
document.querySelectorAll('[data-toggle="tab"], [data-toggle="pill"]').forEach(link => {{
link.addEventListener('click', e => {{
e.preventDefault();
const nav = link.closest('.nav');
nav.querySelectorAll('.nav-link').forEach(l => l.classList.remove('active'));
link.classList.add('active');
const target = document.querySelector(link.getAttribute('href'));
const content = target.parentElement;
content.querySelectorAll('.tab-pane').forEach(p => p.classList.remove('active', 'show'));
target.classList.add('active');
if (target.classList.contains('fade')) target.classList.add('show');
}});
}});
</script>
</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": 1000})
page.set_content(HTML, wait_until="load")
c1_actual = page.evaluate("""() => {
const dis = getComputedStyle(document.getElementById('n-dis'));
const ctr = [...document.querySelectorAll('#n-center .nav-item')].map(e => e.getBoundingClientRect());
const end = [...document.querySelectorAll('#n-end .nav-item')].map(e => e.getBoundingClientRect());
const col = [...document.querySelectorAll('#n-col .nav-item')].map(e => e.getBoundingClientRect());
const mid = (ctr[0].left + ctr[ctr.length - 1].right) / 2;
return {
nav_display: getComputedStyle(document.getElementById('n-basic')).display,
disabled_pe: dis.pointerEvents,
disabled_color: dis.color,
centered: Math.abs(mid - 400) < 1,
right_aligned: Math.abs(end[end.length - 1].right - 800) < 1,
column_stacked: col[1].top > col[0].top
};
}""")
c1_exp = {
"nav_display": "flex",
"disabled_pe": "none",
"disabled_color": "rgb(108, 117, 125)",
"centered": True,
"right_aligned": True,
"column_stacked": True,
}
c2_actual = page.evaluate("""() => {
const tabsUl = getComputedStyle(document.getElementById('n-tabs'));
const tabAct = getComputedStyle(document.getElementById('tab-act'));
const pillAct = getComputedStyle(document.getElementById('pill-act'));
const jws = [...document.querySelectorAll('#n-just .nav-item')].map(e => Math.round(e.getBoundingClientRect().width));
return {
tabs_bottom_border: [tabsUl.borderBottomWidth, tabsUl.borderBottomColor],
tab_active_bg: tabAct.backgroundColor,
pill_active_bg_color: [pillAct.backgroundColor, pillAct.color, pillAct.borderRadius],
justified_widths_px: jws
};
}""")
c2_exp = {
"tabs_bottom_border": ["1px", "rgb(222, 226, 230)"],
"tab_active_bg": "rgb(255, 255, 255)",
"pill_active_bg_color": ["rgb(0, 123, 255)", "rgb(255, 255, 255)", "4px"],
"justified_widths_px": [200, 200, 200, 200],
}
page.click("#t-pill-dd")
page.click("#t-tab-dd")
c3_actual = page.evaluate("""() => ({
pill_dd_display: getComputedStyle(document.getElementById('m-pill-dd')).display,
pill_dd_items: document.querySelectorAll('#m-pill-dd .dropdown-item').length,
tab_dd_display: getComputedStyle(document.getElementById('m-tab-dd')).display,
tab_dd_items: document.querySelectorAll('#m-tab-dd .dropdown-item').length
})""")
c3_exp = {
"pill_dd_display": "block",
"pill_dd_items": 3,
"tab_dd_display": "block",
"tab_dd_items": 3,
}
snap_tabs = lambda: page.evaluate("""() => {
const ids = ['tp-home', 'tp-m1', 'tp-m2'];
return ids.filter(id => getComputedStyle(document.getElementById(id)).display === 'block' && getComputedStyle(document.getElementById(id)).opacity === '1');
}""")
t0 = snap_tabs()
page.click("#dt-m1")
t1 = snap_tabs()
page.click("#dt-m2")
t2 = snap_tabs()
c4_actual = {"init": t0, "after_m1": t1, "after_m2": t2}
c4_exp = {"init": ["tp-home"], "after_m1": ["tp-m1"], "after_m2": ["tp-m2"]}
p0 = page.evaluate("() => [getComputedStyle(document.getElementById('pp-home')).display, getComputedStyle(document.getElementById('pp-m1')).display, getComputedStyle(document.getElementById('dp-home')).backgroundColor]")
page.click("#dp-m1")
p1 = page.evaluate("() => [getComputedStyle(document.getElementById('pp-home')).display, getComputedStyle(document.getElementById('pp-m1')).display, getComputedStyle(document.getElementById('dp-m1')).backgroundColor]")
c5_actual = {"init": p0, "after_m1": p1}
c5_exp = {
"init": ["block", "none", "rgb(0, 123, 255)"],
"after_m1": ["none", "block", "rgb(0, 123, 255)"],
}
cases = [
{"name": "basic_alignment_and_vertical_nav", "input": ".nav, .disabled, .justify-content-center/end, .flex-column", "expected": c1_exp, "actual": c1_actual, "passed": c1_actual == c1_exp},
{"name": "tabs_pills_and_justified", "input": ".nav-tabs, .nav-pills & .nav-justified in 800px", "expected": c2_exp, "actual": c2_actual, "passed": c2_actual == c2_exp},
{"name": "nav_dropdowns_pills_and_tabs", "input": ".nav-pills & .nav-tabs dropdown toggles", "expected": c3_exp, "actual": c3_actual, "passed": c3_actual == c3_exp},
{"name": "dynamic_toggleable_tabs_with_fade", "input": "data-toggle=tab switching .tab-pane.fade panes", "expected": c4_exp, "actual": c4_actual, "passed": c4_actual == c4_exp},
{"name": "dynamic_toggleable_pills", "input": "data-toggle=pill switching .tab-pane panes", "expected": c5_exp, "actual": c5_actual, "passed": c5_actual == c5_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-navs-lab/explore-navs.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-navs.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a688.zip
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a664.zip