#!/usr/bin/env python3 """ Test script for the browser-use server Run this to test the server locally before deployment """ import requests import json import time # Server URL (change this to your deployed URL when testing on Hugging Face Spaces) BASE_URL = "http://localhost:7860" def test_health(): """Test the health endpoint""" print("Testing /health endpoint...") try: response = requests.get(f"{BASE_URL}/health") print(f"Status: {response.status_code}") print(f"Response: {response.json()}") return response.status_code == 200 except Exception as e: print(f"Error: {e}") return False def test_status(): """Test the status endpoint""" print("\nTesting /status endpoint...") try: response = requests.get(f"{BASE_URL}/status") print(f"Status: {response.status_code}") print(f"Response: {json.dumps(response.json(), indent=2)}") return response.status_code == 200 except Exception as e: print(f"Error: {e}") return False def test_run_task(): """Test the run-task endpoint""" print("\nTesting /run-task endpoint...") try: task_data = { "task": "Go to example.com and get the page title", "model": "gpt-4o-mini" } print(f"Sending task: {task_data}") response = requests.post( f"{BASE_URL}/run-task", json=task_data, timeout=60 # 1 minute timeout for testing ) print(f"Status: {response.status_code}") print(f"Response: {json.dumps(response.json(), indent=2)}") return response.status_code == 200 except Exception as e: print(f"Error: {e}") return False def main(): """Run all tests""" print("Browser-Use Server Test Suite") print("=" * 40) # Test basic endpoints health_ok = test_health() status_ok = test_status() if not (health_ok and status_ok): print("\n❌ Basic endpoints failed. Check if server is running and configured properly.") return # Test the main functionality print("\n" + "=" * 40) print("Testing browser automation (this may take a while)...") task_ok = test_run_task() print("\n" + "=" * 40) print("Test Results:") print(f"Health endpoint: {'✅' if health_ok else '❌'}") print(f"Status endpoint: {'✅' if status_ok else '❌'}") print(f"Task execution: {'✅' if task_ok else '❌'}") if health_ok and status_ok and task_ok: print("\n🎉 All tests passed! Your browser-use server is working correctly.") else: print("\n⚠️ Some tests failed. Check the error messages above.") if __name__ == "__main__": main()