"""Offline Playwright Chromium lab for Foundation 5 Ch 16 (tabs)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_TABS_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 20px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; color: #222; background: #fff; }
.tabs {
list-style: none;
margin: 0;
padding: 0;
}
.tabs::before, .tabs::after { content: " "; display: table; }
.tabs::after { clear: both; }
.tabs .tab-title {
position: relative;
margin-bottom: 0;
list-style: none;
float: left;
}
.tabs .tab-title > a {
display: block;
background-color: #EFEFEF;
color: #222222;
padding: 1rem 2rem;
font-size: 1rem;
text-decoration: none;
}
.tabs .tab-title.active > a {
background-color: #FFFFFF;
color: #222222;
}
.tabs.vertical {
width: 20%;
max-width: 20%;
float: left;
margin: 0 0 1.25rem;
}
.tabs.vertical .tab-title {
float: none;
display: block;
}
.tabs-content {
margin-bottom: 1.5rem;
width: 100%;
}
.tabs-content > .content {
display: none;
float: left;
padding: 0.9375rem 0;
width: 100%;
}
.tabs-content > .content.active {
display: block;
float: none;
-webkit-animation: fadeEffect 1s;
animation: fadeEffect 1s;
}
.tabs-content.vertical {
width: 80%;
max-width: 80%;
float: left;
padding-left: 1rem;
}
@-webkit-keyframes fadeEffect {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes fadeEffect {
from { opacity: 0; }
to { opacity: 1; }
}
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_TABS_CSS}</style></head>
<body>
<ul id="tabs-main" class="tabs" data-tab>
<li id="t-home" class="tab-title active"><a href="#home">Home</a></li>
<li id="t-m1" class="tab-title"><a href="#menu1">Menu 1</a></li>
<li id="t-m2" class="tab-title"><a href="#menu2">Menu 2</a></li>
<li id="t-m3" class="tab-title"><a href="#menu3">Menu 3</a></li>
</ul>
<div id="content-main" class="tabs-content">
<div class="content active" id="home"><h3>HOME</h3><p>Home content.</p></div>
<div class="content" id="menu1"><h3>Menu 1</h3><p>First tab content.</p></div>
<div class="content" id="menu2"><h3>Menu 2</h3><p>Second tab content.</p></div>
<div class="content" id="menu3"><h3>Menu 3</h3><p>Last tab content.</p></div>
</div>
<div style="width: 800px; overflow: hidden; margin-top: 24px;">
<ul id="tabs-vert" class="tabs vertical" data-tab>
<li class="tab-title active"><a href="#v1">V-Tab 1</a></li>
<li class="tab-title"><a href="#v2">V-Tab 2</a></li>
<li class="tab-title"><a href="#v3">V-Tab 3</a></li>
</ul>
<div class="tabs-content vertical">
<div class="content active" id="v1"><p>Vertical panel 1</p></div>
<div class="content" id="v2"><p>Vertical panel 2</p></div>
<div class="content" id="v3"><p>Vertical panel 3</p></div>
</div>
</div>
<script>
(function () {{
document.querySelectorAll('ul.tabs[data-tab]').forEach((ul) => {{
ul.querySelectorAll('li.tab-title > a').forEach((a) => {{
a.addEventListener('click', (e) => {{
e.preventDefault();
const targetId = (a.getAttribute('href') || '').replace(/^#/, '');
const target = document.getElementById(targetId);
if (!target) return;
ul.querySelectorAll('li.tab-title').forEach((li) => li.classList.remove('active'));
a.parentElement.classList.add('active');
const container = target.parentElement;
container.querySelectorAll(':scope > .content').forEach((c) => c.classList.remove('active'));
target.classList.add('active');
}});
}});
}});
}})();
</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": 1000, "height": 800})
page.set_content(HTML_PAGE)
# Case 1: horizontal_tabs_and_active_style
act_1 = page.evaluate("""() => {
const items = Array.from(document.querySelectorAll('#tabs-main > li.tab-title'));
const rects = items.map(li => li.getBoundingClientRect());
const sameRow = rects.every(r => Math.abs(r.top - rects[0].top) <= 1);
const strictlyRight = rects.every((r, i) => i === 0 || r.left >= rects[i - 1].right - 1);
const activeBg = getComputedStyle(items[0].querySelector('a')).backgroundColor;
const inactiveBg = getComputedStyle(items[1].querySelector('a')).backgroundColor;
return {
same_row: sameRow,
strictly_left_to_right: strictlyRight,
active_bg: activeBg,
inactive_bg: inactiveBg,
};
}""")
exp_1 = {
"same_row": True,
"strictly_left_to_right": True,
"active_bg": "rgb(255, 255, 255)",
"inactive_bg": "rgb(239, 239, 239)",
}
cases.append({
"name": "horizontal_tabs_and_active_style",
"expected": exp_1,
"actual": act_1,
"passed": act_1 == exp_1,
})
# Case 2: vertical_tabs_layout
act_2 = page.evaluate("""() => {
const ul = document.getElementById('tabs-vert');
const items = Array.from(ul.querySelectorAll('li.tab-title'));
const rects = items.map(li => li.getBoundingClientRect());
const sameLeft = rects.every(r => Math.abs(r.left - rects[0].left) <= 1);
const strictlyDown = rects.every((r, i) => i === 0 || r.top >= rects[i - 1].bottom - 1);
return {
vert_width: Math.round(ul.getBoundingClientRect().width),
same_left: sameLeft,
strictly_vertical: strictlyDown,
};
}""")
exp_2 = {
"vert_width": 160,
"same_left": True,
"strictly_vertical": True,
}
cases.append({
"name": "vertical_tabs_layout",
"expected": exp_2,
"actual": act_2,
"passed": act_2 == exp_2,
})
# Case 3: toggleable_tabs_content_switch
def tab_state():
return page.evaluate("""() => ({
active_tab: document.querySelector('#tabs-main > li.tab-title.active').id,
visible_panels: Array.from(document.querySelectorAll('#content-main > .content'))
.filter(el => getComputedStyle(el).display === 'block')
.map(el => el.id),
})""")
s0 = tab_state()
page.locator('#t-m1 a').click()
s1 = tab_state()
page.locator('#t-m3 a').click()
s2 = tab_state()
act_3 = {"initial": s0, "after_menu1": s1, "after_menu3": s2}
exp_3 = {
"initial": {"active_tab": "t-home", "visible_panels": ["home"]},
"after_menu1": {"active_tab": "t-m1", "visible_panels": ["menu1"]},
"after_menu3": {"active_tab": "t-m3", "visible_panels": ["menu3"]},
}
cases.append({
"name": "toggleable_tabs_content_switch",
"expected": exp_3,
"actual": act_3,
"passed": act_3 == exp_3,
})
# Case 4: tabs_css_fade_animation
act_4 = page.evaluate("""() => {
const activePanel = document.querySelector('#content-main > .content.active');
const s = getComputedStyle(activePanel);
return {
animation_name: s.animationName,
animation_duration: s.animationDuration,
};
}""")
exp_4 = {
"animation_name": "fadeEffect",
"animation_duration": "1s",
}
cases.append({
"name": "tabs_css_fade_animation",
"expected": exp_4,
"actual": act_4,
"passed": act_4 == exp_4,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/foundation-tabs-lab/explore-tabs.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tabs.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a771.zip