"""Offline Playwright Chromium verification for Bootstrap 4 Pagination & Breadcrumb."""
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; }
.pagination { display: flex; padding-left: 0; list-style: none; border-radius: 0.25rem; margin: 12px 0; }
.page-link {
position: relative; display: block; padding: 0.5rem 0.75rem; margin-left: -1px;
line-height: 1.25; color: #007bff; background-color: #fff; border: 1px solid #dee2e6; text-decoration: none;
}
.page-item:first-child .page-link { margin-left: 0; border-top-left-radius: 0.25rem; border-bottom-left-radius: 0.25rem; }
.page-item:last-child .page-link { border-top-right-radius: 0.25rem; border-bottom-right-radius: 0.25rem; }
.page-item.active .page-link { z-index: 3; color: #fff; background-color: #007bff; border-color: #007bff; }
.page-item.disabled .page-link { color: #6c757d; pointer-events: none; cursor: auto; background-color: #fff; border-color: #dee2e6; }
.pagination-lg .page-link { padding: 0.75rem 1.5rem; font-size: 1.25rem; line-height: 1.5; }
.pagination-sm .page-link { padding: 0.25rem 0.5rem; font-size: 0.875rem; line-height: 1.5; }
.justify-content-center { justify-content: center !important; }
.justify-content-end { justify-content: flex-end !important; }
.breadcrumb {
display: flex; flex-wrap: wrap; padding: 0.75rem 1rem; margin-bottom: 1rem;
list-style: none; background-color: #e9ecef; border-radius: 0.25rem;
}
.breadcrumb-item + .breadcrumb-item { padding-left: 0.5rem; }
.breadcrumb-item + .breadcrumb-item::before {
display: inline-block; padding-right: 0.5rem; color: #6c757d; content: "/";
}
.breadcrumb-item.active { color: #6c757d; }
"""
HTML = f"""<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><style>{CSS}</style></head>
<body>
<div class="wrap">
<ul class="pagination" id="pg-main">
<li class="page-item disabled" id="li-dis"><a class="page-link" href="#">Previous</a></li>
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item active" id="li-act"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item"><a class="page-link" href="#">Next</a></li>
</ul>
<ul class="pagination pagination-sm" id="pg-sm"><li class="page-item"><a class="page-link" href="#">1</a></li></ul>
<ul class="pagination" id="pg-md"><li class="page-item"><a class="page-link" href="#">1</a></li></ul>
<ul class="pagination pagination-lg" id="pg-lg"><li class="page-item"><a class="page-link" href="#">1</a></li></ul>
<ul class="breadcrumb" id="bc">
<li class="breadcrumb-item" id="bc-1"><a href="#">Photos</a></li>
<li class="breadcrumb-item" id="bc-2"><a href="#">Summer 2017</a></li>
<li class="breadcrumb-item" id="bc-3"><a href="#">Italy</a></li>
<li class="breadcrumb-item active" id="bc-4">Rome</li>
</ul>
<ul class="pagination" id="align-left"><li class="page-item"><a class="page-link" href="#">1</a></li><li class="page-item"><a class="page-link" href="#">2</a></li></ul>
<ul class="pagination justify-content-center" id="align-center"><li class="page-item"><a class="page-link" href="#">1</a></li><li class="page-item"><a class="page-link" href="#">2</a></li></ul>
<ul class="pagination justify-content-end" id="align-right"><li class="page-item"><a class="page-link" href="#">1</a></li><li class="page-item"><a class="page-link" href="#">2</a></li></ul>
</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 ul = document.getElementById('pg-main');
const act = getComputedStyle(document.querySelector('#li-act .page-link'));
const dis = getComputedStyle(document.querySelector('#li-dis .page-link'));
const items = [...ul.querySelectorAll('.page-item')].map(el => el.getBoundingClientRect());
return {
display: getComputedStyle(ul).display,
list_style: getComputedStyle(ul).listStyleType,
single_row: items.every(r => Math.abs(r.top - items[0].top) < 1),
active_bg: act.backgroundColor,
active_color: act.color,
disabled_pointer_events: dis.pointerEvents,
disabled_color: dis.color
};
}""")
c1_exp = {
"display": "flex",
"list_style": "none",
"single_row": True,
"active_bg": "rgb(0, 123, 255)",
"active_color": "rgb(255, 255, 255)",
"disabled_pointer_events": "none",
"disabled_color": "rgb(108, 117, 125)",
}
c2_actual = page.evaluate("""() => {
const sm = document.querySelector('#pg-sm .page-link');
const md = document.querySelector('#pg-md .page-link');
const lg = document.querySelector('#pg-lg .page-link');
const hs = Math.round(sm.getBoundingClientRect().height);
const hm = Math.round(md.getBoundingClientRect().height);
const hl = Math.round(lg.getBoundingClientRect().height);
return {
font_sizes: [getComputedStyle(sm).fontSize, getComputedStyle(md).fontSize, getComputedStyle(lg).fontSize],
heights_px: [hs, hm, hl],
ordered: hs < hm && hm < hl
};
}""")
c2_exp = {
"font_sizes": ["14px", "16px", "20px"],
"heights_px": [31, 38, 56],
"ordered": True,
}
c3_actual = page.evaluate("""() => {
const bc = getComputedStyle(document.getElementById('bc'));
const sep1 = getComputedStyle(document.getElementById('bc-1'), '::before').content;
const sep2 = getComputedStyle(document.getElementById('bc-2'), '::before').content;
const act = getComputedStyle(document.getElementById('bc-4'));
return {
display: bc.display,
bg: bc.backgroundColor,
first_before: sep1,
second_before: sep2,
active_color: act.color
};
}""")
c3_exp = {
"display": "flex",
"bg": "rgb(233, 236, 239)",
"first_before": "none",
"second_before": '"/"',
"active_color": "rgb(108, 117, 125)",
}
c4_actual = page.evaluate("""() => {
const box = (id) => {
const items = [...document.querySelectorAll('#'+id+' .page-item')].map(e => e.getBoundingClientRect());
return { left: items[0].left, right: items[items.length - 1].right };
};
const l = box('align-left'), c = box('align-center'), r = box('align-right');
const centerMid = (c.left + c.right) / 2;
return {
left_aligned: Math.abs(l.left - 0) < 1,
center_aligned: Math.abs(centerMid - 400) < 1,
right_aligned: Math.abs(r.right - 800) < 1
};
}""")
c4_exp = {
"left_aligned": True,
"center_aligned": True,
"right_aligned": True,
}
cases = [
{"name": "basic_active_and_disabled_pagination", "input": ".pagination with .active and .disabled", "expected": c1_exp, "actual": c1_actual, "passed": c1_actual == c1_exp},
{"name": "pagination_sizing", "input": ".pagination-sm / default / .pagination-lg", "expected": c2_exp, "actual": c2_actual, "passed": c2_actual == c2_exp},
{"name": "breadcrumb_navigation", "input": ".breadcrumb with 4 .breadcrumb-item entries", "expected": c3_exp, "actual": c3_actual, "passed": c3_actual == c3_exp},
{"name": "pagination_alignment", "input": "default / .justify-content-center / .justify-content-end in 800px", "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-pagination-lab/explore-pagination.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-pagination.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a689.zip
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a664.zip