|
| 1 | +import pytest |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import multiprocessing |
| 5 | +import time |
| 6 | +import socket |
| 7 | +import re |
| 8 | + |
| 9 | +from playwright.sync_api import Page, expect |
| 10 | + |
| 11 | +# Ensure root directory is in sys.path |
| 12 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))) |
| 13 | + |
| 14 | +def get_free_port(): |
| 15 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 16 | + s.bind(('127.0.0.1', 0)) |
| 17 | + port = s.getsockname()[1] |
| 18 | + s.close() |
| 19 | + return port |
| 20 | + |
| 21 | +def run_server(port): |
| 22 | + from htag import Tag |
| 23 | + from htag.server import WebApp |
| 24 | + |
| 25 | + class App(Tag.App): |
| 26 | + def init(self): |
| 27 | + self.call = 0 |
| 28 | + self.btn = Tag.button("Update Body", _onclick=self.update_body) |
| 29 | + self += self.btn |
| 30 | + self["class"] = "initial-class" |
| 31 | + self["data-test"] = "foo" |
| 32 | + |
| 33 | + def update_body(self, ev): |
| 34 | + self.call += 1 |
| 35 | + self["class"] = f"updated-class-{self.call}" |
| 36 | + self["data-test"] = "bar" |
| 37 | + # Remove an old attribute and add a new one, to test attribute syncing |
| 38 | + del self["data-test"] |
| 39 | + self["data-new"] = "baz" |
| 40 | + self += Tag.div(f"Call {self.call}", _class="result-div") |
| 41 | + |
| 42 | + app = WebApp(App) |
| 43 | + app.run(port=port, open_browser=False) |
| 44 | + |
| 45 | +@pytest.fixture(scope="module") |
| 46 | +def app_port(): |
| 47 | + port = get_free_port() |
| 48 | + p = multiprocessing.Process(target=run_server, args=(port,)) |
| 49 | + p.start() |
| 50 | + |
| 51 | + # Wait for server to start |
| 52 | + time.sleep(3) |
| 53 | + |
| 54 | + yield port |
| 55 | + |
| 56 | + p.terminate() |
| 57 | + p.join() |
| 58 | + |
| 59 | +def test_body_update_no_dom_exception(app_port, page: Page): |
| 60 | + """ |
| 61 | + This test verifies that updating the <body> tag triggers the |
| 62 | + DOMParser fallback in client_js.py and doesn't throw a DOMException |
| 63 | + when reassigning outerHTML on document.body. |
| 64 | + """ |
| 65 | + url = f"http://127.0.0.1:{app_port}" |
| 66 | + page.goto(url) |
| 67 | + |
| 68 | + # Trap page errors to explicitly fail if DOMException occurs |
| 69 | + errors = [] |
| 70 | + page.on("pageerror", lambda err: errors.append(err)) |
| 71 | + |
| 72 | + # Check initial state |
| 73 | + body = page.locator("body") |
| 74 | + expect(body).to_have_class(re.compile(r".*initial-class.*")) |
| 75 | + |
| 76 | + # Click to update body |
| 77 | + page.click("button:has-text('Update Body')") |
| 78 | + |
| 79 | + # Verification: Did the innerHTML update? |
| 80 | + expect(page.locator("text=Call 1")).to_be_visible() |
| 81 | + |
| 82 | + # Verification: Did the classes update? |
| 83 | + expect(body).to_have_class(re.compile(r".*updated-class-1.*")) |
| 84 | + |
| 85 | + # Verification: Attributes synced properly? |
| 86 | + # Playwright locator.get_attribute |
| 87 | + assert body.get_attribute("data-new") == "baz" |
| 88 | + assert body.get_attribute("data-test") is None |
| 89 | + |
| 90 | + # Ensure no unhandled JS exceptions occurred! |
| 91 | + assert len(errors) == 0, f"Javascript errors found: {errors}" |
| 92 | + |
| 93 | + # Click again to ensure multiple updates work smoothly |
| 94 | + page.click("button:has-text('Update Body')") |
| 95 | + expect(page.locator("text=Call 2")).to_be_visible() |
| 96 | + expect(body).to_have_class(re.compile(r".*updated-class-2.*")) |
| 97 | + assert len(errors) == 0 |
0 commit comments