first commit
This commit is contained in:
173
agent-livekit/test_field_workflow.py
Normal file
173
agent-livekit/test_field_workflow.py
Normal file
@@ -0,0 +1,173 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for the enhanced field workflow functionality.
|
||||
|
||||
This script demonstrates how to use the new execute_field_workflow method
|
||||
to handle missing webpage fields with automatic MCP-based detection.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import json
|
||||
from mcp_chrome_client import MCPChromeClient
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def test_field_workflow():
|
||||
"""Test the enhanced field workflow with various scenarios."""
|
||||
|
||||
# Initialize MCP Chrome client
|
||||
chrome_config = {
|
||||
'mcp_server_type': 'chrome_extension',
|
||||
'mcp_server_url': 'http://localhost:3000',
|
||||
'mcp_server_command': '',
|
||||
'mcp_server_args': []
|
||||
}
|
||||
|
||||
client = MCPChromeClient(chrome_config)
|
||||
|
||||
try:
|
||||
# Test scenarios
|
||||
test_scenarios = [
|
||||
{
|
||||
"name": "Google Search Workflow",
|
||||
"url": "https://www.google.com",
|
||||
"field_name": "search",
|
||||
"field_value": "LiveKit agent automation",
|
||||
"actions": [
|
||||
{"type": "keyboard", "target": "Enter"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Login Form Workflow",
|
||||
"url": "https://example.com/login",
|
||||
"field_name": "email",
|
||||
"field_value": "test@example.com",
|
||||
"actions": [
|
||||
{"type": "wait", "target": "1"},
|
||||
{"type": "click", "target": "input[name='password']"},
|
||||
{"type": "wait", "target": "0.5"},
|
||||
{"type": "submit"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Contact Form Workflow",
|
||||
"url": "https://example.com/contact",
|
||||
"field_name": "message",
|
||||
"field_value": "Hello, this is a test message from the LiveKit agent.",
|
||||
"actions": [
|
||||
{"type": "click", "target": "button[type='submit']"}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
for scenario in test_scenarios:
|
||||
logger.info(f"\n{'='*50}")
|
||||
logger.info(f"Testing: {scenario['name']}")
|
||||
logger.info(f"{'='*50}")
|
||||
|
||||
# Navigate to the test URL
|
||||
logger.info(f"Navigating to: {scenario['url']}")
|
||||
nav_result = await client._navigate_mcp(scenario['url'])
|
||||
logger.info(f"Navigation result: {nav_result}")
|
||||
|
||||
# Wait for page to load
|
||||
await asyncio.sleep(3)
|
||||
|
||||
# Execute the field workflow
|
||||
logger.info(f"Executing workflow for field: {scenario['field_name']}")
|
||||
workflow_result = await client.execute_field_workflow(
|
||||
field_name=scenario['field_name'],
|
||||
field_value=scenario['field_value'],
|
||||
actions=scenario['actions'],
|
||||
max_retries=3
|
||||
)
|
||||
|
||||
# Display results
|
||||
logger.info("Workflow Results:")
|
||||
logger.info(f" Success: {workflow_result['success']}")
|
||||
logger.info(f" Field Filled: {workflow_result['field_filled']}")
|
||||
logger.info(f" Detection Method: {workflow_result.get('detection_method', 'N/A')}")
|
||||
logger.info(f" Execution Time: {workflow_result['execution_time']:.2f}s")
|
||||
|
||||
if workflow_result['field_selector']:
|
||||
logger.info(f" Field Selector: {workflow_result['field_selector']}")
|
||||
|
||||
if workflow_result['actions_executed']:
|
||||
logger.info(f" Actions Executed: {len(workflow_result['actions_executed'])}")
|
||||
for i, action in enumerate(workflow_result['actions_executed']):
|
||||
status = "✓" if action['success'] else "✗"
|
||||
logger.info(f" {i+1}. {status} {action['action_type']}: {action.get('target', 'N/A')}")
|
||||
|
||||
if workflow_result['errors']:
|
||||
logger.warning(" Errors:")
|
||||
for error in workflow_result['errors']:
|
||||
logger.warning(f" - {error}")
|
||||
|
||||
# Wait between tests
|
||||
await asyncio.sleep(2)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Test execution error: {e}")
|
||||
finally:
|
||||
# Cleanup
|
||||
logger.info("Test completed")
|
||||
|
||||
|
||||
async def test_workflow_with_json_actions():
|
||||
"""Test the workflow with JSON-formatted actions (as used by the LiveKit agent)."""
|
||||
|
||||
chrome_config = {
|
||||
'mcp_server_type': 'chrome_extension',
|
||||
'mcp_server_url': 'http://localhost:3000',
|
||||
'mcp_server_command': '',
|
||||
'mcp_server_args': []
|
||||
}
|
||||
|
||||
client = MCPChromeClient(chrome_config)
|
||||
|
||||
try:
|
||||
# Navigate to Google
|
||||
await client._navigate_mcp("https://www.google.com")
|
||||
await asyncio.sleep(3)
|
||||
|
||||
# Test with JSON actions (simulating LiveKit agent call)
|
||||
actions_json = json.dumps([
|
||||
{"type": "keyboard", "target": "Enter", "delay": 0.5}
|
||||
])
|
||||
|
||||
# This simulates how the LiveKit agent would call the workflow
|
||||
logger.info("Testing workflow with JSON actions...")
|
||||
|
||||
# Parse actions (as done in the LiveKit agent)
|
||||
parsed_actions = json.loads(actions_json)
|
||||
|
||||
result = await client.execute_field_workflow(
|
||||
field_name="search",
|
||||
field_value="MCP Chrome automation",
|
||||
actions=parsed_actions,
|
||||
max_retries=3
|
||||
)
|
||||
|
||||
logger.info(f"Workflow result: {json.dumps(result, indent=2)}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"JSON actions test error: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logger.info("Starting enhanced field workflow tests...")
|
||||
|
||||
# Run the tests
|
||||
asyncio.run(test_field_workflow())
|
||||
|
||||
logger.info("\nTesting JSON actions format...")
|
||||
asyncio.run(test_workflow_with_json_actions())
|
||||
|
||||
logger.info("All tests completed!")
|
Reference in New Issue
Block a user