#!/usr/bin/env python3 """ Automated Adventure Simulation - Random Travel & Combat Until Death This test simulates a full adventure: 1. Load character (Thorin) 2. Explore and travel randomly 3. Fight any creatures encountered 4. Continue until character dies (game over) 5. Show everything in Selenium browser """ import time import random import re 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.options import Options def setup_browser(): """Setup Chrome with visible window.""" options = Options() options.add_argument('--start-maximized') # Don't run headless - we want to SEE it! driver = webdriver.Chrome(options=options) return driver def wait_for_response(driver, previous_count, timeout=30): """Wait for new GM response.""" start = time.time() while time.time() - start < timeout: messages = driver.find_elements(By.CSS_SELECTOR, '.message') current_count = len(messages) if current_count > previous_count: # Skip "Loading..." messages last_msg = messages[-1].text if messages else "" if "Loading" not in last_msg and len(last_msg) > 10: return current_count time.sleep(0.5) return previous_count def get_chat_messages(driver): """Get all chat messages from Gradio chatbot.""" try: # Try multiple selectors for Gradio chat messages bot_messages = driver.find_elements(By.CSS_SELECTOR, '.message') if not bot_messages: bot_messages = driver.find_elements(By.CSS_SELECTOR, '[data-testid="bot"]') if not bot_messages: # Fallback: try finding any div with bot-related class bot_messages = driver.find_elements(By.CSS_SELECTOR, '.bot, [class*="bot"]') # Get text from bot messages messages = [] for msg in bot_messages: text = msg.text.strip() if text and "Loading" not in text and text not in messages: # Avoid duplicates messages.append(text) return messages except Exception as e: print(f"Warning: Could not get messages: {e}") return [] def send_message(driver, text): """Send a message and wait for response.""" print(f" šŸ’¬ Sending: {text[:80]}") # Find and fill input textarea = driver.find_element(By.CSS_SELECTOR, 'textarea[placeholder*="Type"]') textarea.clear() textarea.send_keys(text) # Click submit submit_btn = driver.find_element(By.CSS_SELECTOR, 'button.primary') submit_btn.click() # Fixed wait for LLM response (more reliable than trying to detect new messages) time.sleep(10) # Get the latest chat messages to return messages = get_chat_messages(driver) if messages: return messages[-1] return "[No messages found]" def get_hp_from_sheet(driver): """Extract HP from character sheet (not chat history).""" try: # HP is in the character sheet under "Combat Stats" heading # Structure:

Combat Stats