File size: 1,504 Bytes
d8afd6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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()