/** * Simple test to verify network monitoring is working */ import { Browser } from '@stock-bot/browser'; async function testNetworkMonitoring() { console.log('๐Ÿงช Testing Network Monitoring with httpbin.org...'); try { await Browser.initialize({ headless: true, timeout: 15000, blockResources: false, // Don't block resources so we can see requests }); console.log('โœ… Browser initialized'); // Test with a simple API that returns JSON const { page, contextId } = await Browser.createPageWithProxy( 'https://httpbin.org/json' ); let capturedRequests = 0; let capturedResponses = 0; let jsonResponse = null; page.onNetworkEvent(event => { console.log(`๐Ÿ“ก ${event.type.toUpperCase()}: ${event.method} ${event.url}`); if (event.type === 'request') { capturedRequests++; } if (event.type === 'response') { capturedResponses++; console.log(` Status: ${event.status}`); if (event.url.includes('httpbin.org/json') && event.responseData) { jsonResponse = event.responseData; console.log(` ๐Ÿ“Š JSON Response: ${event.responseData}`); } } }); await page.waitForLoadState('domcontentloaded'); await new Promise(resolve => setTimeout(resolve, 2000)); console.log(`\n๐Ÿ“Š Summary:`); console.log(` ๐Ÿ“ค Requests captured: ${capturedRequests}`); console.log(` ๐Ÿ“ฅ Responses captured: ${capturedResponses}`); console.log(` ๐Ÿ“ JSON data captured: ${jsonResponse ? 'Yes' : 'No'}`); await Browser.closeContext(contextId); return true; } catch (error) { console.error('โŒ Test failed:', error); return false; } finally { await Browser.close(); } } async function testWithProxy() { console.log('\n๐ŸŒ Testing with proxy to see IP change...'); try { await Browser.initialize({ headless: true, timeout: 10000, blockResources: false, }); // Test IP without proxy console.log('๐Ÿ“ Getting IP without proxy...'); const { page: page1, contextId: ctx1 } = await Browser.createPageWithProxy( 'https://httpbin.org/ip' ); let ipWithoutProxy = null; page1.onNetworkEvent(event => { if (event.type === 'response' && event.url.includes('/ip') && event.responseData) { ipWithoutProxy = JSON.parse(event.responseData).origin; console.log(` ๐Ÿ”น Your IP: ${ipWithoutProxy}`); } }); await page1.waitForLoadState('domcontentloaded'); await new Promise(resolve => setTimeout(resolve, 1000)); await Browser.closeContext(ctx1); // Test IP with proxy console.log('๐Ÿ”„ Getting IP with proxy...'); const { page: page2, contextId: ctx2 } = await Browser.createPageWithProxy( 'https://httpbin.org/ip', 'http://doimvbnb-US-rotate:w5fpiwrb9895@p.webshare.io:80' ); let ipWithProxy = null; page2.onNetworkEvent(event => { if (event.type === 'response' && event.url.includes('/ip') && event.responseData) { ipWithProxy = JSON.parse(event.responseData).origin; console.log(` ๐Ÿ”ธ Proxy IP: ${ipWithProxy}`); } }); await page2.waitForLoadState('domcontentloaded'); await new Promise(resolve => setTimeout(resolve, 1000)); await Browser.closeContext(ctx2); if (ipWithoutProxy && ipWithProxy && ipWithoutProxy !== ipWithProxy) { console.log('โœ… Proxy is working - IPs are different!'); } else { console.log('โš ๏ธ Proxy may not be working - IPs are the same or not captured'); } } catch (error) { console.error('โŒ Proxy test failed:', error); } finally { await Browser.close(); } } async function runTests() { console.log('๐Ÿš€ Network Monitoring Verification Tests\n'); const basicResult = await testNetworkMonitoring(); await testWithProxy(); console.log(`\n๐Ÿ Network monitoring: ${basicResult ? 'โœ… WORKING' : 'โŒ FAILED'}`); } if (import.meta.main) { runTests().catch(console.error); } export { testNetworkMonitoring, testWithProxy };