"""Offline Playwright Chromium lab for Vue 2 Chapter 03 (vue-directory-structure)."""
import json
import tempfile
from pathlib import Path
from playwright.sync_api import sync_playwright
HTML_DOC = r"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>Vue 2 目录结构与单文件组件实测</title>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
</head>
<body>
<div id="app">
<img id="app-logo" alt="Vue logo" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Cpolygon points='20,38 2,4 38,4' fill='%2341b883'/%3E%3C/svg%3E">
<div class="hello"><h1 id="hello-msg"></h1></div>
</div>
<script>
(function () {
const HelloComponent = {
name: 'hello',
data() {
return { msg: 'Welcome to Your Vue.js App' };
}
};
const state = HelloComponent.data();
let msgVal = state.msg;
const h1 = document.getElementById('hello-msg');
function render() { h1.textContent = msgVal; }
Object.defineProperty(state, 'msg', {
enumerable: true,
get() { return msgVal; },
set(v) { msgVal = v; render(); }
});
render();
window.helloState = state;
window.appComponent = { name: 'app', components: { Hello: HelloComponent } };
})();
</script>
</body>
</html>
"""
def launch_browser(pw):
try:
return pw.chromium.launch(headless=True)
except Exception:
return pw.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
# Case 1: Temporary webpack-template project directory layout verification
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp) / "my-project"
for rel in [
"build/webpack.base.conf.js",
"config/index.js",
"node_modules/.placeholder",
"src/assets/logo.png",
"src/components/Hello.vue",
"src/App.vue",
"src/main.js",
"static/.gitkeep",
"test/unit/index.js",
".babelrc",
".editorconfig",
".eslintrc.js",
".gitignore",
"index.html",
"package.json",
"README.md",
]:
fp = root / rel
fp.parent.mkdir(parents=True, exist_ok=True)
fp.write_text("", encoding="utf-8")
top_dirs = sorted(p.name for p in root.iterdir() if p.is_dir())
src_entries = sorted(p.name for p in (root / "src").iterdir())
dot_files = sorted(p.name for p in root.iterdir() if p.is_file() and p.name.startswith("."))
root_files = sorted(p.name for p in root.iterdir() if p.is_file() and not p.name.startswith("."))
res1 = {
"top_dirs": top_dirs,
"src_entries": src_entries,
"dot_files": dot_files,
"root_files": root_files,
}
exp1 = {
"top_dirs": ["build", "config", "node_modules", "src", "static", "test"],
"src_entries": ["App.vue", "assets", "components", "main.js"],
"dot_files": [".babelrc", ".editorconfig", ".eslintrc.js", ".gitignore"],
"root_files": ["README.md", "index.html", "package.json"],
}
cases.append({
"name": "project_root_and_src_layout",
"passed": res1 == exp1,
"expected": exp1,
"actual": res1,
})
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page()
page.set_content(HTML_DOC)
# Case 2: App.vue SFC structure, component registration & computed styles
res2 = page.evaluate("""() => {
const app = document.getElementById('app');
const cs = getComputedStyle(app);
return {
app_name: window.appComponent.name,
child_registered: 'Hello' in window.appComponent.components,
has_logo: document.getElementById('app-logo') !== null,
text_align: cs.textAlign,
color: cs.color,
margin_top: cs.marginTop
};
}""")
exp2 = {
"app_name": "app",
"child_registered": True,
"has_logo": True,
"text_align": "center",
"color": "rgb(44, 62, 80)",
"margin_top": "60px",
}
cases.append({
"name": "app_vue_sfc_composition",
"passed": res2 == exp2,
"expected": exp2,
"actual": res2,
})
# Case 3: Hello.vue data() mutation and live DOM update
res3 = page.evaluate("""() => {
const before = document.getElementById('hello-msg').textContent;
window.helloState.msg = '欢迎来到蓝色天空 Vue 2 教程!';
const after = document.getElementById('hello-msg').textContent;
return { before, after };
}""")
exp3 = {
"before": "Welcome to Your Vue.js App",
"after": "欢迎来到蓝色天空 Vue 2 教程!",
}
cases.append({
"name": "hello_vue_data_update_hmr",
"passed": res3 == exp3,
"expected": exp3,
"actual": res3,
})
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/vue2-vue-directory-structure-lab/explore-vue-directory-structure.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-vue-directory-structure.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a782.zip
课程关联工程包(登记关联22章) · 含当前章节或实例 · mbskycom202612a776.zip