Files
CyberGril/samples/companion/tools/check_browser.py
T

137 lines
6.0 KiB
Python

"""Real Chromium/WebRTC check; fake devices are explicitly synthetic fixtures."""
import asyncio
import json
import pathlib
import time
from playwright.async_api import async_playwright
ROOT = pathlib.Path(__file__).resolve().parents[1]
OUT = ROOT / "evidence"
OUT.mkdir(exist_ok=True)
URL = "https://192.168.200.12:8014"
async def download(page, path):
await page.click("#record")
await page.wait_for_selector("#download:not([hidden])")
async with page.expect_download() as pending:
await page.click("#download")
await (await pending.value).save_as(path)
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(
args=["--no-sandbox", "--autoplay-policy=no-user-gesture-required"]
)
context = await browser.new_context(
ignore_https_errors=True, viewport={"width": 1280, "height": 800}
)
page = await context.new_page()
errors = []
page.on("pageerror", lambda e: errors.append(str(e)))
await page.goto(URL)
await page.wait_for_function(
"document.querySelector('#status').textContent.includes('就绪')"
)
await page.click("#record")
await page.wait_for_timeout(1000)
await page.fill("#text", "我今天下班了,有点累。请只用一句简短的话回应。")
await page.click("button[type=submit]")
await page.wait_for_function(
"window.sampleEvents.some(e=>e.event==='first-audible')", timeout=60000
)
await page.wait_for_timeout(1500)
await page.click("#cancel")
await page.wait_for_timeout(1500)
assert await page.evaluate("document.querySelector('#talk').srcObject===null")
await page.fill("#text", "你在看什么书?只说一句话。")
await page.click("button[type=submit]")
await page.wait_for_function(
"window.sampleEvents.filter(e=>e.event==='first-audible').length>=2", timeout=60000
)
await page.wait_for_timeout(6000)
await page.click("#back")
await page.wait_for_function(
"document.querySelector('#status').textContent.includes('已回到阅读')", timeout=15000
)
assert not await page.locator("#daily").evaluate("(v)=>v.paused")
await page.click("#pause")
assert await page.locator("#daily").evaluate("(v)=>v.paused")
await page.click("#pause")
await page.locator("summary").click()
await page.select_option("#weather", "clear")
assert "回退" in await page.locator("#selection").inner_text()
await page.click("#enter")
await page.wait_for_function(
"document.querySelector('#status').textContent.includes('正在听')", timeout=30000
)
await page.fill("#text", "这是取消准备阶段的测试,请简短回复。")
await page.click("button[type=submit]")
await page.wait_for_function(
"document.querySelector('#status').textContent.includes('准备回复')", timeout=30000
)
await page.wait_for_timeout(1000)
await page.click("#cancel")
await page.wait_for_timeout(6000)
assert await page.locator("#talk").evaluate("(v)=>v.srcObject===null")
await page.click("#back")
(OUT / "sample-events.json").write_text(
json.dumps(await page.evaluate("window.sampleEvents"), ensure_ascii=False, indent=2)
)
await download(page, OUT / "sample-browser-av.webm")
await page.screenshot(path=str(OUT / "desktop.png"))
await page.set_viewport_size({"width": 390, "height": 844})
await page.screenshot(path=str(OUT / "mobile.png"), full_page=True)
mobile_overflow = await page.evaluate("document.documentElement.scrollWidth > innerWidth")
# Failure tests use browser request routing, never stop shared models.
await page.route("**/api/offer", lambda route: route.abort())
await page.click("#retry")
await page.wait_for_function(
"document.querySelector('#status').textContent.includes('已停止交谈')", timeout=30000
)
await page.unroute("**/api/offer")
await page.click("#retry")
await page.wait_for_function(
"document.querySelector('#status').textContent.includes('正在听')", timeout=30000
)
await context.set_offline(True)
await page.wait_for_timeout(500)
assert await page.locator("#talk").evaluate("(v)=>v.srcObject===null")
await context.set_offline(False)
await page.reload()
await page.wait_for_function(
"document.querySelector('#status').textContent.includes('就绪')"
)
# This is a handler test, not a claim of real OS background testing.
await page.evaluate(
"Object.defineProperty(document,'hidden',{configurable:true,value:true});document.dispatchEvent(new Event('visibilitychange'))"
)
assert await page.locator("#daily").evaluate("(v)=>v.paused")
await page.evaluate(
"Object.defineProperty(document,'hidden',{configurable:true,value:false})"
)
await page.route("**/reading-rain-night.mp4", lambda route: route.abort())
await page.reload()
await page.wait_for_function(
"document.querySelector('#status').textContent.includes('素材丢失')", timeout=30000
)
result = {
"pageerrors": errors,
"mobile_overflow": mobile_overflow,
"visibility_test": "synthetic event only; real OS background not tested",
"cancel": "speaking and preparing",
"network": "offer abort, offline, recovery",
"missing_asset": "daily video request abort",
"time": time.time(),
}
# Events from the main continuous recording were captured before reload.
(OUT / "browser-check.json").write_text(json.dumps(result, ensure_ascii=False, indent=2))
await browser.close()
print(json.dumps(result, ensure_ascii=False), flush=True)
asyncio.run(main())