BlueGit

projects-timfalken-com allianties

Files

allianties / test / ui-playwright.py

ui-playwright.py

ui-playwright.py 13.6 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:
172 give.click()
173 page2.wait_for_selector('#modal:not([hidden])')
174 page2.wait_for_timeout(200)
175 shot(page2, 'desktop-give-ap')
176 # Actually send 1 AP so we get a green arrow in playback
177 send = page2.locator('#modal-body button').last
178 if not send.is_disabled():
179 send.click()
180 page2.wait_for_timeout(600)
181 else:
182 page2.locator('#modal-close').click()
183
184 # Chat
185 if click_opponent(page2):
186 btns = page2.locator('#sheet-body button')
187 chat = None
188 for i in range(btns.count()):
189 t = btns.nth(i).inner_text()
190 if 'message' in t.lower() or 'bericht' in t.lower() or 'chat' in t.lower():
191 chat = btns.nth(i)
192 break
193 if chat is None and btns.count() >= 4:
194 chat = btns.nth(3)
195 if chat is not None:
196 chat.click()
197 page2.wait_for_selector('#modal:not([hidden])')
198 page2.wait_for_timeout(200)
199 # type a message
200 inp = page2.locator('#modal-body input[type="text"]')
201 if inp.count():
202 inp.fill('Good luck!')
203 shot(page2, 'desktop-chat')
204 form_btn = page2.locator('#modal-body button[type="submit"]')
205 if form_btn.count():
206 form_btn.click()
207 page2.wait_for_timeout(400)
208 shot(page2, 'desktop-chat-sent')
209 page2.locator('#modal-close').click()
210
211 # Advance a day on player 1 so they have AP again, then shoot (red arrow)
212 page.evaluate("""async () => {
213 if (window.Api && App.game) {
214 try { await Api.debugAdvance(App.game.id, 1); await App.refreshFull(); }
215 catch (e) { console.error(e); }
216 }
217 }""")
218 page.wait_for_timeout(500)
219 if click_opponent(page):
220 btns = page.locator('#sheet-body button')
221 atk = None
222 for i in range(btns.count()):
223 t = btns.nth(i).inner_text()
224 if 'Attack' in t or 'Aanval' in t or 'Shoot' in t or 'schiet' in t.lower():
225 atk = btns.nth(i)
226 break
227 if atk is None and btns.count() >= 2:
228 atk = btns.nth(1)
229 if atk is not None and not atk.is_disabled():
230 atk.click()
231 page.wait_for_timeout(500)
232 shot(page, 'desktop-game-after-shot')
233
234 # Playback arrows: rewind and play
235 if page.locator('#tl-back').count():
236 # Jump to start via stepping back many times, then play
237 for _ in range(8):
238 page.locator('#tl-back').click()
239 page.wait_for_timeout(80)
240 shot(page, 'desktop-timeline-start')
241 page.locator('#tl-play').click()
242 # Mid-playback: wait enough for a give/shoot/move arrow to show
243 page.wait_for_timeout(700)
244 shot(page, 'desktop-playback-arrows')
245 page.wait_for_timeout(900)
246 shot(page, 'desktop-playback-arrows-2')
247 # Pause, step forward, jump to present
248 if page.locator('#tl-play').get_attribute('title') and 'Pause' in (page.locator('#tl-play').get_attribute('title') or ''):
249 page.locator('#tl-play').click()
250 page.locator('#tl-fwd').click()
251 page.wait_for_timeout(200)
252 shot(page, 'desktop-timeline-step')
253 page.locator('#tl-live').click()
254 page.wait_for_timeout(200)
255 shot(page, 'desktop-timeline-live')
256
257 # Settings / surrender (don't confirm)
258 page.locator('#btn-settings').click()
259 page.wait_for_selector('#settings-panel:not([hidden])')
260 shot(page, 'desktop-settings')
261 page.locator('#btn-surrender').click()
262 page.wait_for_selector('#modal:not([hidden])')
263 shot(page, 'desktop-surrender-confirm')
264 page.locator('#modal-close').click()
265
266 # --- Mobile ---
267 mobile = browser.new_context(
268 viewport={'width': 390, 'height': 844},
269 is_mobile=True,
270 has_touch=True,
271 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',
272 locale='en-GB',
273 )
274 mp = mobile.new_page()
275 mp.goto(BASE + '/', wait_until='networkidle')
276 shot(mp, 'mobile-menu-guest')
277 mp.locator('#lang-current').click()
278 mp.wait_for_selector('#lang-list:not([hidden])')
279 shot(mp, 'mobile-lang-picker')
280
281 mp.goto(BASE + '/auth.php?test=3', wait_until='networkidle')
282 mp.wait_for_selector('#btn-create, #menu-user')
283 shot(mp, 'mobile-menu')
284 mp.locator('#btn-create').click()
285 mp.wait_for_selector('#lobby:not([hidden])')
286 mp.locator('#rule-ceasefire').check()
287 mp.locator('#rule-ap-loot').wait_for()
288 mp.wait_for_timeout(300)
289 shot_full(mp, 'mobile-lobby-clock')
290 mp.locator('#rule-ap-time').scroll_into_view_if_needed()
291 shot(mp, 'mobile-lobby-ap-time')
292 vis = mp.locator('#lobby-visibility')
293 if vis.count():
294 vis.scroll_into_view_if_needed()
295 shot(mp, 'mobile-lobby-visibility')
296
297 # Player 2's running game on mobile
298 mobile2 = browser.new_context(
299 viewport={'width': 390, 'height': 844},
300 is_mobile=True,
301 has_touch=True,
302 locale='en-GB',
303 )
304 m2 = mobile2.new_page()
305 m2.goto(BASE + '/auth.php?test=2', wait_until='networkidle')
306 m2.wait_for_selector('#menu, #game, #lobby')
307 if m2.locator('#games-list button').count() > 0:
308 m2.locator('#games-list button').first.click()
309 m2.wait_for_selector('#game:not([hidden]), #lobby:not([hidden])')
310 m2.wait_for_timeout(400)
311 shot(m2, 'mobile-game')
312 if m2.locator('#ap-hms').count():
313 shot(m2, 'mobile-game-countdown')
314 # playback on mobile
315 if m2.locator('#tl-play').count() and m2.locator('#game:not([hidden])').count():
316 m2.locator('#tl-back').click()
317 m2.locator('#tl-back').click()
318 m2.locator('#tl-play').click()
319 m2.wait_for_timeout(700)
320 shot(m2, 'mobile-playback-arrows')
321
322 desk.close()
323 desk2.close()
324 mobile.close()
325 mobile2.close()
326 browser.close()
327 print('playwright done')
328
329
330if __name__ == '__main__':
331 main()