"""Offline Playwright Chromium verification for Bootstrap 5 Spinners (Ch 15)."""
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; }
@keyframes spinner-border { to { transform: rotate(360deg); } }
@keyframes spinner-grow {
0% { transform: scale(0); opacity: 0; }
50% { opacity: 1; transform: none; }
}
.spinner-border {
display: inline-block;
width: 2rem;
height: 2rem;
vertical-align: -0.125em;
border: 0.25em solid currentColor;
border-right-color: transparent;
border-radius: 50%;
animation: 0.75s linear infinite spinner-border;
}
.spinner-border-sm { width: 1rem; height: 1rem; border-width: 0.2em; }
.spinner-grow {
display: inline-block;
width: 2rem;
height: 2rem;
vertical-align: -0.125em;
background-color: currentColor;
border-radius: 50%;
opacity: 0;
animation: 0.75s linear infinite spinner-grow;
}
.spinner-grow-sm { width: 1rem; height: 1rem; }
.text-muted { color: #6c757d !important; }
.text-primary { color: #0d6efd !important; }
.text-success { color: #198754 !important; }
.text-info { color: #0dcaf0 !important; }
.text-warning { color: #ffc107 !important; }
.text-danger { color: #dc3545 !important; }
.text-secondary { color: #6c757d !important; }
.text-dark { color: #212529 !important; }
.text-light { color: #f8f9fa !important; }
.btn {
display: inline-block; padding: 0.375rem 0.75rem; font-size: 1rem; line-height: 1.5;
border: 1px solid transparent; border-radius: 0.375rem;
}
.btn-primary { color: #fff; background-color: #0d6efd; border-color: #0d6efd; }
.btn:disabled { pointer-events: none; opacity: 0.65; }
</style>
</head>
<body>
<section id="sec-border">
<div id="sb-default" class="spinner-border" role="status"></div>
<div id="sb-muted" class="spinner-border text-muted"></div>
<div id="sb-primary" class="spinner-border text-primary"></div>
<div id="sb-success" class="spinner-border text-success"></div>
<div id="sb-info" class="spinner-border text-info"></div>
<div id="sb-warning" class="spinner-border text-warning"></div>
<div id="sb-danger" class="spinner-border text-danger"></div>
<div id="sb-secondary" class="spinner-border text-secondary"></div>
<div id="sb-dark" class="spinner-border text-dark"></div>
<div id="sb-light" class="spinner-border text-light"></div>
</section>
<section id="sec-grow">
<div id="sg-primary" class="spinner-grow text-primary"></div>
<div id="sg-success" class="spinner-grow text-success"></div>
<div id="sg-danger" class="spinner-grow text-danger"></div>
</section>
<section id="sec-sm">
<div id="sb-sm" class="spinner-border spinner-border-sm"></div>
<div id="sg-sm" class="spinner-grow spinner-grow-sm"></div>
</section>
<section id="sec-btn">
<button id="btn-1" class="btn btn-primary"><span id="bi-1" class="spinner-border spinner-border-sm"></span></button>
<button id="btn-2" class="btn btn-primary"><span class="spinner-border spinner-border-sm"></span> Loading..</button>
<button id="btn-3" class="btn btn-primary" disabled><span class="spinner-border spinner-border-sm"></span> Loading..</button>
<button id="btn-4" class="btn btn-primary" disabled><span id="bi-4" class="spinner-grow spinner-grow-sm"></span> Loading..</button>
</section>
</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": 800, "height": 600})
page.set_content(HTML, wait_until="load")
c1_actual = page.evaluate("""() => {
const s = getComputedStyle(document.getElementById("sb-default"));
const bcol = id => getComputedStyle(document.getElementById(id)).borderTopColor;
return {
width: s.width,
height: s.height,
border_radius: s.borderTopLeftRadius,
border_right_color: s.borderRightColor,
animation_name: s.animationName,
animation_duration: s.animationDuration,
primary_border: bcol("sb-primary"),
success_border: bcol("sb-success"),
danger_border: bcol("sb-danger"),
warning_border: bcol("sb-warning"),
total_colored_borders: document.querySelectorAll("#sec-border .spinner-border").length,
};
}""")
c1_expected = {
"width": "32px",
"height": "32px",
"border_radius": "50%",
"border_right_color": "rgba(0, 0, 0, 0)",
"animation_name": "spinner-border",
"animation_duration": "0.75s",
"primary_border": "rgb(13, 110, 253)",
"success_border": "rgb(25, 135, 84)",
"danger_border": "rgb(220, 53, 69)",
"warning_border": "rgb(255, 193, 7)",
"total_colored_borders": 10,
}
c2_actual = page.evaluate("""() => {
const sp = getComputedStyle(document.getElementById("sg-primary"));
return {
width: sp.width,
height: sp.height,
border_radius: sp.borderTopLeftRadius,
animation_name: sp.animationName,
primary_bg: sp.backgroundColor,
success_bg: getComputedStyle(document.getElementById("sg-success")).backgroundColor,
danger_bg: getComputedStyle(document.getElementById("sg-danger")).backgroundColor,
};
}""")
c2_expected = {
"width": "32px",
"height": "32px",
"border_radius": "50%",
"animation_name": "spinner-grow",
"primary_bg": "rgb(13, 110, 253)",
"success_bg": "rgb(25, 135, 84)",
"danger_bg": "rgb(220, 53, 69)",
}
c3_actual = page.evaluate("""() => {
const b = getComputedStyle(document.getElementById("sb-sm"));
const g = getComputedStyle(document.getElementById("sg-sm"));
return {
border_sm_wh: [b.width, b.height],
border_sm_bw: b.borderTopWidth,
grow_sm_wh: [g.width, g.height],
};
}""")
c3_expected = {
"border_sm_wh": ["16px", "16px"],
"border_sm_bw": "3px",
"grow_sm_wh": ["16px", "16px"],
}
c4_actual = page.evaluate("""() => {
const b1 = document.getElementById("btn-1");
const b3 = document.getElementById("btn-3");
const b4 = document.getElementById("btn-4");
const i1 = getComputedStyle(document.getElementById("bi-1"));
const i4 = getComputedStyle(document.getElementById("bi-4"));
return {
btn1_disabled: b1.disabled,
btn3_disabled: b3.disabled,
btn4_disabled: b4.disabled,
btn3_opacity: getComputedStyle(b3).opacity,
inline_border_sm: [i1.width, i1.height],
inline_grow_sm: [i4.width, i4.height],
};
}""")
c4_expected = {
"btn1_disabled": False,
"btn3_disabled": True,
"btn4_disabled": True,
"btn3_opacity": "0.65",
"inline_border_sm": ["16px", "16px"],
"inline_grow_sm": ["16px", "16px"],
}
browser.close()
cases = [
dict(name="spinner_border_and_colors", description="核对 .spinner-border 32px 圆形透明右边框旋转动画与 text-* currentColor 继承", expected=c1_expected, actual=c1_actual, passed=c1_actual == c1_expected),
dict(name="spinner_grow_and_colors", description="核对 .spinner-grow 脉冲缩放动画与 currentColor 实心背景色", expected=c2_expected, actual=c2_actual, passed=c2_actual == c2_expected),
dict(name="spinner_sizes", description="核对 .spinner-border-sm 与 .spinner-grow-sm 缩至 16px 及 Chromium 像素对齐后 3px 边框宽度", expected=c3_expected, actual=c3_actual, passed=c3_actual == c3_expected),
dict(name="spinner_inside_buttons", description="核对加载按钮内嵌旋转器与禁用态 opacity=0.65 组合", 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-spinners-lab/explore-spinners.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-spinners.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a731.zip