Files
broswer-automation/agent-livekit/test_dynamic_form_filling.py
nasir@endelospay.com d97cad1736 first commit
2025-08-12 02:54:17 +05:00

171 lines
6.0 KiB
Python

#!/usr/bin/env python3
"""
Test script for the new dynamic form filling capabilities.
This script tests the enhanced form filling system that:
1. Uses MCP tools to dynamically discover form elements
2. Retries when selectors are not found
3. Maps natural language to form fields intelligently
4. Never uses hardcoded selectors
"""
import asyncio
import logging
import sys
import os
# Add the current directory to the path so we can import our modules
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from mcp_chrome_client import MCPChromeClient
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
async def test_dynamic_form_filling():
"""Test the dynamic form filling capabilities"""
# Initialize MCP Chrome client
client = MCPChromeClient(
server_type="http",
server_url="http://127.0.0.1:12306/mcp"
)
try:
# Connect to MCP server
logger.info("Connecting to MCP server...")
await client.connect()
logger.info("Connected successfully!")
# Test 1: Navigate to a test page with forms
logger.info("=== Test 1: Navigate to Google ===")
result = await client._navigate_mcp("https://www.google.com")
logger.info(f"Navigation result: {result}")
await asyncio.sleep(3) # Wait for page to load
# Test 2: Test dynamic discovery for search field
logger.info("=== Test 2: Dynamic discovery for search field ===")
discovery_result = await client._discover_form_fields_dynamically("search", "python programming")
logger.info(f"Discovery result: {discovery_result}")
# Test 3: Test enhanced field detection with retry
logger.info("=== Test 3: Enhanced field detection with retry ===")
enhanced_result = await client._enhanced_field_detection_with_retry("search", "machine learning", max_retries=2)
logger.info(f"Enhanced result: {enhanced_result}")
# Test 4: Test the main fill_field_by_name method with dynamic discovery
logger.info("=== Test 4: Main fill_field_by_name method ===")
fill_result = await client.fill_field_by_name("search", "artificial intelligence")
logger.info(f"Fill result: {fill_result}")
# Test 5: Test voice command processing
logger.info("=== Test 5: Voice command processing ===")
voice_commands = [
"fill search with deep learning",
"enter neural networks in search box",
"type computer vision in search field"
]
for command in voice_commands:
logger.info(f"Testing voice command: '{command}'")
voice_result = await client.execute_voice_command(command)
logger.info(f"Voice command result: {voice_result}")
await asyncio.sleep(2)
# Test 6: Navigate to a different site and test form discovery
logger.info("=== Test 6: Test on different website ===")
result = await client._navigate_mcp("https://www.github.com")
logger.info(f"GitHub navigation result: {result}")
await asyncio.sleep(3)
# Try to find search field on GitHub
github_discovery = await client._discover_form_fields_dynamically("search", "python")
logger.info(f"GitHub search discovery: {github_discovery}")
logger.info("=== All tests completed! ===")
except Exception as e:
logger.error(f"Test failed with error: {e}")
import traceback
traceback.print_exc()
finally:
# Disconnect from MCP server
try:
await client.disconnect()
logger.info("Disconnected from MCP server")
except Exception as e:
logger.error(f"Error disconnecting: {e}")
async def test_field_matching():
"""Test the field matching logic"""
logger.info("=== Testing field matching logic ===")
client = MCPChromeClient(server_type="http", server_url="http://127.0.0.1:12306/mcp")
# Test element matching
test_elements = [
{
"tagName": "input",
"attributes": {
"name": "email",
"type": "email",
"placeholder": "Enter your email"
}
},
{
"tagName": "input",
"attributes": {
"name": "search_query",
"type": "search",
"placeholder": "Search..."
}
},
{
"tagName": "textarea",
"attributes": {
"name": "message",
"placeholder": "Type your message here"
}
}
]
test_field_names = ["email", "search", "message", "query"]
for field_name in test_field_names:
logger.info(f"Testing field name: '{field_name}'")
for i, element in enumerate(test_elements):
is_match = client._is_field_match(element, field_name.lower())
selector = client._extract_best_selector(element)
logger.info(f" Element {i+1}: Match={is_match}, Selector={selector}")
logger.info("")
def main():
"""Main function to run the tests"""
logger.info("Starting dynamic form filling tests...")
# Check if MCP server is likely running
import socket
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex(('127.0.0.1', 12306))
sock.close()
if result != 0:
logger.warning("MCP server doesn't appear to be running on port 12306")
logger.warning("Please start the MCP server before running this test")
return
except Exception as e:
logger.warning(f"Could not check MCP server status: {e}")
# Run the tests
asyncio.run(test_field_matching())
asyncio.run(test_dynamic_form_filling())
if __name__ == "__main__":
main()