Branchwell

AI Resource Pro / website / tests / selenium / README.md

Show the source

Selenium Testing for AI Resource Pro

This directory contains Selenium-based tests for the AI Resource Pro website. These tests automate browser interactions to verify application functionality.

Automated Test Suite

This directory contains automated Selenium tests for the AI Resource Pro website.

Setup

  1. Create a Python virtual environment:
    ```bash
    python -m venv venv
    ```
  2. Activate the virtual environment:
    ```bash
    source venv/bin/activate # On Unix/macOS
    venv\Scripts\activate # On Windows
    ```
  3. Install the required packages:
    ```bash
    pip install -r requirements.txt
    ```

Running Tests

Ensure that the Next.js application is running on http://localhost:3001 before executing tests.

Run a specific test:

python -m pytest -v test_ai_coach.py::TestAICoach::test_page_loads

Run all tests in a file:

python -m pytest -v test_ai_coach.py

Run all tests:

python -m pytest -v

Automated Test Script

For convenience, an automated shell script is provided to help set up and run the tests:

./run_tests.sh

This script will:

  1. Check if the Next.js application is running (on port 3001) and start it if not
  2. Navigate to the Selenium tests directory
  3. Activate the Python virtual environment (or create one if it doesn't exist)
  4. Provide options to run specific tests or all tests
  5. Show test results with colored output

The script is particularly useful for users who want a simple way to run the tests without remembering all the commands.

Full Script Content

Here's the complete content of the run_tests.sh script:

#!/bin/bash

# Define paths
SITE_DIR="/path/to/file"
SELENIUM_DIR="$SITE_DIR/tests/selenium"

# Function to check if a process is running on port 3001
check_port() {
    lsof -i:3001 > /dev/null
    return $?
}

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}====================================${NC}"
echo -e "${GREEN}AI Resource Pro Testing Script${NC}"
echo -e "${BLUE}====================================${NC}"

# Navigate to the project root
echo -e "${YELLOW}Navigating to project root at $SITE_DIR${NC}"
cd "$SITE_DIR" || { echo "Failed to navigate to $SITE_DIR"; exit 1; }

# Check if Next.js app is already running
if check_port; then
    echo -e "${GREEN}Next.js app is already running on port 3001${NC}"
else
    echo -e "${YELLOW}Starting Next.js app in a new terminal window...${NC}"
    # For macOS, use open command to start a new terminal window
    # This assumes zsh shell, adjust if using a different shell
    osascript -e 'tell application "Terminal" to do script "cd '"$SITE_DIR"' && npm run dev"'
    
    # Wait for Next.js to start (adjust sleep time as needed)
    echo -e "${YELLOW}Waiting for Next.js to start...${NC}"
    sleep 10
    
    # Check if Next.js started successfully
    if check_port; then
        echo -e "${GREEN}Next.js app started successfully on port 3001${NC}"
    else
        echo "Failed to start Next.js app on port 3001"
        exit 1
    fi
fi

# Navigate to the Selenium tests directory
echo -e "${YELLOW}Navigating to Selenium tests directory at $SELENIUM_DIR${NC}"
cd "$SELENIUM_DIR" || { echo "Failed to navigate to $SELENIUM_DIR"; exit 1; }

# Activate Python virtual environment
echo -e "${YELLOW}Activating Python virtual environment...${NC}"
if [ -d "venv" ]; then
    source venv/bin/activate || { echo "Failed to activate virtual environment"; exit 1; }
    echo -e "${GREEN}Virtual environment activated${NC}"
else
    echo "Virtual environment not found at $SELENIUM_DIR/venv"
    echo "Would you like to create a new virtual environment? (y/n)"
    read -r create_venv
    if [ "$create_venv" = "y" ]; then
        python -m venv venv
        source venv/bin/activate
        echo "Installing requirements..."
        pip install -r requirements.txt
    else
        exit 1
    fi
fi

