蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线 Chromium 核验 ng-click 递增计数及 personCtrl 联动 ng-hide/ng-show 切换

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-html-events.py
explore-html-events.py172 行
"""Offline Playwright Chromium lab for AngularJS Ch 15: html-events."""
import json
from playwright.sync_api import sync_playwright

HTML_DOC = r"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>AngularJS Events Lab</title>
<style>.ng-hide { display: none !important; }</style>
</head>
<body>
  <div id="sec-click">
    <button id="btn-count">点我!</button>
    <p id="out-count"></p>
  </div>

  <div id="sec-hide">
    <button id="btn-toggle-hide">隐藏/显示</button>
    <p id="panel-hide">
      名: <input id="hide-first" type="text"><br>
      姓: <input id="hide-last" type="text"><br>
      Full Name: <span id="hide-fullname"></span>
    </p>
  </div>

  <div id="sec-show">
    <button id="btn-toggle-show">隐藏/显示</button>
    <p id="panel-show">
      名: <input id="show-first" type="text"><br>
      姓: <input id="show-last" type="text"><br>
      姓名: <span id="show-fullname"></span>
    </p>
  </div>

  <script>
  (function () {
    // 1. ng-click="count = count + 1" (AngularJS treats undefined + 1 as 1)
    let count;
    const outCount = document.getElementById("out-count");
    document.getElementById("btn-count").addEventListener("click", () => {
      count = (count || 0) + 1;
      outCount.textContent = String(count);
    });

    // 2. ng-hide="myVar" with initial myVar = false
    const hideScope = { firstName: "John", lastName: "Doe", myVar: false };
    const hideFirst = document.getElementById("hide-first");
    const hideLast = document.getElementById("hide-last");
    const hideFull = document.getElementById("hide-fullname");
    const panelHide = document.getElementById("panel-hide");
    function renderHide() {
      hideFirst.value = hideScope.firstName;
      hideLast.value = hideScope.lastName;
      hideFull.textContent = hideScope.firstName + " " + hideScope.lastName;
      panelHide.classList.toggle("ng-hide", !!hideScope.myVar);
    }
    document.getElementById("btn-toggle-hide").addEventListener("click", () => {
      hideScope.myVar = !hideScope.myVar;
      renderHide();
    });
    hideFirst.addEventListener("input", () => { hideScope.firstName = hideFirst.value; renderHide(); });
    hideLast.addEventListener("input", () => { hideScope.lastName = hideLast.value; renderHide(); });
    renderHide();

    // 3. ng-show="myVar" with initial myVar = true
    const showScope = { firstName: "John", lastName: "Doe", myVar: true };
    const showFirst = document.getElementById("show-first");
    const showLast = document.getElementById("show-last");
    const showFull = document.getElementById("show-fullname");
    const panelShow = document.getElementById("panel-show");
    function renderShow() {
      showFirst.value = showScope.firstName;
      showLast.value = showScope.lastName;
      showFull.textContent = showScope.firstName + " " + showScope.lastName;
      panelShow.classList.toggle("ng-hide", !showScope.myVar);
    }
    document.getElementById("btn-toggle-show").addEventListener("click", () => {
      showScope.myVar = !showScope.myVar;
      renderShow();
    });
    showFirst.addEventListener("input", () => { showScope.firstName = showFirst.value; renderShow(); });
    showLast.addEventListener("input", () => { showScope.lastName = showLast.value; renderShow(); });
    renderShow();
  })();
  </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)
        try:
            page = browser.new_page()
            page.set_content(HTML_DOC)

            # Case 1: ng-click counter increment
            before = page.text_content("#out-count")
            page.click("#btn-count")
            after1 = page.text_content("#out-count")
            page.click("#btn-count")
            page.click("#btn-count")
            after3 = page.text_content("#out-count")
            actual1 = {"initial": before, "after_1_click": after1, "after_3_clicks": after3}
            expected1 = {"initial": "", "after_1_click": "1", "after_3_clicks": "3"}
            cases.append({"name": "ng_click_counter_increment", "expected": expected1, "actual": actual1, "passed": actual1 == expected1})

            # Case 2: ng-hide toggle + two-way personCtrl binding
            init_vis = page.evaluate('getComputedStyle(document.getElementById("panel-hide")).display')
            init_name = page.text_content("#hide-fullname")
            page.fill("#hide-first", "Alice")
            updated_name = page.text_content("#hide-fullname")
            page.click("#btn-toggle-hide")
            hidden_vis = page.evaluate('getComputedStyle(document.getElementById("panel-hide")).display')
            page.click("#btn-toggle-hide")
            restored_vis = page.evaluate('getComputedStyle(document.getElementById("panel-hide")).display')
            actual2 = {
                "initial_display": init_vis,
                "initial_fullname": init_name,
                "updated_fullname": updated_name,
                "after_first_toggle": hidden_vis,
                "after_second_toggle": restored_vis,
            }
            expected2 = {
                "initial_display": "block",
                "initial_fullname": "John Doe",
                "updated_fullname": "Alice Doe",
                "after_first_toggle": "none",
                "after_second_toggle": "block",
            }
            cases.append({"name": "ng_hide_toggle_and_person_binding", "expected": expected2, "actual": actual2, "passed": actual2 == expected2})

            # Case 3: ng-show toggle with initial myVar = true
            s_init = page.evaluate('getComputedStyle(document.getElementById("panel-show")).display')
            page.click("#btn-toggle-show")
            s_hidden = page.evaluate('getComputedStyle(document.getElementById("panel-show")).display')
            page.click("#btn-toggle-show")
            s_shown = page.evaluate('getComputedStyle(document.getElementById("panel-show")).display')
            page.fill("#show-last", "Smith")
            s_name = page.text_content("#show-fullname")
            actual3 = {
                "initial_display": s_init,
                "after_first_toggle": s_hidden,
                "after_second_toggle": s_shown,
                "updated_fullname": s_name,
            }
            expected3 = {
                "initial_display": "block",
                "after_first_toggle": "none",
                "after_second_toggle": "block",
                "updated_fullname": "John Smith",
            }
            cases.append({"name": "ng_show_toggle_and_person_binding", "expected": expected3, "actual": actual3, "passed": actual3 == expected3})
        finally:
            browser.close()

    print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/angularjs-html-events-lab/explore-html-events.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-html-events.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:AngularJS

课程关联工程包(登记关联26章) · 含当前章节或实例 · mbskycom202612a851.zip

下载课程关联工程包:AngularJS

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a861.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习