import sys, time, subprocess, os from playwright.sync_api import sync_playwright wait = float(sys.argv[1]) if len(sys.argv) > 1 else 20 out = sys.argv[2] if len(sys.argv) > 2 else '/tmp/opencode/shot.png' keys = sys.argv[3] if len(sys.argv) > 3 else '' srv = subprocess.Popen(['python3', '-m', 'http.server', '8765', '--directory', os.path.join(os.path.dirname(__file__), '..', 'web')], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) time.sleep(1) try: with sync_playwright() as p: b = p.chromium.launch(args=['--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage']) pg = b.new_page(viewport={'width': 1280, 'height': 900}) logs = [] pg.on('console', lambda m: logs.append(m.text)) pg.on('pageerror', lambda e: logs.append('PAGEERR ' + str(e))) pg.goto('http://localhost:8765/index.html') t0 = time.time() sent = False while time.time() - t0 < wait: time.sleep(1) if keys and not sent and time.time() - t0 > float(keys.split(':')[0]): sent = True # click virtual key by data-code for code in keys.split(':')[1].split(','): pg.click(f'.key[data-code="{code}"]') time.sleep(0.3) pg.screenshot(path=out, timeout=90000) print('STAT:', pg.inner_text('#stat')); print('SERIAL:', pg.inner_text('#serial')[-1500:]) print('LOGS:', logs[-10:]) b.close() finally: srv.terminate()