|
| 1 | +import shutil |
| 2 | +import subprocess |
| 3 | + |
| 4 | +import pytest |
| 5 | +from django.contrib.staticfiles.testing import StaticLiveServerTestCase |
| 6 | +from selenium import webdriver |
| 7 | +from selenium.webdriver.chrome.options import Options |
| 8 | +from selenium.webdriver.chrome.service import Service |
| 9 | +from selenium.webdriver.support.ui import WebDriverWait |
| 10 | + |
| 11 | + |
| 12 | +class BaseTestCase(StaticLiveServerTestCase): |
| 13 | + """Base class for all frontend tests using Selenium.""" |
| 14 | + |
| 15 | + @classmethod |
| 16 | + def setUpClass(cls): |
| 17 | + super().setUpClass() |
| 18 | + |
| 19 | + # Verify ChromeDriver is available |
| 20 | + chromedriver_path = shutil.which("chromedriver") |
| 21 | + if not chromedriver_path: |
| 22 | + pytest.fail("ChromeDriver not found. Please ensure chromium-driver is installed.") |
| 23 | + |
| 24 | + # # Set up Chrome options |
| 25 | + chrome_options = Options() |
| 26 | + chrome_options.add_argument("--headless") |
| 27 | + chrome_options.add_argument("--no-sandbox") |
| 28 | + chrome_options.add_argument("--disable-dev-shm-usage") |
| 29 | + chrome_options.binary_location = "/usr/bin/chromium" |
| 30 | + |
| 31 | + try: |
| 32 | + # Create service with explicit path |
| 33 | + service = Service(executable_path=chromedriver_path, log_path="/tmp/chromedriver.log") |
| 34 | + |
| 35 | + # Initialize WebDriver with service and options |
| 36 | + cls.driver = webdriver.Chrome(service=service, options=chrome_options) |
| 37 | + |
| 38 | + cls.driver.set_window_size(1920, 1080) |
| 39 | + cls.driver.implicitly_wait(10) |
| 40 | + cls.wait = WebDriverWait(cls.driver, 10) |
| 41 | + |
| 42 | + except Exception as e: |
| 43 | + # Print debugging information |
| 44 | + subprocess.run(["which", "chromium"]) |
| 45 | + subprocess.run(["which", "chromedriver"]) |
| 46 | + subprocess.run(["chromium", "--version"]) |
| 47 | + subprocess.run(["chromedriver", "--version"]) |
| 48 | + pytest.fail(f"Failed to initialize ChromeDriver: {str(e)}") |
| 49 | + |
| 50 | + @classmethod |
| 51 | + def tearDownClass(cls): |
| 52 | + if hasattr(cls, "driver"): |
| 53 | + cls.driver.quit() |
| 54 | + super().tearDownClass() |
| 55 | + |
| 56 | + def setUp(self): |
| 57 | + """Set up test case.""" |
| 58 | + super().setUp() |
| 59 | + # Add any additional setup here |
0 commit comments