# Run the tests
echo -e "${BLUE}====================================${NC}"
echo -e "${GREEN}Running Selenium tests...${NC}"
echo -e "${BLUE}====================================${NC}"
echo "Available test options:"
echo "1) Run single test (test_page_loads)"
echo "2) Run all AI Coach tests"
echo "3) Run all tests"
echo "4) Run test_generate_roadmap test"
echo "Enter your choice (1-4):"
read -r test_choice

case $test_choice in
    1)
        echo -e "${YELLOW}Running test_page_loads test...${NC}"
        python -m pytest -v test_ai_coach.py::TestAICoach::test_page_loads
        ;;
    2)
        echo -e "${YELLOW}Running all AI Coach tests...${NC}"
        python -m pytest -v test_ai_coach.py
        ;;
    3)
        echo -e "${YELLOW}Running all tests...${NC}"
        python -m pytest -v
        ;;
    4)
        echo -e "${YELLOW}Running test_generate_roadmap test...${NC}"
        python -m pytest -v test_ai_coach.py::TestAICoach::test_generate_roadmap
        ;;
    *)
        echo "Invalid choice"
        exit 1
        ;;
esac

# Check test results
test_result=$?
if [ $test_result -eq 0 ]; then
    echo -e "${GREEN}All tests passed successfully!${NC}"
else
    echo -e "${YELLOW}Some tests failed. Check the output above for more details.${NC}"
fi

echo -e "${BLUE}====================================${NC}"
echo -e "${GREEN}Test run complete${NC}"
echo -e "${BLUE}====================================${NC}"

# Deactivate Python virtual environment
deactivate
echo "Python virtual environment deactivated"

echo "Press Enter to exit..."
read -r

You may need to make the script executable before using it:

chmod +x run_tests.sh

Test Structure

Page Objects

Tests use the Page Object Model (POM) pattern:

  • page_objects/ai_coach_page.py: Contains methods for interacting with the AI Coach page

Test Files

  • test_ai_coach.py: Tests for the AI Coach page functionality

Screenshots

Tests automatically capture screenshots at important points during test execution:

  • Screenshots are organized in datetime-named folders (e.g., 20240528_153045) within the screenshots directory
  • Each test run creates a new folder with the current timestamp
  • Screenshots include:
    • Successful roadmap generation (successful_roadmap.png)
    • Error states with descriptive filenames
    • Debugging screenshots for problematic steps
    • Auto-captured screenshots on test failures

Screenshots are excluded from version control via .gitignore.

Configuration

  • The default base URL is http://localhost:3001
  • To use a different URL, set the TEST_BASE_URL environment variable:
# On Windows (PowerShell)
$env:TEST_BASE_URL="http://example.com"
pytest

# On macOS/Linux
TEST_BASE_URL="http://example.com" pytest

Generating Reports

You can generate HTML reports of test results:

pytest --html=report.html

Troubleshooting

  • WebDriver Issues: If you encounter driver issues, try updating the webdriver-manager package:
    ```bash
    pip install --upgrade webdriver-manager
    ```
  • Mac ARM64 (Apple Silicon) Issues: If you encounter issues on macOS with Apple Silicon:
    ```bash
    # Clear the webdriver cache
    rm -rf ~/.wdm/drivers/chromedriver
    # Install the latest chromedriver manually
    brew install --cask chromedriver
    # If chromedriver crashes with Status code -9, try:
    brew uninstall chromedriver && brew install --cask chromedriver
    # Make sure your Chrome browser version matches the chromedriver version
    # Check Chrome version:
    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version
    # Check chromedriver version:
    chromedriver --version
    ```
  • Using Alternate Browsers: If Chrome continues to have issues:
    ```bash
    # Install Firefox and use it for testing
    brew install --cask firefox
    # Run tests with Firefox
    pytest --browser=firefox
    ```
  • Browser Compatibility: Make sure you have the latest browser version installed.
  • Element Not Found: If tests fail with element not found errors, the UI structure might have changed. Update the locators in the page objects.