"""Offline Playwright Chromium verification for Bootstrap 4 Progress Bars."""
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; }
.wrap { width: 400px; }
@keyframes progress-bar-stripes {
from { background-position: 1rem 0; }
to { background-position: 0 0; }
}
.progress {
display: flex; height: 1rem; overflow: hidden; font-size: 0.75rem;
background-color: #e9ecef; border-radius: 0.25rem; margin-bottom: 12px;
}
.progress-bar {
display: flex; flex-direction: column; justify-content: center; overflow: hidden;
color: #fff; text-align: center; white-space: nowrap; background-color: #007bff;
transition: width 0.6s ease;
}
.progress-bar-striped {
background-image: linear-gradient(45deg, rgba(255,255,255,.15) 25%, transparent 25%, transparent 50%, rgba(255,255,255,.15) 50%, rgba(255,255,255,.15) 75%, transparent 75%, transparent);
background-size: 1rem 1rem;
}
.progress-bar-animated { animation: progress-bar-stripes 1s linear infinite; }
.bg-success { background-color: #28a745 !important; }
.bg-info { background-color: #17a2b8 !important; }
.bg-warning { background-color: #ffc107 !important; }
.bg-danger { background-color: #dc3545 !important; }
"""
HTML = f"""<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><style>{CSS}</style></head>
<body>
<div class="wrap">
<div class="progress" id="p-basic"><div class="progress-bar" id="pb-basic" style="width:70%"></div></div>
<div class="progress" id="p-tall" style="height:20px;"><div class="progress-bar" id="pb-tall" style="width:40%;"></div></div>
<div class="progress" id="p-label"><div class="progress-bar" id="pb-label" style="width:70%">70%</div></div>
<div class="progress"><div class="progress-bar bg-success" id="pb-success" style="width:40%"></div></div>
<div class="progress"><div class="progress-bar bg-info" id="pb-info" style="width:50%"></div></div>
<div class="progress"><div class="progress-bar bg-warning" id="pb-warning" style="width:60%"></div></div>
<div class="progress"><div class="progress-bar bg-danger" id="pb-danger" style="width:70%"></div></div>
<div class="progress"><div class="progress-bar progress-bar-striped" id="pb-striped" style="width:40%"></div></div>
<div class="progress"><div class="progress-bar progress-bar-striped progress-bar-animated" id="pb-anim" style="width:40%"></div></div>
<div class="progress" id="p-multi">
<div class="progress-bar bg-success" id="m-free" style="width:40%">Free Space</div>
<div class="progress-bar bg-warning" id="m-warn" style="width:10%">Warning</div>
<div class="progress-bar bg-danger" id="m-danger" style="width:20%">Danger</div>
</div>
</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": 800})
page.set_content(HTML, wait_until="load")
c1_actual = page.evaluate("""() => {
const pBasic = document.getElementById('p-basic').getBoundingClientRect();
const pbBasic = document.getElementById('pb-basic').getBoundingClientRect();
const pTall = document.getElementById('p-tall').getBoundingClientRect();
const pbTall = document.getElementById('pb-tall').getBoundingClientRect();
const pbLabel = document.getElementById('pb-label');
const stL = getComputedStyle(pbLabel);
return {
default_height_px: Math.round(pBasic.height),
basic_bar_width_px: Math.round(pbBasic.width),
custom_height_px: Math.round(pTall.height),
tall_bar_width_px: Math.round(pbTall.width),
label_text: pbLabel.textContent,
label_color: stL.color,
label_align: stL.textAlign
};
}""")
c1_exp = {
"default_height_px": 16,
"basic_bar_width_px": 280,
"custom_height_px": 20,
"tall_bar_width_px": 160,
"label_text": "70%",
"label_color": "rgb(255, 255, 255)",
"label_align": "center",
}
c2_actual = page.evaluate("""() => {
const ids = ['pb-basic', 'pb-success', 'pb-info', 'pb-warning', 'pb-danger'];
const res = {};
for (const id of ids) {
const el = document.getElementById(id);
res[id] = [getComputedStyle(el).backgroundColor, Math.round(el.getBoundingClientRect().width)];
}
return res;
}""")
c2_exp = {
"pb-basic": ["rgb(0, 123, 255)", 280],
"pb-success": ["rgb(40, 167, 69)", 160],
"pb-info": ["rgb(23, 162, 184)", 200],
"pb-warning": ["rgb(255, 193, 7)", 240],
"pb-danger": ["rgb(220, 53, 69)", 280],
}
c3_actual = page.evaluate("""() => {
const s = getComputedStyle(document.getElementById('pb-striped'));
const a = getComputedStyle(document.getElementById('pb-anim'));
return {
striped_has_gradient: s.backgroundImage.startsWith('linear-gradient('),
striped_bg_size: s.backgroundSize,
striped_anim_name: s.animationName,
animated_name: a.animationName,
animated_duration: a.animationDuration,
animated_iteration: a.animationIterationCount
};
}""")
c3_exp = {
"striped_has_gradient": True,
"striped_bg_size": "16px 16px",
"striped_anim_name": "none",
"animated_name": "progress-bar-stripes",
"animated_duration": "1s",
"animated_iteration": "infinite",
}
c4_actual = page.evaluate("""() => {
const r1 = document.getElementById('m-free').getBoundingClientRect();
const r2 = document.getElementById('m-warn').getBoundingClientRect();
const r3 = document.getElementById('m-danger').getBoundingClientRect();
return {
widths_px: [Math.round(r1.width), Math.round(r2.width), Math.round(r3.width)],
total_width_px: Math.round(r1.width + r2.width + r3.width),
contiguous_row: Math.abs(r1.top - r2.top) < 1 && Math.abs(r2.left - r1.right) < 1 && Math.abs(r3.left - r2.right) < 1
};
}""")
c4_exp = {
"widths_px": [160, 40, 80],
"total_width_px": 280,
"contiguous_row": True,
}
cases = [
{"name": "basic_height_and_label", "input": "400px track default 16px, custom 20px, labeled 70%", "expected": c1_exp, "actual": c1_actual, "passed": c1_actual == c1_exp},
{"name": "contextual_progress_colors", "input": "default + bg-success/info/warning/danger widths", "expected": c2_exp, "actual": c2_actual, "passed": c2_actual == c2_exp},
{"name": "striped_and_animated_bars", "input": ".progress-bar-striped & .progress-bar-animated", "expected": c3_exp, "actual": c3_actual, "passed": c3_actual == c3_exp},
{"name": "stacked_multi_color_bar", "input": "40% + 10% + 20% stacked bars in 400px .progress", "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-progressbars-lab/explore-progressbars.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-progressbars.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a691.zip
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a664.zip