"""Offline Playwright Chromium lab for Foundation 5 Ch 18 (pricing-tables)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_PRICING_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; color: #222; background: #fff; }
.row {
width: 100%;
max-width: 960px;
margin: 0 auto;
}
.row::before, .row::after { content: " "; display: table; }
.row::after { clear: both; }
.columns {
position: relative;
padding-left: 15px;
padding-right: 15px;
width: 100%;
float: left;
}
@media only screen and (min-width: 40.063em) {
.medium-4 { width: 33.33333%; }
}
.pricing-table {
border: solid 1px #DDDDDD;
margin: 0 0 1.25rem 0;
padding: 0;
list-style: none;
width: 100%;
}
.pricing-table .title {
background-color: #333333;
padding: 0.9375rem 1.25rem;
text-align: center;
color: #EEEEEE;
font-weight: normal;
font-size: 1rem;
}
.pricing-table .price {
background-color: #F6F6F6;
padding: 0.9375rem 1.25rem;
text-align: center;
color: #333333;
font-weight: normal;
font-size: 2rem;
}
.pricing-table .description {
background-color: #FFFFFF;
padding: 0.9375rem;
text-align: center;
color: #777777;
font-size: 0.75rem;
border-bottom: dotted 1px #DDDDDD;
}
.pricing-table .bullet-item {
background-color: #FFFFFF;
padding: 0.9375rem;
text-align: center;
color: #333333;
font-size: 0.875rem;
border-bottom: dotted 1px #DDDDDD;
}
.pricing-table .cta-button {
background-color: #FFFFFF;
text-align: center;
padding: 1.25rem 1.25rem 0;
}
.button {
display: inline-block;
background-color: #008CBA;
color: #FFFFFF;
padding: 0.75rem 1.5rem;
margin-bottom: 1.25rem;
text-decoration: none;
font-size: 1rem;
}
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_PRICING_CSS}</style></head>
<body>
<div id="single-box" style="width: 600px; margin: 16px auto;">
<ul id="pt-single" class="pricing-table">
<li class="title">Basic</li>
<li class="price">$19.99</li>
<li class="description">Great for small business</li>
<li class="bullet-item">24/7 Support</li>
<li class="bullet-item">15GB Storage</li>
<li class="bullet-item">1GB Bandwidth</li>
<li class="cta-button"><a class="button" href="#">Buy</a></li>
</ul>
</div>
<div id="grid-row" class="row">
<div id="col-1" class="medium-4 columns">
<ul class="pricing-table">
<li class="title">Basic</li>
<li class="price">$19.99</li>
<li class="bullet-item">15GB Storage</li>
<li class="cta-button"><a class="button" href="#">Buy</a></li>
</ul>
</div>
<div id="col-2" class="medium-4 columns">
<ul class="pricing-table">
<li class="title">Pro</li>
<li class="price">$49.99</li>
<li class="bullet-item">50GB Storage</li>
<li class="cta-button"><a class="button" href="#">Buy</a></li>
</ul>
</div>
<div id="col-3" class="medium-4 columns">
<ul class="pricing-table">
<li class="title">Premium</li>
<li class="price">$99.99</li>
<li class="bullet-item">200GB Storage</li>
<li class="cta-button"><a class="button" href="#">Buy</a></li>
</ul>
</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():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 960, "height": 900})
page.set_content(HTML_PAGE)
# Case 1: single_pricing_table_anatomy
act_1 = page.evaluate("""() => {
const box = document.getElementById('single-box');
const ul = document.getElementById('pt-single');
const title = getComputedStyle(ul.querySelector('.title'));
const price = getComputedStyle(ul.querySelector('.price'));
const desc = getComputedStyle(ul.querySelector('.description'));
const bullet = getComputedStyle(ul.querySelector('.bullet-item'));
const cta = getComputedStyle(ul.querySelector('.cta-button'));
const btn = getComputedStyle(ul.querySelector('.cta-button .button'));
return {
fills_container_100pct: Math.round(ul.getBoundingClientRect().width) === Math.round(box.getBoundingClientRect().width),
table_border: getComputedStyle(ul).borderTopWidth + ' ' + getComputedStyle(ul).borderTopStyle,
title_bg: title.backgroundColor,
title_align: title.textAlign,
price_bg: price.backgroundColor,
price_font_size: price.fontSize,
desc_border_bottom: desc.borderBottomStyle,
bullet_count: ul.querySelectorAll('.bullet-item').length,
bullet_align: bullet.textAlign,
cta_align: cta.textAlign,
cta_button_bg: btn.backgroundColor,
};
}""")
exp_1 = {
"fills_container_100pct": True,
"table_border": "1px solid",
"title_bg": "rgb(51, 51, 51)",
"title_align": "center",
"price_bg": "rgb(246, 246, 246)",
"price_font_size": "32px",
"desc_border_bottom": "dotted",
"bullet_count": 3,
"bullet_align": "center",
"cta_align": "center",
"cta_button_bg": "rgb(0, 140, 186)",
}
cases.append({
"name": "single_pricing_table_anatomy",
"expected": exp_1,
"actual": act_1,
"passed": act_1 == exp_1,
})
# Case 2: three_column_pricing_grid_desktop
act_2 = page.evaluate("""() => {
const cols = ['col-1', 'col-2', 'col-3'].map(id => document.getElementById(id).getBoundingClientRect());
const widths = cols.map(r => Math.round(r.width));
const sameRow = cols.every(r => Math.abs(r.top - cols[0].top) <= 1);
const tableWidth = Math.round(document.querySelector('#col-1 .pricing-table').getBoundingClientRect().width);
return {
same_row: sameRow,
col_widths: widths,
inner_table_width: tableWidth,
};
}""")
exp_2 = {
"same_row": True,
"col_widths": [320, 320, 320],
"inner_table_width": 290,
}
cases.append({
"name": "three_column_pricing_grid_desktop",
"expected": exp_2,
"actual": act_2,
"passed": act_2 == exp_2,
})
# Case 3: three_column_pricing_grid_mobile
page.set_viewport_size({"width": 480, "height": 900})
act_3 = page.evaluate("""() => {
const cols = ['col-1', 'col-2', 'col-3'].map(id => document.getElementById(id).getBoundingClientRect());
const widths = cols.map(r => Math.round(r.width));
const stacked = cols[1].top >= cols[0].bottom - 1 && cols[2].top >= cols[1].bottom - 1;
return {
stacked_vertically: stacked,
col_widths: widths,
};
}""")
exp_3 = {
"stacked_vertically": True,
"col_widths": [480, 480, 480],
}
cases.append({
"name": "three_column_pricing_grid_mobile",
"expected": exp_3,
"actual": act_3,
"passed": act_3 == exp_3,
})
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-pricing-tables-lab/explore-pricing-tables.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-pricing-tables.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a739.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a765.zip