"""Offline Playwright Chromium lab for Tailwind CSS Ch 01 (tutorial)."""
import json
import os
from playwright.sync_api import sync_playwright
TW_CSS = """
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, -apple-system, sans-serif; font-size: 16px; line-height: 1.5; }
.bg-blue-500 { background-color: #3b82f6; }
.bg-emerald-500 { background-color: #10b981; }
.bg-slate-100 { background-color: #f1f5f9; }
.text-white { color: #ffffff; }
.text-slate-800 { color: #1e293b; }
.p-4 { padding: 1rem; }
.p-6 { padding: 1.5rem; }
.px-4 { padding-left: 1rem; padding-right: 1rem; }
.py-2 { padding-top: 0.5rem; padding-bottom: 0.5rem; }
.rounded-md { border-radius: 0.375rem; }
.rounded-lg { border-radius: 0.5rem; }
.rounded-xl { border-radius: 0.75rem; }
.border { border-width: 1px; border-style: solid; }
.border-slate-300 { border-color: #cbd5e1; }
.w-80 { width: 20rem; }
"""
def launch_browser(p):
exe = p.chromium.executable_path
if exe and os.path.exists(exe):
return p.chromium.launch(headless=True)
for ch in ("chrome", "msedge"):
try:
return p.chromium.launch(channel=ch, headless=True)
except Exception:
continue
return p.chromium.launch(headless=True)
def run():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 1024, "height": 768})
# Case 1: Official tutorial hero card utility styles
page.set_content(f"""<!doctype html>
<html><head><meta charset="utf-8"><style>{TW_CSS}</style></head>
<body>
<div id="hero" class="bg-blue-500 text-white p-4 rounded-lg">
Hello, Tailwind CSS!
</div>
</body></html>""")
actual1 = page.eval_on_selector("#hero", """el => {
const s = getComputedStyle(el);
return {
text: el.textContent.trim(),
backgroundColor: s.backgroundColor,
color: s.color,
padding: s.padding,
borderRadius: s.borderRadius,
};
}""")
expected1 = {
"text": "Hello, Tailwind CSS!",
"backgroundColor": "rgb(59, 130, 246)",
"color": "rgb(255, 255, 255)",
"padding": "16px",
"borderRadius": "8px",
}
cases.append(dict(
name="hero_card_utility_styles",
expected=expected1,
actual=actual1,
passed=(actual1 == expected1),
))
# Case 2: Box geometry under p-4 vs p-6 with fixed w-80 (320px)
page.set_content(f"""<!doctype html>
<html><head><meta charset="utf-8"><style>{TW_CSS}</style></head>
<body>
<div id="card-a" class="w-80 bg-blue-500 text-white p-4 rounded-lg">Line</div>
<div id="card-b" class="w-80 bg-blue-500 text-white p-6 rounded-lg">Line</div>
</body></html>""")
actual2 = page.evaluate("""() => {
const a = document.getElementById('card-a');
const b = document.getElementById('card-b');
const ra = a.getBoundingClientRect();
const rb = b.getBoundingClientRect();
return {
widthA: Math.round(ra.width),
widthB: Math.round(rb.width),
paddingA: getComputedStyle(a).paddingTop,
paddingB: getComputedStyle(b).paddingTop,
heightDeltaPx: Math.round(rb.height - ra.height),
};
}""")
expected2 = {
"widthA": 320,
"widthB": 320,
"paddingA": "16px",
"paddingB": "24px",
"heightDeltaPx": 16,
}
cases.append(dict(
name="utility_composition_box_geometry",
expected=expected2,
actual=actual2,
passed=(actual2 == expected2),
))
# Case 3: Distinct custom UI cards without custom semantic CSS
page.set_content(f"""<!doctype html>
<html><head><meta charset="utf-8"><style>{TW_CSS}</style></head>
<body>
<button id="btn" class="bg-emerald-500 text-white px-4 py-2 rounded-md">Start</button>
<div id="panel" class="bg-slate-100 text-slate-800 border border-slate-300 p-4 rounded-xl">Notice</div>
</body></html>""")
actual3 = page.evaluate("""() => {
const bs = getComputedStyle(document.getElementById('btn'));
const ps = getComputedStyle(document.getElementById('panel'));
return {
btnBg: bs.backgroundColor,
btnPadX: bs.paddingLeft,
btnPadY: bs.paddingTop,
btnRadius: bs.borderRadius,
panelBg: ps.backgroundColor,
panelColor: ps.color,
panelBorderWidth: ps.borderTopWidth,
panelBorderColor: ps.borderTopColor,
panelRadius: ps.borderRadius,
};
}""")
expected3 = {
"btnBg": "rgb(16, 185, 129)",
"btnPadX": "16px",
"btnPadY": "8px",
"btnRadius": "6px",
"panelBg": "rgb(241, 245, 249)",
"panelColor": "rgb(30, 41, 59)",
"panelBorderWidth": "1px",
"panelBorderColor": "rgb(203, 213, 225)",
"panelRadius": "12px",
}
cases.append(dict(
name="zero_custom_css_card_variants",
expected=expected3,
actual=actual3,
passed=(actual3 == expected3),
))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
run()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/tailwindcss-tutorial-lab/explore-tutorial.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a660.zip
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a639.zip