browser-x-mcp
Version:
AI-Powered Browser Automation with Advanced Form Testing - A Model Context Provider (MCP) server that enables intelligent browser automation with form testing, element extraction, and comprehensive logging
146 lines (113 loc) โข 4.09 kB
JavaScript
/**
* Basic Usage Example for Browser[X]MCP
* This example demonstrates how to use the AI-powered browser automation system
*/
import { MCPAIInteractionAgent } from '../test/ai-mcp-interaction-test.js';
async function basicExample() {
console.log('๐ Browser[X]MCP Basic Usage Example');
// Create AI agent with configuration
const agent = new MCPAIInteractionAgent({
maxIterations: 10,
useMockAI: false, // Use real AI for demonstration
stopOnFailure: false,
headless: false // Show browser window
});
try {
// Initialize the agent
console.log('๐ Initializing AI agent...');
await agent.init();
// Run intelligent form testing
console.log('๐ค Starting AI-powered form testing...');
await agent.runInteractionTest();
// Generate comprehensive report
console.log('๐ Generating test report...');
const report = await agent.generateReport();
// Display results
console.log('\nโ
Test completed successfully!');
console.log(`๐ Success Rate: ${report.summary.successRate}`);
console.log(`โฑ๏ธ Duration: ${(report.results.duration / 1000).toFixed(1)}s`);
console.log(`๐ฏ Actions: ${report.summary.totalAIDecisions}`);
} catch (error) {
console.error('โ Test failed:', error.message);
} finally {
// Cleanup
await agent.cleanup();
console.log('๐งน Cleanup completed');
}
}
async function advancedExample() {
console.log('\n๐ฅ Browser[X]MCP Advanced Usage Example');
const agent = new MCPAIInteractionAgent({
maxIterations: 20,
useMockAI: false,
stopOnFailure: true,
headless: true,
loopThreshold: 3
});
try {
await agent.init();
// Custom page testing
console.log('๐ Testing custom webpage...');
// Navigate to specific page
await agent.callMCP('navigate_browser', {
url: 'https://example.com/form'
});
// Extract page structure
const canvasResult = await agent.callMCP('extract_xml_canvas', {});
console.log('๐ Page analysis completed');
// Run targeted testing
await agent.runInteractionTest();
console.log('โ
Advanced test completed');
} catch (error) {
console.error('โ Advanced test failed:', error.message);
} finally {
await agent.cleanup();
}
}
async function mockTestingExample() {
console.log('\nโก Browser[X]MCP Mock Testing Example (Fast)');
const agent = new MCPAIInteractionAgent({
maxIterations: 5,
useMockAI: true, // Use mock AI for speed
stopOnFailure: false,
headless: true
});
try {
await agent.init();
console.log('๐โโ๏ธ Running fast mock testing...');
const startTime = Date.now();
await agent.runInteractionTest();
const duration = Date.now() - startTime;
console.log(`โก Mock test completed in ${duration}ms`);
} catch (error) {
console.error('โ Mock test failed:', error.message);
} finally {
await agent.cleanup();
}
}
// Run examples
async function runAllExamples() {
console.log('๐ฌ Running Browser[X]MCP Examples\n');
// Run basic example
await basicExample();
// Add delay between examples
await new Promise(resolve => setTimeout(resolve, 2000));
// Run advanced example
await advancedExample();
// Add delay between examples
await new Promise(resolve => setTimeout(resolve, 2000));
// Run mock testing example
await mockTestingExample();
console.log('\n๐ All examples completed!');
}
// Export for use in other modules
export {
basicExample,
advancedExample,
mockTestingExample,
runAllExamples
};
// Run if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
runAllExamples().catch(console.error);
}