UNPKG

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
/** * 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); }