-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselenium_test_fixed.py
More file actions
executable file
·563 lines (465 loc) · 23.2 KB
/
selenium_test_fixed.py
File metadata and controls
executable file
·563 lines (465 loc) · 23.2 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#!/usr/bin/env python3
"""
SELENIUM-BASED REAL USER MULTIPLAYER GAME TESTING - FIXED VERSION
This script simulates actual user interactions with the multiplayer quiz game
and automatically captures debugging information when issues occur.
"""
import os
import sys
import time
import json
import logging
from datetime import datetime
from typing import List, Dict, Any, Optional
from dataclasses import dataclass, field
import traceback
import random
import string
# Install required packages if not available
def install_dependencies():
packages = ['selenium', 'webdriver-manager', 'requests']
for package in packages:
try:
__import__(package.replace('-', '_'))
except ImportError:
print(f"Installing {package}...")
os.system(f"pip3 install --user {package}")
install_dependencies()
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException, NoSuchElementException, WebDriverException
from webdriver_manager.chrome import ChromeDriverManager
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('selenium_test_log.txt'),
logging.StreamHandler()
]
)
@dataclass
class TestUser:
email: str
username: str
password: str
role: str = "player"
@dataclass
class TestResult:
test_name: str
status: str
duration: float
error_message: str = ""
screenshot_path: str = ""
console_logs: List[str] = field(default_factory=list)
class MultiplayerGameTester:
def __init__(self, base_url: str = "http://localhost:3000"):
self.base_url = base_url
self.drivers: List[webdriver.Chrome] = []
self.test_results: List[TestResult] = []
self.screenshots_dir = "test_screenshots"
self.logs_dir = "test_logs"
os.makedirs(self.screenshots_dir, exist_ok=True)
os.makedirs(self.logs_dir, exist_ok=True)
self.test_users = [
TestUser("host@gametest.com", "GameHost", "TestPass123!", "host"),
TestUser("player1@gametest.com", "Player1", "TestPass123!", "player"),
TestUser("player2@gametest.com", "Player2", "TestPass123!", "player"),
TestUser("player3@gametest.com", "Player3", "TestPass123!", "player")
]
self.room_code = ""
def setup_driver(self, headless: bool = False) -> webdriver.Chrome:
"""Create and configure Chrome WebDriver"""
try:
chrome_options = Options()
if headless:
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
self.drivers.append(driver)
return driver
except Exception as e:
logging.error(f"Failed to setup WebDriver: {e}")
raise
def capture_screenshot(self, driver: webdriver.Chrome, test_name: str) -> str:
"""Capture screenshot for debugging"""
try:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{test_name.replace(' ', '_')}_{timestamp}.png"
filepath = os.path.join(self.screenshots_dir, filename)
driver.save_screenshot(filepath)
logging.info(f"📸 Screenshot saved: {filepath}")
return filepath
except Exception as e:
logging.error(f"Failed to capture screenshot: {e}")
return ""
def capture_console_logs(self, driver: webdriver.Chrome) -> List[str]:
"""Capture browser console logs"""
try:
logs = driver.get_log('browser')
return [f"{log['level']}: {log['message']}" for log in logs]
except Exception as e:
logging.error(f"Failed to capture console logs: {e}")
return []
def wait_for_element(self, driver: webdriver.Chrome, by: By, value: str, timeout: int = 10) -> Any:
"""Wait for element to be present and clickable"""
try:
element = WebDriverWait(driver, timeout).until(
EC.element_to_be_clickable((by, value))
)
return element
except TimeoutException:
raise TimeoutException(f"Element not found: {by}={value}")
def execute_test(self, test_name: str, test_func, *args, **kwargs) -> TestResult:
"""Execute a test with error handling and logging"""
start_time = time.time()
logging.info(f"🧪 Starting test: {test_name}")
try:
test_func(*args, **kwargs)
duration = time.time() - start_time
result = TestResult(test_name, "PASS", duration)
logging.info(f"✅ PASSED: {test_name} ({duration:.2f}s)")
except Exception as e:
duration = time.time() - start_time
error_msg = str(e)
screenshot_path = ""
console_logs = []
# Capture debugging information
if self.drivers:
screenshot_path = self.capture_screenshot(self.drivers[0], test_name)
console_logs = self.capture_console_logs(self.drivers[0])
# Save page source for debugging
try:
page_source_file = os.path.join(self.logs_dir, f"{test_name.replace(' ', '_')}_page_source.html")
with open(page_source_file, 'w', encoding='utf-8') as f:
f.write(self.drivers[0].page_source)
except Exception as ex:
logging.error(f"Failed to save page source: {ex}")
result = TestResult(
test_name, "FAIL", duration, error_msg,
screenshot_path, console_logs
)
logging.error(f"❌ FAILED: {test_name} - {error_msg}")
logging.error(f"Stacktrace: {traceback.format_exc()}")
self.test_results.append(result)
return result
def test_frontend_accessibility(self):
"""Test if frontend is accessible"""
driver = self.setup_driver()
try:
driver.get(self.base_url)
WebDriverWait(driver, 10).until(
lambda d: d.execute_script("return document.readyState") == "complete"
)
# Check if page loaded successfully
page_title = driver.title
body = driver.find_element(By.TAG_NAME, "body")
assert body is not None, "Page body not found"
logging.info(f"✅ Frontend accessible - Title: {page_title}")
finally:
driver.quit()
if driver in self.drivers:
self.drivers.remove(driver)
def test_user_registration_and_login(self):
"""Test user registration and login flow"""
driver = self.setup_driver()
try:
user = self.test_users[0] # Test with first user
# Try registration first
driver.get(f"{self.base_url}/auth/register")
time.sleep(3)
try:
# Find registration form fields
email_field = driver.find_element(By.CSS_SELECTOR, "input[name='email'], input[type='email']")
username_field = driver.find_element(By.CSS_SELECTOR, "input[name='username']")
password_field = driver.find_element(By.CSS_SELECTOR, "input[name='password'], input[type='password']")
# Fill form
email_field.clear()
email_field.send_keys(user.email)
username_field.clear()
username_field.send_keys(user.username)
password_field.clear()
password_field.send_keys(user.password)
# Try to find additional required fields
try:
firstname_field = driver.find_element(By.CSS_SELECTOR, "input[name='firstName']")
firstname_field.clear()
firstname_field.send_keys(user.username)
lastname_field = driver.find_element(By.CSS_SELECTOR, "input[name='lastName']")
lastname_field.clear()
lastname_field.send_keys("Tester")
except NoSuchElementException:
pass # Optional fields
# Submit registration
submit_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit'], input[type='submit']")
submit_button.click()
time.sleep(5)
except Exception as e:
logging.info(f"Registration attempt failed for {user.username}: {e}")
# Test login
driver.get(f"{self.base_url}/auth/login")
time.sleep(3)
# Find login form
email_field = driver.find_element(By.CSS_SELECTOR, "input[name='email'], input[type='email']")
password_field = driver.find_element(By.CSS_SELECTOR, "input[name='password'], input[type='password']")
email_field.clear()
email_field.send_keys(user.email)
password_field.clear()
password_field.send_keys(user.password)
# Submit login
login_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit'], input[type='submit']")
login_button.click()
time.sleep(5)
# Check if login was successful
current_url = driver.current_url
if "dashboard" in current_url or "room" in current_url or not "login" in current_url:
logging.info(f"✅ User {user.username} logged in successfully")
else:
# Check page content for success indicators
body_text = driver.find_element(By.TAG_NAME, "body").text.lower()
if "welcome" in body_text or "dashboard" in body_text or "create room" in body_text:
logging.info(f"✅ User {user.username} appears to be logged in")
else:
logging.warning(f"⚠️ Login status unclear for {user.username}")
finally:
driver.quit()
if driver in self.drivers:
self.drivers.remove(driver)
def test_multiplayer_game_flow(self):
"""Test complete multiplayer game flow"""
host_driver = None
player_driver = None
try:
# Setup two browser instances
host_driver = self.setup_driver()
player_driver = self.setup_driver()
# STEP 1: Host login and room creation
logging.info("🎯 STEP 1: Host creates room")
host_user = self.test_users[0]
host_driver.get(f"{self.base_url}/auth/login")
time.sleep(3)
# Host login
email_field = host_driver.find_element(By.CSS_SELECTOR, "input[name='email'], input[type='email']")
password_field = host_driver.find_element(By.CSS_SELECTOR, "input[name='password'], input[type='password']")
email_field.send_keys(host_user.email)
password_field.send_keys(host_user.password)
login_button = host_driver.find_element(By.CSS_SELECTOR, "button[type='submit'], input[type='submit']")
login_button.click()
time.sleep(5)
# Look for room creation functionality
current_url = host_driver.current_url
page_text = host_driver.find_element(By.TAG_NAME, "body").text
# Try to find create room option
create_room_found = False
create_room_selectors = [
"//button[contains(text(), 'Create Room')]",
"//a[contains(text(), 'Create Room')]",
"//button[contains(text(), 'Create')]",
"//a[contains(text(), 'Create')]"
]
for selector in create_room_selectors:
try:
create_element = host_driver.find_element(By.XPATH, selector)
create_element.click()
time.sleep(3)
create_room_found = True
logging.info("✅ Found and clicked create room button")
break
except NoSuchElementException:
continue
if not create_room_found:
logging.warning("⚠️ Create room button not found - checking if already in room creation")
# Maybe we're already on a page where we can create a room
# STEP 2: Player login (simplified test)
logging.info("🎯 STEP 2: Player login test")
player_user = self.test_users[1]
player_driver.get(f"{self.base_url}/auth/login")
time.sleep(3)
# Player login
email_field = player_driver.find_element(By.CSS_SELECTOR, "input[name='email'], input[type='email']")
password_field = player_driver.find_element(By.CSS_SELECTOR, "input[name='password'], input[type='password']")
email_field.send_keys(player_user.email)
password_field.send_keys(player_user.password)
login_button = player_driver.find_element(By.CSS_SELECTOR, "button[type='submit'], input[type='submit']")
login_button.click()
time.sleep(5)
# Check if both users are logged in
host_page = host_driver.find_element(By.TAG_NAME, "body").text.lower()
player_page = player_driver.find_element(By.TAG_NAME, "body").text.lower()
if "login" not in host_page and "login" not in player_page:
logging.info("✅ Both host and player appear to be logged in")
else:
logging.warning("⚠️ Login status unclear for one or both users")
# STEP 3: Test UI interactions
logging.info("🎯 STEP 3: Testing UI interactions")
# Look for interactive elements
interactive_elements = host_driver.find_elements(By.CSS_SELECTOR, "button, a[href], input")
logging.info(f"Found {len(interactive_elements)} interactive elements on host page")
interactive_elements_player = player_driver.find_elements(By.CSS_SELECTOR, "button, a[href], input")
logging.info(f"Found {len(interactive_elements_player)} interactive elements on player page")
if len(interactive_elements) > 0 and len(interactive_elements_player) > 0:
logging.info("✅ Both pages have interactive elements")
else:
logging.warning("⚠️ Some pages may be missing interactive elements")
finally:
if host_driver:
host_driver.quit()
if host_driver in self.drivers:
self.drivers.remove(host_driver)
if player_driver:
player_driver.quit()
if player_driver in self.drivers:
self.drivers.remove(player_driver)
def test_navigation_and_pages(self):
"""Test various page navigation"""
driver = self.setup_driver()
try:
# Test different pages
pages_to_test = [
("/", "Home page"),
("/auth/login", "Login page"),
("/auth/register", "Registration page"),
("/dashboard", "Dashboard (may require auth)"),
("/room/TESTROOM", "Room page")
]
for path, description in pages_to_test:
try:
driver.get(f"{self.base_url}{path}")
time.sleep(2)
# Check if page loads
WebDriverWait(driver, 5).until(
lambda d: d.execute_script("return document.readyState") == "complete"
)
page_title = driver.title
logging.info(f"✅ {description} loaded - Title: {page_title}")
except Exception as e:
logging.warning(f"⚠️ {description} had issues: {e}")
finally:
driver.quit()
if driver in self.drivers:
self.drivers.remove(driver)
def run_all_tests(self):
"""Execute all automated tests"""
logging.info("🚀 Starting Selenium-based Real User Multiplayer Game Testing")
logging.info("=" * 80)
tests = [
("Frontend Accessibility", self.test_frontend_accessibility),
("User Registration and Login", self.test_user_registration_and_login),
("Navigation and Pages", self.test_navigation_and_pages),
("Multiplayer Game Flow", self.test_multiplayer_game_flow),
]
for test_name, test_func in tests:
self.execute_test(test_name, test_func)
time.sleep(2) # Brief pause between tests
self.generate_report()
self.provide_debugging_info()
def generate_report(self):
"""Generate comprehensive test report"""
logging.info("📊 Generating test report")
logging.info("=" * 80)
total_tests = len(self.test_results)
passed_tests = sum(1 for result in self.test_results if result.status == "PASS")
failed_tests = total_tests - passed_tests
success_rate = (passed_tests / total_tests * 100) if total_tests > 0 else 0
print(f"\n🎯 SELENIUM TEST RESULTS SUMMARY")
print("=" * 50)
print(f"✅ PASSED: {passed_tests}")
print(f"❌ FAILED: {failed_tests}")
print(f"📈 SUCCESS RATE: {success_rate:.1f}%")
print(f"🕒 TOTAL DURATION: {sum(r.duration for r in self.test_results):.2f}s")
if failed_tests > 0:
print(f"\n❌ FAILED TESTS WITH DEBUGGING INFO:")
print("-" * 40)
for i, result in enumerate([r for r in self.test_results if r.status == "FAIL"], 1):
print(f"{i}. {result.test_name}")
print(f" 💥 Error: {result.error_message}")
if result.screenshot_path:
print(f" 📸 Screenshot: {result.screenshot_path}")
if result.console_logs:
print(f" 📜 Console logs captured: {len(result.console_logs)} entries")
# Show first few console log entries
for log in result.console_logs[:3]:
print(f" {log}")
print("")
# Health assessment
print(f"\n🏥 SYSTEM ASSESSMENT:")
print("-" * 25)
if success_rate >= 75:
print("🎉 SYSTEM APPEARS TO BE WORKING WELL!")
elif success_rate >= 50:
print("⚠️ SYSTEM PARTIALLY FUNCTIONAL - SOME ISSUES DETECTED")
else:
print("🚨 SYSTEM NEEDS ATTENTION - MULTIPLE ISSUES FOUND")
def provide_debugging_info(self):
"""Provide specific debugging information and fix suggestions"""
print(f"\n🔧 DEBUGGING INFORMATION AND FIXES:")
print("-" * 45)
failed_results = [r for r in self.test_results if r.status == "FAIL"]
if not failed_results:
print("✅ No issues found - system is working correctly!")
return
for result in failed_results:
print(f"\n🐛 Issue: {result.test_name}")
print(f"Error: {result.error_message}")
# Provide specific fix suggestions based on error type
if "element not found" in result.error_message.lower():
print("🔧 Suggested Fix: Element locator issue")
print(" - Check if page structure changed")
print(" - Verify element IDs/classes in browser dev tools")
print(" - Consider waiting longer for dynamic content")
elif "timeout" in result.error_message.lower():
print("🔧 Suggested Fix: Timeout issue")
print(" - Increase wait time for slow-loading elements")
print(" - Check if backend services are responding")
print(" - Verify network connectivity")
elif "login" in result.test_name.lower():
print("🔧 Suggested Fix: Authentication issue")
print(" - Verify user credentials are correct")
print(" - Check if backend authentication API is working")
print(" - Ensure database is accessible")
elif "navigation" in result.test_name.lower():
print("🔧 Suggested Fix: Navigation/routing issue")
print(" - Check if frontend routes are properly configured")
print(" - Verify Next.js routing is working")
print(" - Check for JavaScript errors in console")
# System-wide recommendations
print(f"\n💡 GENERAL RECOMMENDATIONS:")
print("- Check browser console for JavaScript errors")
print("- Verify all services (frontend, backend, database, redis) are running")
print("- Review captured screenshots for visual issues")
print("- Test manually with browser to compare behavior")
print("- Check network tab for failed HTTP requests")
def cleanup(self):
"""Clean up resources"""
for driver in self.drivers:
try:
driver.quit()
except:
pass
self.drivers.clear()
def main():
"""Main execution"""
tester = MultiplayerGameTester()
try:
tester.run_all_tests()
except KeyboardInterrupt:
logging.info("🛑 Testing interrupted by user")
except Exception as e:
logging.error(f"💥 Critical error: {e}")
logging.error(traceback.format_exc())
finally:
tester.cleanup()
if __name__ == "__main__":
main()