test_firefox.py raw
1 #!/usr/bin/env python3
2 # Selenium/Firefox test script for gio demo.
3 # Requires LibreWolf + geckodriver + a real GPU display (not headless Xvfb).
4 # Run with: DISPLAY=:0 python3 test_firefox.py (or from the desktop)
5
6 from selenium import webdriver
7 from selenium.webdriver.firefox.options import Options
8 from selenium.webdriver.firefox.service import Service
9 from selenium.webdriver.common.action_chains import ActionChains
10 import time
11
12 URL = 'http://localhost:8765/'
13
14 def make_driver():
15 opts = Options()
16 opts.binary_location = '/usr/lib/librewolf/librewolf'
17 # No --headless: needs real GPU for WebGL2
18 svc = Service('/bin/geckodriver', log_output='/dev/null')
19 driver = webdriver.Firefox(service=svc, options=opts)
20 driver.set_window_size(1280, 900)
21 return driver
22
23 def test_button_click(driver):
24 driver.get(URL)
25 time.sleep(3)
26 canvas = driver.find_element('tag name', 'canvas')
27 cw = canvas.size['width']
28 ch = canvas.size['height']
29
30 # Click "Click me!" button at ~(50, 67)
31 ac = ActionChains(driver)
32 ac.move_to_element_with_offset(canvas, -cw//2 + 50, -ch//2 + 67)
33 ac.click()
34 ac.perform()
35 time.sleep(0.5)
36 driver.save_screenshot('/tmp/gio-after-click.png')
37 print('Button click: screenshot at /tmp/gio-after-click.png')
38
39 def test_scrollbar_drag(driver):
40 # Mouse down on scrollbar (right edge of list), drag down 80px
41 script = '''
42 const canvas = document.querySelector('canvas[style*="fixed"]');
43 const rect = canvas.getBoundingClientRect();
44 const sectionPad = 12, thick = 6;
45 const sbX = rect.left + rect.width - sectionPad - thick/2;
46 // List viewport at ~40% down the page
47 const listTopY = rect.top + rect.height * 0.4;
48 const ev = (t, x, y, b) => canvas.dispatchEvent(new MouseEvent(t, {
49 clientX: x, clientY: y, bubbles: true, buttons: b||0, button: 0
50 }));
51 ev('mousedown', sbX, listTopY, 1);
52 for (let i = 1; i <= 10; i++) ev('mousemove', sbX, listTopY + i*8, 1);
53 ev('mouseup', sbX, listTopY+80, 0);
54 return 'drag done';
55 '''
56 result = driver.execute_script(script)
57 time.sleep(0.5)
58 driver.save_screenshot('/tmp/gio-after-scroll.png')
59 print(f'Scrollbar drag: {result}, screenshot at /tmp/gio-after-scroll.png')
60
61 if __name__ == '__main__':
62 driver = make_driver()
63 try:
64 test_button_click(driver)
65 test_scrollbar_drag(driver)
66 finally:
67 driver.quit()
68