蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

在离线 Chromium 中验证进度条高度、标签配色、条纹动画与多段拼接

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-progressbars.py
explore-progressbars.py204 行
"""Offline Playwright Chromium verification for Bootstrap 5 Progress Bars (Ch 14)."""
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; }
  .wrap { width: 500px; }
  .progress {
    display: flex;
    height: 1rem;
    overflow: hidden;
    font-size: 0.75rem;
    background-color: #e9ecef;
    border-radius: 0.375rem;
    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: #0d6efd;
  }
  .bg-success { background-color: #198754 !important; }
  .bg-info { background-color: #0dcaf0 !important; }
  .bg-warning { background-color: #ffc107 !important; }
  .bg-danger { background-color: #dc3545 !important; }
  .progress-bar-striped {
    background-image: linear-gradient(
      45deg,
      rgba(255, 255, 255, 0.15) 25%,
      transparent 25%,
      transparent 50%,
      rgba(255, 255, 255, 0.15) 50%,
      rgba(255, 255, 255, 0.15) 75%,
      transparent 75%,
      transparent
    );
    background-size: 1rem 1rem;
  }
  @keyframes progress-bar-stripes {
    0% { background-position-x: 1rem; }
  }
  .progress-bar-animated {
    animation: 1s linear infinite progress-bar-stripes;
  }
</style>
</head>
<body>
<div class="wrap">
  <div id="p-basic" class="progress">
    <div id="pb-basic" class="progress-bar" style="width:70%"></div>
  </div>
  <div id="p-height" class="progress" style="height:20px;">
    <div id="pb-height" class="progress-bar" style="width:40%;"></div>
  </div>
  <div class="progress">
    <div id="pb-label" class="progress-bar" style="width:70%">70%</div>
  </div>
  <div class="progress"><div id="pb-success" class="progress-bar bg-success" style="width:40%"></div></div>
  <div class="progress"><div id="pb-info" class="progress-bar bg-info" style="width:50%"></div></div>
  <div class="progress"><div id="pb-warning" class="progress-bar bg-warning" style="width:60%"></div></div>
  <div class="progress"><div id="pb-danger" class="progress-bar bg-danger" style="width:70%"></div></div>
  <div class="progress">
    <div id="pb-striped" class="progress-bar progress-bar-striped" style="width:40%"></div>
  </div>
  <div class="progress">
    <div id="pb-animated" class="progress-bar progress-bar-striped progress-bar-animated" style="width:40%"></div>
  </div>
  <div id="p-stacked" class="progress">
    <div id="st-free" class="progress-bar bg-success" style="width:40%">Free Space</div>
    <div id="st-warn" class="progress-bar bg-warning" style="width:10%">Warning</div>
    <div id="st-danger" class="progress-bar bg-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)
        page = browser.new_page(viewport={"width": 800, "height": 600})
        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 pH = document.getElementById("p-height").getBoundingClientRect();
            const pbH = document.getElementById("pb-height").getBoundingClientRect();
            return {
                default_track_height_px: Math.round(pBasic.height),
                basic_70pct_width_px: Math.round(pbBasic.width),
                custom_track_height_px: Math.round(pH.height),
                custom_bar_height_px: Math.round(pbH.height),
                custom_40pct_width_px: Math.round(pbH.width),
                track_bg: getComputedStyle(document.getElementById("p-basic")).backgroundColor,
            };
        }""")
        c1_expected = {
            "default_track_height_px": 16,
            "basic_70pct_width_px": 350,
            "custom_track_height_px": 20,
            "custom_bar_height_px": 20,
            "custom_40pct_width_px": 200,
            "track_bg": "rgb(233, 236, 239)",
        }

        c2_actual = page.evaluate("""() => {
            const info = id => {
                const el = document.getElementById(id);
                return { bg: getComputedStyle(el).backgroundColor, w: Math.round(el.getBoundingClientRect().width) };
            };
            const lbl = document.getElementById("pb-label");
            return {
                label_text: lbl.textContent,
                label_color: getComputedStyle(lbl).color,
                label_align: getComputedStyle(lbl).textAlign,
                default_bg: info("pb-basic").bg,
                success: info("pb-success"),
                info: info("pb-info"),
                warning: info("pb-warning"),
                danger: info("pb-danger"),
            };
        }""")
        c2_expected = {
            "label_text": "70%",
            "label_color": "rgb(255, 255, 255)",
            "label_align": "center",
            "default_bg": "rgb(13, 110, 253)",
            "success": {"bg": "rgb(25, 135, 84)", "w": 200},
            "info": {"bg": "rgb(13, 202, 240)", "w": 250},
            "warning": {"bg": "rgb(255, 193, 7)", "w": 300},
            "danger": {"bg": "rgb(220, 53, 69)", "w": 350},
        }

        c3_actual = page.evaluate("""() => {
            const s1 = getComputedStyle(document.getElementById("pb-striped"));
            const s2 = getComputedStyle(document.getElementById("pb-animated"));
            return {
                has_linear_gradient: s1.backgroundImage.includes("linear-gradient"),
                stripe_bg_size: s1.backgroundSize,
                animated_name: s2.animationName,
                animated_duration: s2.animationDuration,
                animated_iteration: s2.animationIterationCount,
            };
        }""")
        c3_expected = {
            "has_linear_gradient": True,
            "stripe_bg_size": "16px 16px",
            "animated_name": "progress-bar-stripes",
            "animated_duration": "1s",
            "animated_iteration": "infinite",
        }

        c4_actual = page.evaluate("""() => {
            const r1 = document.getElementById("st-free").getBoundingClientRect();
            const r2 = document.getElementById("st-warn").getBoundingClientRect();
            const r3 = document.getElementById("st-danger").getBoundingClientRect();
            return {
                widths_px: [Math.round(r1.width), Math.round(r2.width), Math.round(r3.width)],
                contiguous: Math.round(r1.right) === Math.round(r2.left) && Math.round(r2.right) === Math.round(r3.left),
                same_top: Math.round(r1.top) === Math.round(r2.top) && Math.round(r2.top) === Math.round(r3.top),
                total_width_px: Math.round(r1.width + r2.width + r3.width),
            };
        }""")
        c4_expected = {
            "widths_px": [200, 50, 100],
            "contiguous": True,
            "same_top": True,
            "total_width_px": 350,
        }

        browser.close()

    cases = [
        dict(name="basic_progress_and_height", description="核对 .progress 默认 16px 高度、自定义 20px 高度及百分比宽度换算", expected=c1_expected, actual=c1_actual, passed=c1_actual == c1_expected),
        dict(name="labeled_and_contextual_bars", description="核对进度条居中文本标签与默认/success/info/warning/danger 语义色", expected=c2_expected, actual=c2_actual, passed=c2_actual == c2_expected),
        dict(name="striped_and_animated_bars", description="核对 .progress-bar-striped 45 度渐变条纹与 .progress-bar-animated 1s 无限动画", expected=c3_expected, actual=c3_actual, passed=c3_actual == c3_expected),
        dict(name="stacked_multiple_bars", description="核对同一 .progress 弹性容器内多段不同颜色进度条无缝拼接", 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-progressbars-lab/explore-progressbars.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-progressbars.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Bootstrap5

课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip

下载课程关联工程包:Bootstrap5

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a729.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习