|
| 1 | +"""Headless Chromium smoke test usable on any vip-supported platform. |
| 2 | +
|
| 3 | +Mirrors microsoft/playwright#40312's manual test: launches Chromium |
| 4 | +headlessly, exercises basic page interaction and JS evaluation, and |
| 5 | +takes a screenshot. Exits non-zero on any failure so docker run / |
| 6 | +the GitHub Actions step propagates the result. |
| 7 | +""" |
| 8 | + |
| 9 | +import platform as _stdplatform |
| 10 | +import re |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +from playwright.sync_api import sync_playwright |
| 15 | + |
| 16 | + |
| 17 | +def _platform_label() -> str: |
| 18 | + """Best-effort short label like 'rhel9', 'leap15', 'macos-14.5', or 'unknown'.""" |
| 19 | + if sys.platform == "darwin": |
| 20 | + ver = _stdplatform.mac_ver()[0] or "unknown" |
| 21 | + return f"macos-{ver}" |
| 22 | + try: |
| 23 | + text = Path("/etc/os-release").read_text() |
| 24 | + except OSError: |
| 25 | + return "unknown" |
| 26 | + distro_id = "" |
| 27 | + version_major = "" |
| 28 | + m_id = re.search(r'^ID="?([^"\n]+)"?', text, re.MULTILINE) |
| 29 | + if m_id: |
| 30 | + distro_id = m_id.group(1).strip() |
| 31 | + m_ver = re.search(r'^VERSION_ID="?(\d+)', text, re.MULTILINE) |
| 32 | + if m_ver: |
| 33 | + version_major = m_ver.group(1) |
| 34 | + label = f"{distro_id}{version_major}".strip() |
| 35 | + return label or "unknown" |
| 36 | + |
| 37 | + |
| 38 | +def main() -> None: |
| 39 | + label = _platform_label() |
| 40 | + with sync_playwright() as p: |
| 41 | + browser = p.chromium.launch(headless=True, channel="chromium-headless-shell") |
| 42 | + page = browser.new_page() |
| 43 | + page.goto( |
| 44 | + "data:text/html,<title>vip smoke</title><h1 id=h>ok</h1><input id=i><div id=r></div>" |
| 45 | + ) |
| 46 | + title = page.title() |
| 47 | + if title != "vip smoke": |
| 48 | + raise RuntimeError(f"unexpected title: {title!r}") |
| 49 | + page.fill("#i", f"hello from {label}") |
| 50 | + page.evaluate( |
| 51 | + "document.getElementById('r').textContent = document.getElementById('i').value" |
| 52 | + ) |
| 53 | + text = page.text_content("#r") |
| 54 | + if text != f"hello from {label}": |
| 55 | + raise RuntimeError(f"unexpected DOM text: {text!r}") |
| 56 | + ua = page.evaluate("navigator.userAgent") |
| 57 | + if "HeadlessChrome" not in ua: |
| 58 | + raise RuntimeError(f"unexpected UA: {ua!r}") |
| 59 | + page.screenshot(path="/tmp/smoke.png") |
| 60 | + browser.close() |
| 61 | + print(f"PASS: {label} headless chromium smoke") |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + main() |
0 commit comments