#!/usr/bin/env python3 """ Real Functionality Test Utilities Utilities for testing real functionality without mocks or simulations. Provides helpers for real service interaction, real data validation, and real UI testing. """ import time import json import requests from typing import Dict, List, Optional, Any 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.common.exceptions import TimeoutException, NoSuchElementException from test_config_real import get_real_test_config, RealTestConfig class RealServiceValidator: """Validates real services without mocks.""" def __init__(self, config: RealTestConfig): self.config = config def validate_all_services(self) -> bool: """Validate all required real services are healthy.""" for service in self.config.services: if service.required_for_test and not self.validate_service(service): return False return True def validate_service(self, service) -> bool: """Validate a single real service.""" try: response = requests.get(service.health_endpoint, timeout=service.timeout_seconds) return response.status_code == 200 except: return False def get_real_api_data(self, endpoint_name: str) -> Optional[Dict[str, Any]]: """Get real data from API endpoint (no mocks).""" if endpoint_name not in self.config.api_endpoints: return None try: url = self.config.api_endpoints[endpoint_name] response = requests.get(url, timeout=self.config.browser_timeout_seconds) if response.status_code == 200: return response.json() except Exception as e: print(f"โŒ Real API call failed for {endpoint_name}: {e}") return None class RealUITester: """Tests real UI functionality without simulations.""" def __init__(self, driver: webdriver.Chrome, config: RealTestConfig): self.driver = driver self.config = config self.wait = WebDriverWait(driver, config.element_wait_timeout_seconds) def find_real_element(self, selector_group: str) -> Optional[Any]: """Find real UI element using multiple selectors.""" selectors = self.config.ui_selectors.get(selector_group, []) for selector in selectors: try: elements = self.driver.find_elements(By.CSS_SELECTOR, selector) for element in elements: if element.is_displayed() and element.is_enabled(): return element except: continue return None def find_all_real_elements(self, selector_group: str) -> List[Any]: """Find all real UI elements using multiple selectors.""" selectors = self.config.ui_selectors.get(selector_group, []) found_elements = [] for selector in selectors: try: elements = self.driver.find_elements(By.CSS_SELECTOR, selector) for element in elements: if element.is_displayed(): found_elements.append(element) except: continue return found_elements def click_real_element(self, element) -> bool: """Perform real click on element.""" try: # Use JavaScript click for reliability in real browser self.driver.execute_script("arguments[0].click();", element) return True except Exception as e: print(f"โŒ Real click failed: {e}") return False def get_real_element_text(self, element) -> str: """Get real text from element.""" try: return (element.text or element.get_attribute('aria-label') or element.get_attribute('title') or element.get_attribute('alt') or '').strip() except: return '' def validate_real_content(self, element, min_length: int = 20) -> bool: """Validate that element contains real content.""" try: content = self.get_real_element_text(element) return len(content) >= min_length except: return False def wait_for_real_element(self, selector_group: str, timeout: Optional[int] = None) -> Optional[Any]: """Wait for real element to appear.""" timeout = timeout or self.config.element_wait_timeout_seconds selectors = self.config.ui_selectors.get(selector_group, []) for selector in selectors: try: element = WebDriverWait(self.driver, timeout).until( EC.presence_of_element_located((By.CSS_SELECTOR, selector)) ) if element.is_displayed(): return element except TimeoutException: continue return None class RealDataValidator: """Validates real data without mocks.""" def __init__(self, config: RealTestConfig): self.config = config def validate_api_response_structure(self, data: Dict[str, Any], endpoint_name: str) -> bool: """Validate real API response structure.""" if endpoint_name == "documentation": return self.validate_documentation_data(data) elif endpoint_name == "tools": return self.validate_tools_data(data) return True def validate_documentation_data(self, data: Dict[str, Any]) -> bool: """Validate real documentation data structure.""" if not isinstance(data, dict): return False if 'tools' not in data: return False tools = data['tools'] if not isinstance(tools, dict): return False if len(tools) < self.config.min_documentation_tools: return False # Validate at least one tool has required fields for tool_name, tool_data in tools.items(): if isinstance(tool_data, dict): has_required_fields = all( field in tool_data for field in self.config.required_api_fields ) if has_required_fields: return True return False def validate_tools_data(self, data: Dict[str, Any]) -> bool: """Validate real tools data structure.""" return isinstance(data, (dict, list)) def validate_ui_api_consistency(self, ui_content: str, api_data: Dict[str, Any]) -> bool: """Validate that UI content matches real API data.""" if not api_data or not ui_content: return False ui_lower = ui_content.lower() # Check if API data appears in UI if 'tools' in api_data: tools_found = 0 for tool_name, tool_data in api_data['tools'].items(): if tool_name.lower() in ui_lower: tools_found += 1 # Check description description = tool_data.get('description', '') if description and description.lower()[:50] in ui_lower: tools_found += 1 return tools_found > 0 return False def create_real_browser_driver(headless: bool = True) -> webdriver.Chrome: """Create real browser driver for testing.""" from selenium.webdriver.chrome.options import Options 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("--window-size=1920,1080") chrome_options.add_argument("--enable-logging") chrome_options.add_argument("--log-level=0") driver = webdriver.Chrome(options=chrome_options) driver.implicitly_wait(10) return driver def capture_real_browser_state(driver: webdriver.Chrome) -> Dict[str, Any]: """Capture real browser state for debugging.""" try: return { "url": driver.current_url, "title": driver.title, "page_source_length": len(driver.page_source), "console_errors": len([log for log in driver.get_log('browser') if log['level'] == 'SEVERE']), "console_warnings": len([log for log in driver.get_log('browser') if log['level'] == 'WARNING']), "window_size": driver.get_window_size(), "cookies": len(driver.get_cookies()) } except: return {"error": "Could not capture browser state"} if __name__ == "__main__": # Test the utilities config = get_real_test_config() validator = RealServiceValidator(config) print("๐Ÿงช Testing real functionality utilities...") if validator.validate_all_services(): print("โœ… Real services validation working") else: print("โŒ Real services validation failed")