AI Resource Pro / website / tests / selenium / conftest.py
| 1 | import pytest |
| 2 | import os |
| 3 | import platform |
| 4 | import subprocess |
| 5 | import sys |
| 6 | from selenium import webdriver |
| 7 | from selenium.webdriver.chrome.service import Service as ChromeService |
| 8 | from selenium.webdriver.firefox.service import Service as FirefoxService |
| 9 | from webdriver_manager.chrome import ChromeDriverManager |
| 10 | from webdriver_manager.firefox import GeckoDriverManager |
| 11 | from webdriver_manager.core.os_manager import ChromeType |
| 12 | from dotenv import load_dotenv |
| 13 | |
| 14 | # Import from utils module instead of test_ai_coach |
| 15 | from utils import save_screenshot, SCREENSHOT_FOLDER |
| 16 | |
| 17 | # Load environment variables from .env file |
| 18 | load_dotenv() |
| 19 | |
| 20 | # Default test URL - can be overridden by environment variable |
| 21 | BASE_URL = os.getenv("TEST_BASE_URL", "http://localhost:3001") |
| 22 | |
| 23 | def pytest_addoption(parser): |
| 24 | """Add command-line options for test configuration.""" |
| 25 | parser.addoption( |
| 26 | "--browser", |
| 27 | action="store", |
| 28 | default="chrome", |
| 29 | help="Browser to run tests on: chrome or firefox" |
| 30 | ) |
| 31 | parser.addoption( |
| 32 | "--headless", |
| 33 | action="store_true", |
| 34 | default=False, |
| 35 | help="Run browser in headless mode" |
| 36 | ) |
| 37 | |
| 38 | # Reset screenshot folder at the start of each test session |
| 39 | def pytest_configure(config): |
| 40 | """Reset screenshot folder at the start of each test session.""" |
| 41 | import utils |
| 42 | utils.SCREENSHOT_FOLDER = None |
| 43 | print("Screenshot folder reset for new test session") |
| 44 | |
| 45 | @pytest.fixture(scope="session") |
| 46 | def browser(request): |
| 47 | """ |
| 48 | Setup WebDriver for Selenium tests. |
| 49 | |
| 50 | Returns: |
| 51 | WebDriver: configured browser instance |
| 52 | """ |
| 53 | browser_name = request.config.getoption("--browser").lower() |
| 54 | headless = request.config.getoption("--headless") |
| 55 | |
| 56 | if browser_name == "chrome": |
| 57 | options = webdriver.ChromeOptions() |
| 58 | if headless: |
| 59 | options.add_argument("--headless=new") # Updated headless flag |
| 60 | options.add_argument("--window-size=1920,1080") |
| 61 | options.add_argument("--no-sandbox") |
| 62 | options.add_argument("--disable-dev-shm-usage") |
| 63 | |
| 64 | # Enable verbose logging |
| 65 | options.add_argument("--verbose") |
| 66 | options.add_argument("--log-level=0") |
| 67 | |
| 68 | # Special handling for macOS ARM64 |
| 69 | if platform.system() == "Darwin" and platform.machine() == "arm64": |
| 70 | print("Detected macOS ARM64 system") |
| 71 | try: |
| 72 | # Check if chromedriver is installed via Homebrew |
| 73 | chromedriver_path = subprocess.check_output(["which", "chromedriver"]).decode().strip() |
| 74 | print(f"Using chromedriver from: {chromedriver_path}") |
| 75 | |
| 76 | # Check chromedriver version |
| 77 | try: |
| 78 | version_output = subprocess.check_output([chromedriver_path, "--version"]).decode().strip() |
| 79 | print(f"ChromeDriver version: {version_output}") |
| 80 | except Exception as e: |
| 81 | print(f"Failed to get chromedriver version: {e}") |
| 82 | |
| 83 | # Set up service with increased start timeout |
| 84 | service = ChromeService( |
| 85 | executable_path=chromedriver_path, |
| 86 | log_path="chromedriver.log", |
| 87 | service_args=["--verbose"] |
| 88 | ) |
| 89 | driver = webdriver.Chrome(service=service, options=options) |
| 90 | except Exception as e: |
| 91 | print(f"Error using local chromedriver: {e}") |
| 92 | print("Falling back to webdriver-manager...") |
| 93 | driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options) |
| 94 | else: |
| 95 | driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options) |
| 96 | elif browser_name == "firefox": |
| 97 | options = webdriver.FirefoxOptions() |
| 98 | if headless: |
| 99 | options.add_argument("--headless") |
| 100 | driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()), options=options) |
| 101 | else: |
| 102 | raise ValueError(f"Unsupported browser: {browser_name}") |
| 103 | |
| 104 | driver.maximize_window() |
| 105 | driver.implicitly_wait(10) # seconds |
| 106 | |
| 107 | yield driver |
| 108 | driver.quit() |
| 109 | |
| 110 | @pytest.fixture |
| 111 | def base_url(): |
| 112 | """Return the base URL for the application.""" |
| 113 | return BASE_URL |
| 114 | |
| 115 | @pytest.hookimpl(tryfirst=True, hookwrapper=True) |
| 116 | def pytest_runtest_makereport(item, call): |
| 117 | """ |
| 118 | Take screenshot when test fails. |
| 119 | This runs after each test phase (setup, call, teardown). |
| 120 | """ |
| 121 | # Execute all other hooks to obtain the report object |
| 122 | outcome = yield |
| 123 | report = outcome.get_result() |
| 124 | |
| 125 | # Check if test failed during call phase (actual test execution) |
| 126 | if report.when == "call" and report.failed: |
| 127 | # Try to get browser fixture |
| 128 | try: |
| 129 | browser = item.funcargs.get("browser") |
| 130 | if browser: |
| 131 | test_name = item.nodeid.replace("::", "_").replace("/", "_").replace(".", "_") |
| 132 | save_screenshot(browser, f"fail_{test_name}.png") |
| 133 | print(f"Screenshot taken for failed test: {test_name}") |
| 134 | except Exception as e: |
| 135 | print(f"Failed to capture screenshot on test failure: {e}") |