/** * Simple Browser and Network Monitoring Test */ import { Browser } from '@stock-bot/browser'; async function testBasicBrowser() { console.log('๐Ÿš€ Testing basic browser functionality...'); try { // Initialize browser await Browser.initialize({ headless: true, timeout: 15000, blockResources: false, enableNetworkLogging: true, }); console.log('โœ… Browser initialized'); // Test 1: Simple page without proxy console.log('๐Ÿ“„ Testing simple page without proxy...'); const { page, contextId } = await Browser.createPageWithProxy( 'https://httpbin.org/json' ); let capturedData = null; let eventCount = 0; page.onNetworkEvent(event => { eventCount++; console.log(`๐Ÿ“ก Event ${eventCount}: ${event.type} - ${event.method} ${event.url}`); if (event.type === 'response' && event.url.includes('httpbin.org/json')) { console.log(` ๐Ÿ“Š Status: ${event.status}`); if (event.responseData) { capturedData = event.responseData; console.log(` ๐Ÿ“ Response: ${event.responseData}`); } } }); await page.waitForLoadState('domcontentloaded'); await new Promise(resolve => setTimeout(resolve, 2000)); console.log(`โœ… Test completed. Events captured: ${eventCount}`); if (capturedData) { console.log('โœ… Successfully captured response data'); } await Browser.closeContext(contextId); return true; } catch (error) { console.error('โŒ Basic test failed:', error); return false; } finally { await Browser.close(); } } async function testProxyConnection() { console.log('\n๐Ÿ”„ Testing proxy connection...'); try { await Browser.initialize({ headless: true, timeout: 10000, blockResources: false, }); // Test different proxy formats const proxyConfigs = [ null, // No proxy 'http://doimvbnb-US-rotate:w5fpiwrb9895@p.webshare.io:80', ]; for (const proxy of proxyConfigs) { console.log(`\n๐ŸŒ Testing with proxy: ${proxy || 'No proxy'}`); try { const { page, contextId } = await Browser.createPageWithProxy( 'https://httpbin.org/ip', proxy ); page.onNetworkEvent(event => { if (event.type === 'response' && event.url.includes('httpbin.org/ip')) { console.log(` ๐Ÿ“ IP Response: ${event.responseData}`); } }); await page.waitForLoadState('domcontentloaded'); await new Promise(resolve => setTimeout(resolve, 1500)); await Browser.closeContext(contextId); console.log(' โœ… Success'); } catch (error) { console.log(` โŒ Failed: ${error.message}`); } } } catch (error) { console.error('โŒ Proxy test setup failed:', error); } finally { await Browser.close(); } } async function testIBWithWorkaround() { console.log('\n๐Ÿฆ Testing IB endpoint with workaround...'); try { await Browser.initialize({ headless: true, timeout: 20000, blockResources: true, // Block resources for performance }); // Try without proxy first console.log('๐ŸŒ Attempting IB without proxy...'); try { const { page, contextId } = await Browser.createPageWithProxy( 'https://www.interactivebrokers.com' ); let responseCount = 0; page.onNetworkEvent(event => { if (event.type === 'response') { responseCount++; console.log(` ๐Ÿ“ฅ Response ${responseCount}: ${event.status} ${event.url}`); } }); await page.waitForLoadState('domcontentloaded'); await new Promise(resolve => setTimeout(resolve, 3000)); console.log(`โœ… IB main page loaded. Responses: ${responseCount}`); await Browser.closeContext(contextId); } catch (error) { console.log(`โŒ IB without proxy failed: ${error.message}`); } } catch (error) { console.error('โŒ IB test failed:', error); } finally { await Browser.close(); } } // Run tests async function runAllTests() { console.log('๐Ÿงช Starting Browser Network Monitoring Tests\n'); const basicResult = await testBasicBrowser(); await testProxyConnection(); await testIBWithWorkaround(); console.log(`\n๐Ÿ Basic functionality: ${basicResult ? 'โœ… PASS' : 'โŒ FAIL'}`); console.log('โœ… All tests completed!'); } if (import.meta.main) { runAllTests().catch(console.error); } export { testBasicBrowser, testProxyConnection, testIBWithWorkaround };