"""Offline Playwright Chromium lab for Vue 2 Chapter 02 (vue-install)."""
import json
from playwright.sync_api import sync_playwright
MINI_VUE2_JS = r"""
(function (global) {
function observe(data) {
Object.keys(data).forEach(key => {
let val = data[key];
const listeners = [];
data['__dep_' + key] = listeners;
Object.defineProperty(data, key, {
enumerable: true,
configurable: true,
get() { return val; },
set(v) { val = v; listeners.forEach(fn => fn()); }
});
});
}
class Vue {
constructor(options) {
this.$data = options.data || {};
observe(this.$data);
Object.keys(this.$data).forEach(k => {
if (k.startsWith('__dep_')) return;
Object.defineProperty(this, k, {
get: () => this.$data[k],
set: (v) => { this.$data[k] = v; }
});
});
this.$el = document.querySelector(options.el);
const p = this.$el.querySelector('p');
if (p) {
const tpl = p.textContent;
const render = () => {
p.textContent = tpl.replace(/\{\{(.+?)\}\}/g, (_, exp) => this[exp.trim()]);
};
Object.keys(this.$data).forEach(k => {
if (k.startsWith('__dep_')) this.$data[k].push(render);
});
render();
}
}
}
global.Vue = Vue;
})(window);
"""
HTML_DOC = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><title>Vue 2 安装方式离线核验</title></head>
<body>
<div id="app"><p>{{{{ message }}}}</p></div>
<div id="cli-welcome">
<h1>Welcome to Your Vue.js App</h1>
<h2>Essential Links</h2>
<ul><li>Core Docs</li><li>Forum</li></ul>
</div>
<script>{MINI_VUE2_JS}</script>
<script>
window.vm = new Vue({{
el: '#app',
data: {{ message: 'Hello Vue.js!' }}
}});
</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 = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page()
page.set_content(HTML_DOC)
# Case 1: Standalone script & CDN script-tag mount + ES5 requirement
res1 = page.evaluate("""() => {
const cdns = {
standalone: 'vue.min.js',
cdnjs: 'https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.8/vue.min.js',
staticfile: 'https://cdn.staticfile.net/vue/2.2.2/vue.min.js',
unpkg: 'https://unpkg.com/vue@2.6.14/dist/vue.min.js'
};
const rendered = document.querySelector('#app p').textContent;
window.vm.message = 'Standalone OK';
const updated = document.querySelector('#app p').textContent;
return {
rendered,
updated,
cdn_count: Object.keys(cdns).length,
requires_es5_define_property: typeof Object.defineProperty === 'function'
};
}""")
exp1 = {
"rendered": "Hello Vue.js!",
"updated": "Standalone OK",
"cdn_count": 4,
"requires_es5_define_property": True,
}
cases.append({
"name": "standalone_and_cdn_script_mount",
"passed": res1 == exp1,
"expected": exp1,
"actual": res1,
})
# Case 2: npm/cnpm & vue-cli webpack scaffold workflow + welcome page DOM
res2 = page.evaluate("""() => {
const cmds = [
'npm -v',
'cnpm install vue',
'cnpm install --global vue-cli',
'vue init webpack my-project',
'cnpm run dev'
];
const h1 = document.querySelector('#cli-welcome h1').textContent;
const links = Array.from(document.querySelectorAll('#cli-welcome li')).map(el => el.textContent);
return {
cmd_count: cmds.length,
has_webpack_template: cmds[3].includes('webpack'),
welcome_heading: h1,
essential_links: links
};
}""")
exp2 = {
"cmd_count": 5,
"has_webpack_template": True,
"welcome_heading": "Welcome to Your Vue.js App",
"essential_links": ["Core Docs", "Forum"],
}
cases.append({
"name": "npm_cnpm_and_vue_cli_workflow",
"passed": res2 == exp2,
"expected": exp2,
"actual": res2,
})
# Case 3: npm run build dist/index.html absolute vs relative asset paths under file://
res3 = page.evaluate("""() => {
const base = 'file:///D:/my-project/dist/index.html';
const absCss = new URL('/static/css/app.33da80d69744798940b135da93bc7b98.css', base).href;
const absJs = new URL('/static/js/app.717bb358ddc19e181140.js', base).href;
const relCss = new URL('static/css/app.33da80d69744798940b135da93bc7b98.css', base).href;
const relJs = new URL('static/js/app.717bb358ddc19e181140.js', base).href;
return {
abs_css_escapes_dist: !absCss.includes('/my-project/dist/'),
abs_js_escapes_dist: !absJs.includes('/my-project/dist/'),
rel_css_inside_dist: relCss === 'file:///D:/my-project/dist/static/css/app.33da80d69744798940b135da93bc7b98.css',
rel_js_inside_dist: relJs === 'file:///D:/my-project/dist/static/js/app.717bb358ddc19e181140.js'
};
}""")
exp3 = {
"abs_css_escapes_dist": True,
"abs_js_escapes_dist": True,
"rel_css_inside_dist": True,
"rel_js_inside_dist": True,
}
cases.append({
"name": "dist_build_absolute_vs_relative_paths",
"passed": res3 == exp3,
"expected": exp3,
"actual": res3,
})
# Case 4: Editor & AI IDE cross-platform metadata check
res4 = page.evaluate("""() => {
const editors = {
vscode: { platforms: ['Windows', 'macOS', 'Linux'], open_source: true },
ai_ides: ['Trae', 'Qoder']
};
return {
vscode_platforms: editors.vscode.platforms,
vscode_open_source: editors.vscode.open_source,
ai_ides: editors.ai_ides
};
}""")
exp4 = {
"vscode_platforms": ["Windows", "macOS", "Linux"],
"vscode_open_source": True,
"ai_ides": ["Trae", "Qoder"],
}
cases.append({
"name": "editor_and_ai_ide_metadata",
"passed": res4 == exp4,
"expected": exp4,
"actual": res4,
})
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-install-lab/explore-vue-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-vue-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联22章) · 含当前章节或实例 · mbskycom202612a776.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a787.zip