|
1 | 1 | import shutil
|
2 |
| -import subprocess |
3 | 2 |
|
4 | 3 | import pytest
|
5 |
| -from django.contrib.auth import get_user_model |
6 | 4 | from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
7 | 5 | from selenium import webdriver
|
8 | 6 | from selenium.webdriver.chrome.options import Options
|
9 | 7 | from selenium.webdriver.chrome.service import Service
|
10 |
| -from selenium.webdriver.common.by import By |
11 |
| -from selenium.webdriver.support import expected_conditions as EC |
12 | 8 | from selenium.webdriver.support.ui import WebDriverWait
|
13 | 9 |
|
| 10 | +from .mixins import AuthenticationMixin |
14 | 11 |
|
15 |
| -class BaseTestCase(StaticLiveServerTestCase): |
| 12 | + |
| 13 | +class BaseTestCase(StaticLiveServerTestCase, AuthenticationMixin): |
16 | 14 | """Base class for all frontend tests using Selenium."""
|
17 | 15 |
|
18 | 16 | @classmethod
|
19 | 17 | def setUpClass(cls):
|
20 | 18 | super().setUpClass()
|
21 | 19 |
|
22 |
| - # Verify ChromeDriver is available |
| 20 | + # Verify ChromeDriver and Chromium are available |
23 | 21 | chromedriver_path = shutil.which("chromedriver")
|
| 22 | + chromium_path = shutil.which("chromium") |
| 23 | + |
24 | 24 | if not chromedriver_path:
|
25 | 25 | pytest.fail("ChromeDriver not found. Please ensure chromium-driver is installed.")
|
| 26 | + if not chromium_path: |
| 27 | + pytest.fail("Chromium not found. Please ensure chromium is installed.") |
26 | 28 |
|
27 |
| - # # Set up Chrome options |
| 29 | + # Set up Chrome options |
28 | 30 | chrome_options = Options()
|
29 | 31 | chrome_options.add_argument("--headless")
|
30 | 32 | chrome_options.add_argument("--no-sandbox")
|
31 | 33 | chrome_options.add_argument("--disable-dev-shm-usage")
|
32 |
| - chrome_options.binary_location = "/usr/bin/chromium" |
| 34 | + chrome_options.binary_location = chromium_path |
33 | 35 |
|
34 | 36 | try:
|
35 |
| - # Create service with explicit path |
36 |
| - service = Service(executable_path=chromedriver_path, log_path="/tmp/chromedriver.log") |
37 |
| - |
38 |
| - # Initialize WebDriver with service and options |
| 37 | + service = Service(executable_path=chromedriver_path) |
39 | 38 | cls.driver = webdriver.Chrome(service=service, options=chrome_options)
|
40 |
| - |
41 | 39 | cls.driver.set_window_size(1920, 1080)
|
42 | 40 | cls.driver.implicitly_wait(10)
|
43 | 41 | cls.wait = WebDriverWait(cls.driver, 10)
|
44 | 42 |
|
45 | 43 | except Exception as e:
|
46 |
| - # Print debugging information |
47 |
| - subprocess.run(["which", "chromium"]) |
48 |
| - subprocess.run(["which", "chromedriver"]) |
49 |
| - subprocess.run(["chromium", "--version"]) |
50 |
| - subprocess.run(["chromedriver", "--version"]) |
51 | 44 | pytest.fail(f"Failed to initialize ChromeDriver: {str(e)}")
|
52 | 45 |
|
53 | 46 | @classmethod
|
54 | 47 | def tearDownClass(cls):
|
55 | 48 | if hasattr(cls, "driver"):
|
56 | 49 | cls.driver.quit()
|
57 | 50 | super().tearDownClass()
|
58 |
| - |
59 |
| - def setUp(self): |
60 |
| - """Set up test case.""" |
61 |
| - super().setUp() |
62 |
| - # Add any additional setup here |
63 |
| - |
64 |
| - def create_test_user(self, username="test_user", password="test_password123", **kwargs): |
65 |
| - """Create a test user for login testing.""" |
66 |
| - User = get_user_model() |
67 |
| - |
68 |
| - # Delete user if it already exists |
69 |
| - User.objects.filter(username=username).delete() |
70 |
| - |
71 |
| - user_data = { |
72 |
| - "username": username, |
73 |
| - "is_active": True, |
74 |
| - "is_staff": True, # Ensure user is staff |
75 |
| - "is_superuser": False, # Ensure user is superuser |
76 |
| - } |
77 |
| - user_data.update(kwargs) |
78 |
| - |
79 |
| - user = User.objects.create_user(**user_data) |
80 |
| - user.set_password(password) |
81 |
| - user.save() |
82 |
| - |
83 |
| - # Verify user was created correctly |
84 |
| - print(f"\nCreated user: {username}") |
85 |
| - print(f"Is active: {user.is_active}") |
86 |
| - print(f"Is staff: {user.is_staff}") |
87 |
| - print(f"Is superuser: {user.is_superuser}") |
88 |
| - |
89 |
| - return user, password |
90 |
| - |
91 |
| - def login(self, username="test_user", password="test_password123"): |
92 |
| - """ |
93 |
| - Login helper method. |
94 |
| - Returns True if login successful, False otherwise. |
95 |
| - """ |
96 |
| - # Navigate to login page |
97 |
| - self.driver.get(f"{self.live_server_url}/accounts/login/") |
98 |
| - |
99 |
| - try: |
100 |
| - # Wait for and fill username |
101 |
| - username_input = self.wait.until(EC.presence_of_element_located((By.NAME, "login"))) |
102 |
| - username_input.send_keys(username) |
103 |
| - |
104 |
| - # Fill password |
105 |
| - password_input = self.driver.find_element(By.NAME, "password") |
106 |
| - password_input.send_keys(password) |
107 |
| - |
108 |
| - # Find and click the login button |
109 |
| - login_button = self.driver.find_element(By.CSS_SELECTOR, "button[type='submit']") |
110 |
| - login_button.click() |
111 |
| - |
112 |
| - # Wait for successful login by checking for redirect |
113 |
| - self.wait.until(EC.url_changes("/accounts/login/")) |
114 |
| - |
115 |
| - # Print debug information |
116 |
| - print(f"Current URL after login: {self.driver.current_url}") |
117 |
| - return True |
118 |
| - |
119 |
| - except Exception as e: |
120 |
| - print(f"Login failed: {str(e)}") |
121 |
| - return False |
122 |
| - |
123 |
| - def logout(self): |
124 |
| - """Logout helper method.""" |
125 |
| - try: |
126 |
| - # Click logout link/button (adjust selector based on your UI) |
127 |
| - logout_link = self.driver.find_element(By.CSS_SELECTOR, "a[href*='logout']") |
128 |
| - logout_link.click() |
129 |
| - |
130 |
| - # Wait for redirect to login page |
131 |
| - self.wait.until(EC.presence_of_element_located((By.NAME, "username"))) |
132 |
| - return True |
133 |
| - except Exception as e: |
134 |
| - print(f"Logout failed: {str(e)}") |
135 |
| - return False |
0 commit comments