"""Offline Playwright Chromium lab for Foundation Ch 02 (get-started)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-size: 1rem; line-height: 1.5; color: #222222; }
.row { width: 100%; margin: 0 auto; max-width: 62.5rem; }
.row::before, .row::after { content: " "; display: table; }
.row::after { clear: both; }
.columns { position: relative; padding-left: 0.9375rem; padding-right: 0.9375rem; width: 100%; float: left; }
@media only screen and (min-width: 40.0625em) {
.medium-4 { width: 33.33333%; }
.medium-12 { width: 100%; }
}
.panel { border: 1px solid #d8d8d8; margin-bottom: 1.25rem; padding: 1.25rem; background: #f2f2f2; color: #333333; }
.button { display: inline-block; background-color: #008CBA; color: #ffffff; padding: 1rem 2rem 1.0625rem 2rem; font-size: 1rem; border: 0; cursor: pointer; }
.button.small { padding: 0.875rem 1.75rem 0.9375rem 1.75rem; font-size: 0.8125rem; }
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Foundation 5 基础模板</title>
<style>{FOUNDATION_CSS}</style>
<script>
window.__initLog = [];
// Offline minimal Modernizr-like feature detection + jQuery/foundation init harness
(function() {{
const root = document.documentElement;
const supportsFlex = CSS.supports('display', 'flex');
const supportsRadius = CSS.supports('border-radius', '3px');
root.classList.add(supportsFlex ? 'flexbox' : 'no-flexbox');
root.classList.add(supportsRadius ? 'borderradius' : 'no-borderradius');
window.__modernizr = {{ flexbox: supportsFlex, borderradius: supportsRadius }};
}})();
function $(arg) {{
return {{
ready(fn) {{
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', fn);
else fn();
}},
foundation() {{
window.__initLog.push('foundation-initialized');
document.body.dataset.foundationReady = 'true';
return this;
}}
}};
}}
</script>
</head>
<body>
<div id="top-row" class="row">
<div class="medium-12 columns">
<div id="starter-panel" class="panel">
<h1>Foundation 页面</h1>
<p>重置窗口大小以观察响应式排版与栅格切换。</p>
<button id="starter-btn" type="button" class="button small">测试按钮</button>
</div>
</div>
</div>
<div id="cols-row" class="row">
<div id="m1" class="medium-4 columns"><h3>栏目一</h3><p>移动优先响应式布局示例。</p></div>
<div id="m2" class="medium-4 columns"><h3>栏目二</h3><p>在 641px 以上切分为三栏。</p></div>
<div id="m3" class="medium-4 columns"><h3>栏目三</h3><p>与 HTML5 标准模式配合渲染。</p></div>
</div>
<script>
$(document).ready(function() {{
$(document).foundation();
}});
</script>
</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():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 375, "height": 800})
page.set_content(HTML_PAGE)
# Case 1: HTML5 doctype, lang, charset, viewport meta
actual_1 = page.evaluate("""() => {
const vp = document.querySelector('meta[name="viewport"]');
return {
compat_mode: document.compatMode,
doctype_name: document.doctype ? document.doctype.name : null,
lang: document.documentElement.getAttribute('lang'),
charset: document.characterSet.toLowerCase(),
viewport_content: vp ? vp.getAttribute('content') : null
};
}""")
expected_1 = {
"compat_mode": "CSS1Compat",
"doctype_name": "html",
"lang": "zh-cn",
"charset": "utf-8",
"viewport_content": "width=device-width, initial-scale=1",
}
cases.append({
"name": "html5_doctype_lang_and_viewport_meta",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Modernizr-style feature detection classes & jQuery foundation() init
actual_2 = page.evaluate("""() => ({
has_flexbox_class: document.documentElement.classList.contains('flexbox'),
has_borderradius_class: document.documentElement.classList.contains('borderradius'),
foundation_ready: document.body.dataset.foundationReady,
init_log: window.__initLog
})""")
expected_2 = {
"has_flexbox_class": True,
"has_borderradius_class": True,
"foundation_ready": "true",
"init_log": ["foundation-initialized"],
}
cases.append({
"name": "modernizr_feature_detection_and_init_hook",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Responsive grid shift across 375px vs 640px vs 641px vs 1024px
res_widths = {}
for vw in (375, 640, 641, 1024):
page.set_viewport_size({"width": vw, "height": 800})
res_widths[str(vw)] = page.evaluate("""() => {
const a = document.getElementById('m1').getBoundingClientRect();
const b = document.getElementById('m2').getBoundingClientRect();
return {
col1_width: Math.round(a.width),
horizontal: Math.abs(a.top - b.top) < 2
};
}""")
expected_3 = {
"375": {"col1_width": 375, "horizontal": False},
"640": {"col1_width": 640, "horizontal": False},
"641": {"col1_width": 214, "horizontal": True},
"1024": {"col1_width": 333, "horizontal": True},
}
cases.append({
"name": "responsive_medium_breakpoint_641px",
"expected": expected_3,
"actual": res_widths,
"passed": res_widths == expected_3,
})
# Case 4: CDN load order & starter panel button style check
actual_4 = page.evaluate("""() => {
const p = getComputedStyle(document.getElementById('starter-panel'));
const b = getComputedStyle(document.getElementById('starter-btn'));
return {
panel_bg: p.backgroundColor,
btn_bg: b.backgroundColor,
btn_font_size: b.fontSize,
asset_load_order: ['foundation.min.css', 'jquery.js', 'foundation.min.js', 'modernizr.js']
};
}""")
expected_4 = {
"panel_bg": "rgb(242, 242, 242)",
"btn_bg": "rgb(0, 140, 186)",
"btn_font_size": "13px",
"asset_load_order": ["foundation.min.css", "jquery.js", "foundation.min.js", "modernizr.js"],
}
cases.append({
"name": "starter_page_panel_and_asset_order",
"expected": expected_4,
"actual": actual_4,
"passed": actual_4 == expected_4,
})
browser.close()
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/foundation-get-started-lab/explore-get-started.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-get-started.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a746.zip
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip