"""Offline Playwright Chromium lab for Bootstrap 5 Chapter 09 (jumbotron)."""
import json
from playwright.sync_api import sync_playwright
BS5_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; font-family: system-ui, sans-serif; color: #212529; }
.mt-4 { margin-top: 1.5rem !important; }
.p-5 { padding: 3rem !important; }
.bg-primary { background-color: #0d6efd !important; }
.bg-light { background-color: #f8f9fa !important; }
.text-white { color: #fff !important; }
.text-dark { color: #212529 !important; }
.rounded { border-radius: 0.25rem !important; }
.rounded-3 { border-radius: 0.3rem !important; }
.btn { display: inline-block; padding: 0.375rem 0.75rem; font-size: 1rem; border-radius: 0.25rem; border: 1px solid transparent; }
.btn-light { color: #000; background-color: #f8f9fa; border-color: #f8f9fa; }
"""
HTML_DOC = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS5_CSS}</style></head>
<body>
<div id="bs5-jumbo" class="mt-4 p-5 bg-primary text-white rounded">
<h1>超大屏幕展示区</h1>
<p>在 Bootstrap 5 中通过间距、背景色与圆角工具类自由组合实现。</p>
<button id="jumbo-btn" type="button" class="btn btn-light">了解更多</button>
</div>
<div id="legacy-jumbo" class="jumbotron">旧版 .jumbotron 类在 Bootstrap 5 中已移除</div>
<div id="light-jumbo" class="mt-4 p-5 bg-light text-dark rounded-3">
<h2>经典浅灰风格</h2>
<p>接近 Bootstrap 4 默认浅灰背景框的视觉效果。</p>
</div>
</body>
</html>
"""
def launch_browser(pw):
try:
return pw.chromium.launch(headless=True)
except Exception:
return pw.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 1000, "height": 800})
page.set_content(HTML_DOC)
# Case 1: Bootstrap 5 utility-built jumbotron styles
c1 = page.evaluate("""() => {
const cs = getComputedStyle(document.getElementById('bs5-jumbo'));
return {
margin_top: cs.marginTop,
padding: [cs.paddingTop, cs.paddingRight, cs.paddingBottom, cs.paddingLeft],
bg: cs.backgroundColor,
color: cs.color,
border_radius: cs.borderRadius
};
}""")
exp1 = {
"margin_top": "24px",
"padding": ["48px", "48px", "48px", "48px"],
"bg": "rgb(13, 110, 253)",
"color": "rgb(255, 255, 255)",
"border_radius": "4px",
}
cases.append({
"name": "utility_jumbotron_styles",
"passed": c1 == exp1,
"expected": exp1,
"actual": c1,
})
# Case 2: Legacy .jumbotron has no built-in styles in BS5
c2 = page.evaluate("""() => {
const cs = getComputedStyle(document.getElementById('legacy-jumbo'));
return {
bg: cs.backgroundColor,
padding_top: cs.paddingTop,
border_radius: cs.borderRadius
};
}""")
exp2 = {
"bg": "rgba(0, 0, 0, 0)",
"padding_top": "0px",
"border_radius": "0px",
}
cases.append({
"name": "bs5_no_builtin_jumbotron_class",
"passed": c2 == exp2,
"expected": exp2,
"actual": c2,
})
# Case 3: Nested HTML/Bootstrap components & light gray variant
c3 = page.evaluate("""() => {
const jb = document.getElementById('bs5-jumbo');
const btn = getComputedStyle(document.getElementById('jumbo-btn'));
const lt = getComputedStyle(document.getElementById('light-jumbo'));
return {
nested_tags: [...jb.children].map(el => el.tagName.toLowerCase()),
nested_btn_bg: btn.backgroundColor,
light_variant_bg: lt.backgroundColor,
light_variant_color: lt.color,
light_variant_radius: lt.borderRadius
};
}""")
exp3 = {
"nested_tags": ["h1", "p", "button"],
"nested_btn_bg": "rgb(248, 249, 250)",
"light_variant_bg": "rgb(248, 249, 250)",
"light_variant_color": "rgb(33, 37, 41)",
"light_variant_radius": "4.8px",
}
cases.append({
"name": "jumbotron_nested_elements_and_light_variant",
"passed": c3 == exp3,
"expected": exp3,
"actual": c3,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/bootstrap5-jumbotron-lab/explore-jumbotron.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-jumbotron.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a721.zip