BlueGit

projects-timfalken-com allianties

Files

allianties / test / ui-playwright.py

ui-playwright.py

ui-playwright.py 14.1 KiB Download Raw
1#!/usr/bin/env python3
2"""Desktop + mobile screenshots and a two-player local flow."""
3from __future__ import annotations
4
5import sys
6from pathlib import Path
7from urllib.parse import quote
8
9from playwright.sync_api import sync_playwright, expect
10
11SHOTS = Path('/workspace/allianties-shots')
12SHOTS.mkdir(parents=True, exist_ok=True)
13BASE = sys.argv[1] if len(sys.argv) > 1 else 'http://127.0.0.1:8080'
14
15
16def shot(page, name, full=False):
17 path = SHOTS / f'{name}.png'
18 page.screenshot(path=str(path), full_page=full)
19 print('shot', path)
20
21
22def shot_full(page, name):
23 shot(page, name, full=True)
24
25
26def main():
27 with sync_playwright() as p:
28 browser = p.chromium.launch(headless=True)
29 desk = browser.new_context(viewport={'width': 1280, 'height': 800}, locale='en-GB')
30 page = desk.new_page()
31 page.goto(BASE + '/', wait_until='networkidle')
32 page.wait_for_selector('#menu h1')
33 expect(page.locator('#menu h1')).to_have_text('Allianties')
34 shot_full(page, 'desktop-menu-guest')
35
36 # Flag picker
37 page.locator('#lang-current').click()
38 page.wait_for_selector('#lang-list:not([hidden])')
39 shot(page, 'desktop-lang-picker')
40 page.locator('#lang-current').click() # close, stay in English
41 page.wait_for_timeout(200)
42
43 # Login test user 1
44 page.goto(BASE + '/auth.php?test=1', wait_until='networkidle')
45 page.wait_for_selector('#btn-create')
46 shot_full(page, 'desktop-menu-loggedin')
47
48 page.locator('#btn-create').click()
49 page.wait_for_selector('#lobby:not([hidden])')
50 page.wait_for_timeout(200)
51 shot_full(page, 'desktop-lobby')
52
53 # Enable shrink, AP loot, cease-fire, set AP time, show clock + vis options
54 page.locator('#rule-shrink').check()
55 page.locator('#rule-ap-loot').check()
56 page.locator('#rule-ap-time').fill('00:00')
57 page.locator('#rule-ceasefire').check()
58 page.wait_for_selector('#ceasefire-editor:not([hidden])')
59 page.wait_for_timeout(200)
60 shot_full(page, 'desktop-lobby-rules-clock')
61
62 page.locator('#cf-add').click()
63 page.wait_for_timeout(300)
64 # Use a daytime range so we can still shoot later (not wrapping current night)
65 # Fill via evaluate so locale 12h inputs don't bite.
66 page.evaluate("""() => {
67 const inputs = document.querySelectorAll('#cf-ranges input[type=time]');
68 if (inputs[0]) inputs[0].value = '10:00';
69 if (inputs[1]) inputs[1].value = '12:00';
70 inputs[0] && inputs[0].dispatchEvent(new Event('change', { bubbles: true }));
71 }""")
72 page.wait_for_timeout(400)
73 shot_full(page, 'desktop-lobby-cf-range')
74 shot_full(page, 'desktop-lobby-options') # time picker, loot, vis, clock
75
76 # Scroll visibility into view
77 vis = page.locator('#lobby-visibility')
78 if vis.count():
79 vis.scroll_into_view_if_needed()
80 page.wait_for_timeout(150)
81 shot(page, 'desktop-lobby-visibility')
82
83 code = page.locator('#lobby-code').inner_text().strip()
84 print('lobby code', code)
85
86 # Turn cease-fire OFF before start so shots are legal; keep the other options.
87 # (Clock already screenshotted.) 10:00-12:00 is already off at night, fine.
88
89 desk2 = browser.new_context(viewport={'width': 1280, 'height': 800}, locale='en-GB')
90 page2 = desk2.new_page()
91 next_url = quote('index.html?join=' + code, safe='')
92 page2.goto(BASE + '/auth.php?test=2&next=' + next_url, wait_until='networkidle')
93 print('player2 url', page2.url)
94 try:
95 page2.wait_for_selector('#lobby:not([hidden]), #game:not([hidden])', timeout=15000)
96 except Exception:
97 shot(page2, 'debug-player2')
98 print('player2 text', page2.locator('body').inner_text()[:800])
99 raise
100 page2.wait_for_timeout(300)
101 shot_full(page2, 'desktop-player2-lobby')
102
103 page.wait_for_function(
104 "() => document.querySelectorAll('#lobby-players li').length >= 2",
105 timeout=15000,
106 )
107 shot_full(page, 'desktop-lobby-two-players')
108 page.locator('#btn-start').click()
109 page.wait_for_selector('#game:not([hidden])')
110 page.wait_for_timeout(400)
111 expect(page.locator('#ap-hms')).to_be_visible()
112 shot(page, 'desktop-game')
113
114 # Click own tank, screenshot selection + move targets + timeline controls
115 pieces = page.locator('#board .cell:has(.piece, .piece-initials)')
116 print('pieces', pieces.count())
117 if pieces.count() >= 1:
118 pieces.nth(0).click()
119 page.wait_for_timeout(250)
120 shot(page, 'desktop-game-selected')
121 # Move toward the other player if a move-target exists
122 targets = page.locator('#board .cell.move-target')
123 if targets.count() >= 1:
124 # Prefer a cell closer to the bottom (higher y) — downward toward P2
125 targets.last.click()
126 page.wait_for_timeout(500)
127 shot(page, 'desktop-game-moved')
128
129 # Player 2: after poll, should see the move; in range now, can give / attack
130 page2.wait_for_timeout(3500)
131 # If still lobby, start already happened — poll should switch to game
132 if page2.locator('#lobby:not([hidden])').count():
133 page2.wait_for_selector('#game:not([hidden])', timeout=10000)
134 page2.wait_for_selector('#game:not([hidden])', timeout=10000)
135 page2.wait_for_timeout(300)
136 shot(page2, 'desktop-game-p2')
137
138 # Click the opponent (T1, first or the non-own piece)
139 def click_opponent(pg):
140 cells = pg.locator('#board .cell:has(.piece, .piece-initials)')
141 n = cells.count()
142 # Click each until the sheet opens
143 for i in range(n):
144 cells.nth(i).click()
145 pg.wait_for_timeout(200)
146 if pg.locator('#sheet:not([hidden])').count():
147 return True
148 return False
149
150 if click_opponent(page2):
151 shot(page2, 'desktop-player-menu')
152 # Info
153 page2.locator('#sheet-body button').first.click()
154 page2.wait_for_selector('#modal:not([hidden])')
155 page2.wait_for_timeout(200)
156 shot(page2, 'desktop-info')
157 page2.locator('#modal-close').click()
158 page2.wait_for_timeout(150)
159
160 if click_opponent(page2):
161 # Give AP — the Give button is usually 3rd (Info, Attack, Give, Message)
162 btns = page2.locator('#sheet-body button')
163 give = None
164 for i in range(btns.count()):
165 t = btns.nth(i).inner_text()
166 if 'Give' in t or 'geven' in t.lower() or 'AP' in t:
167 give = btns.nth(i)
168 break
169 if give is None and btns.count() >= 3:
170 give = btns.nth(2)
171 if give is not None and give.is_disabled():
172 # Opponent out of range (random spawn): giving is legitimately disabled.
173 print('give disabled (out of range), skipping')
174 page2.locator('#sheet-close').click()
175 page2.wait_for_timeout(150)
176 give = None
177 if give is not None:
178 give.click()
179 page2.wait_for_selector('#modal:not([hidden])')
180 page2.wait_for_timeout(200)
181 shot(page2, 'desktop-give-ap')
182 # Actually send 1 AP so we get a green arrow in playback
183 send = page2.locator('#modal-body button').last
184 if not send.is_disabled():
185 send.click()
186 page2.wait_for_timeout(600)
187 else:
188 page2.locator('#modal-close').click()
189
190 # Chat
191 if click_opponent(page2):
192 btns = page2.locator('#sheet-body button')
193 chat = None
194 for i in range(btns.count()):
195 t = btns.nth(i).inner_text()
196 if 'message' in t.lower() or 'bericht' in t.lower() or 'chat' in t.lower():
197 chat = btns.nth(i)
198 break
199 if chat is None and btns.count() >= 4:
200 chat = btns.nth(3)
201 if chat is not None:
202 chat.click()
203 page2.wait_for_selector('#modal:not([hidden])')
204 page2.wait_for_timeout(200)
205 # type a message
206 inp = page2.locator('#modal-body input[type="text"]')
207 if inp.count():
208 inp.fill('Good luck!')
209 shot(page2, 'desktop-chat')
210 form_btn = page2.locator('#modal-body button[type="submit"]')
211 if form_btn.count():
212 form_btn.click()
213 page2.wait_for_timeout(400)
214 shot(page2, 'desktop-chat-sent')
215 page2.locator('#modal-close').click()
216
217 # Advance a day on player 1 so they have AP again, then shoot (red arrow)
218 page.evaluate("""async () => {
219 if (window.Api && App.game) {
220 try { await Api.debugAdvance(App.game.id, 1); await App.refreshFull(); }
221 catch (e) { console.error(e); }
222 }
223 }""")
224 page.wait_for_timeout(500)
225 if click_opponent(page):
226 btns = page.locator('#sheet-body button')
227 atk = None
228 for i in range(btns.count()):
229 t = btns.nth(i).inner_text()
230 if 'Attack' in t or 'Aanval' in t or 'Shoot' in t or 'schiet' in t.lower():
231 atk = btns.nth(i)
232 break
233 if atk is None and btns.count() >= 2:
234 atk = btns.nth(1)
235 if atk is not None and not atk.is_disabled():
236 atk.click()
237 page.wait_for_timeout(500)
238 shot(page, 'desktop-game-after-shot')
239
240 # Close any sheet left open (e.g. attack disabled when out of range)
241 if page.locator('#sheet:not([hidden])').count():
242 page.locator('#sheet-close').click()
243 page.wait_for_timeout(150)
244
245 # Playback arrows: rewind and play
246 if page.locator('#tl-back').count():
247 # Jump to start via stepping back many times, then play
248 for _ in range(8):
249 page.locator('#tl-back').click()
250 page.wait_for_timeout(80)
251 shot(page, 'desktop-timeline-start')
252 page.locator('#tl-play').click()
253 # Mid-playback: wait enough for a give/shoot/move arrow to show
254 page.wait_for_timeout(700)
255 shot(page, 'desktop-playback-arrows')
256 page.wait_for_timeout(900)
257 shot(page, 'desktop-playback-arrows-2')
258 # Pause, step forward, jump to present
259 if page.locator('#tl-play').get_attribute('title') and 'Pause' in (page.locator('#tl-play').get_attribute('title') or ''):
260 page.locator('#tl-play').click()
261 page.locator('#tl-fwd').click()
262 page.wait_for_timeout(200)
263 shot(page, 'desktop-timeline-step')
264 page.locator('#tl-live').click()
265 page.wait_for_timeout(200)
266 shot(page, 'desktop-timeline-live')
267
268 # Settings / surrender (don't confirm)
269 page.locator('#btn-settings').click()
270 page.wait_for_selector('#settings-panel:not([hidden])')
271 shot(page, 'desktop-settings')
272 page.locator('#btn-surrender').click()
273 page.wait_for_selector('#modal:not([hidden])')
274 shot(page, 'desktop-surrender-confirm')
275 page.locator('#modal-close').click()
276
277 # --- Mobile ---
278 mobile = browser.new_context(
279 viewport={'width': 390, 'height': 844},
280 is_mobile=True,
281 has_touch=True,
282 user_agent='Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1',
283 locale='en-GB',
284 )
285 mp = mobile.new_page()
286 mp.goto(BASE + '/', wait_until='networkidle')
287 shot(mp, 'mobile-menu-guest')
288 mp.locator('#lang-current').click()
289 mp.wait_for_selector('#lang-list:not([hidden])')
290 shot(mp, 'mobile-lang-picker')
291
292 mp.goto(BASE + '/auth.php?test=3', wait_until='networkidle')
293 mp.wait_for_selector('#btn-create, #menu-user')
294 shot(mp, 'mobile-menu')
295 mp.locator('#btn-create').click()
296 mp.wait_for_selector('#lobby:not([hidden])')
297 mp.locator('#rule-ceasefire').check()
298 mp.locator('#rule-ap-loot').wait_for()
299 mp.wait_for_timeout(300)
300 shot_full(mp, 'mobile-lobby-clock')
301 mp.locator('#rule-ap-time').scroll_into_view_if_needed()
302 shot(mp, 'mobile-lobby-ap-time')
303 vis = mp.locator('#lobby-visibility')
304 if vis.count():
305 vis.scroll_into_view_if_needed()
306 shot(mp, 'mobile-lobby-visibility')
307
308 # Player 2's running game on mobile
309 mobile2 = browser.new_context(
310 viewport={'width': 390, 'height': 844},
311 is_mobile=True,
312 has_touch=True,
313 locale='en-GB',
314 )
315 m2 = mobile2.new_page()
316 m2.goto(BASE + '/auth.php?test=2', wait_until='networkidle')
317 m2.wait_for_selector('#menu, #game, #lobby')
318 if m2.locator('#games-list button').count() > 0:
319 m2.locator('#games-list button').first.click()
320 m2.wait_for_selector('#game:not([hidden]), #lobby:not([hidden])')
321 m2.wait_for_timeout(400)
322 shot(m2, 'mobile-game')
323 if m2.locator('#ap-hms').count():
324 shot(m2, 'mobile-game-countdown')
325 # playback on mobile
326 if m2.locator('#tl-play').count() and m2.locator('#game:not([hidden])').count():
327 m2.locator('#tl-back').click()
328 m2.locator('#tl-back').click()
329 m2.locator('#tl-play').click()
330 m2.wait_for_timeout(700)
331 shot(m2, 'mobile-playback-arrows')
332
333 desk.close()
334 desk2.close()
335 mobile.close()
336 mobile2.close()
337 browser.close()
338 print('playwright done')
339
340
341if __name__ == '__main__':
342 main()