|
| 1 | +import { Browser, connect } from 'puppeteer-core'; |
| 2 | +import Browserbase from 'browserbase/index'; |
| 3 | + |
| 4 | +// Configuration |
| 5 | +const BROWSERBASE_PROJECT_ID = process.env['BROWSERBASE_PROJECT_ID']!; |
| 6 | +const BROWSERBASE_API_KEY = process.env['BROWSERBASE_API_KEY']!; |
| 7 | + |
| 8 | +const bb = new Browserbase({ |
| 9 | + apiKey: BROWSERBASE_API_KEY, |
| 10 | +}); |
| 11 | + |
| 12 | +async function extractFromTable(browser: Browser, cell: string) { |
| 13 | + const [page] = await browser.pages(); |
| 14 | + if (!page) { |
| 15 | + throw new Error('No pages available'); |
| 16 | + } |
| 17 | + await page.goto('https://www.showmyip.com/'); |
| 18 | + await page.waitForSelector('table.iptab'); |
| 19 | + |
| 20 | + const text = await page.evaluate((cell) => { |
| 21 | + // @ts-expect-error |
| 22 | + const rows = Array.from(document.querySelectorAll('table.iptab tr')); |
| 23 | + for (const row of rows) { |
| 24 | + // @ts-expect-error |
| 25 | + const cells = row.querySelectorAll('td'); |
| 26 | + if (cells.length > 1 && cells[0].textContent?.trim() === cell) { |
| 27 | + return cells[cells.length - 1].textContent?.trim(); |
| 28 | + } |
| 29 | + } |
| 30 | + return null; |
| 31 | + }, cell); |
| 32 | + |
| 33 | + if (!text) { |
| 34 | + throw new Error(`Failed to extract ${cell}`); |
| 35 | + } |
| 36 | + return text; |
| 37 | +} |
| 38 | + |
| 39 | +async function testProxies() { |
| 40 | + try { |
| 41 | + // Test 1: Basic proxy functionality |
| 42 | + console.log('\nTesting basic proxy functionality...'); |
| 43 | + const session = await bb.sessions.create({ |
| 44 | + projectId: BROWSERBASE_PROJECT_ID, |
| 45 | + proxies: true, |
| 46 | + }); |
| 47 | + |
| 48 | + let browser = await connect({ |
| 49 | + browserWSEndpoint: session.connectUrl, |
| 50 | + defaultViewport: null, |
| 51 | + }); |
| 52 | + |
| 53 | + const [page] = await browser.pages(); |
| 54 | + if (!page) { |
| 55 | + throw new Error('No pages available'); |
| 56 | + } |
| 57 | + |
| 58 | + await page.goto('https://www.google.com/'); |
| 59 | + const pageTitle = await page.title(); |
| 60 | + console.log('Page title:', pageTitle); |
| 61 | + |
| 62 | + await browser.disconnect(); |
| 63 | + |
| 64 | + console.log(`View session replay at https://browserbase.com/sessions/${session.id}`); |
| 65 | + |
| 66 | + // Test 2: Geolocation - Canada |
| 67 | + console.log('\nTesting proxy geolocation - Canada...'); |
| 68 | + const canadaSession = await bb.sessions.create({ |
| 69 | + projectId: BROWSERBASE_PROJECT_ID, |
| 70 | + proxies: [ |
| 71 | + { |
| 72 | + type: 'browserbase', |
| 73 | + geolocation: { |
| 74 | + country: 'CA', |
| 75 | + }, |
| 76 | + }, |
| 77 | + ], |
| 78 | + }); |
| 79 | + |
| 80 | + browser = await connect({ |
| 81 | + browserWSEndpoint: canadaSession.connectUrl, |
| 82 | + defaultViewport: null, |
| 83 | + }); |
| 84 | + |
| 85 | + const country = await extractFromTable(browser, 'Country'); |
| 86 | + console.log('Detected country:', country); |
| 87 | + await browser.disconnect(); |
| 88 | + console.log(`View session replay at https://browserbase.com/sessions/${canadaSession.id}`); |
| 89 | + |
| 90 | + // Test 3: Geolocation - New York |
| 91 | + console.log('\nTesting proxy geolocation - New York...'); |
| 92 | + const nySession = await bb.sessions.create({ |
| 93 | + projectId: BROWSERBASE_PROJECT_ID, |
| 94 | + proxies: [ |
| 95 | + { |
| 96 | + type: 'browserbase', |
| 97 | + geolocation: { |
| 98 | + country: 'US', |
| 99 | + state: 'NY', |
| 100 | + }, |
| 101 | + }, |
| 102 | + ], |
| 103 | + }); |
| 104 | + |
| 105 | + browser = await connect({ |
| 106 | + browserWSEndpoint: nySession.connectUrl, |
| 107 | + defaultViewport: null, |
| 108 | + }); |
| 109 | + |
| 110 | + const state = await extractFromTable(browser, 'Region'); |
| 111 | + console.log('Detected state:', state); |
| 112 | + await browser.disconnect(); |
| 113 | + console.log(`View session replay at https://browserbase.com/sessions/${nySession.id}`); |
| 114 | + |
| 115 | + // Test 4: Geolocation - Los Angeles |
| 116 | + console.log('\nTesting proxy geolocation - Los Angeles...'); |
| 117 | + const laSession = await bb.sessions.create({ |
| 118 | + projectId: BROWSERBASE_PROJECT_ID, |
| 119 | + proxies: [ |
| 120 | + { |
| 121 | + type: 'browserbase', |
| 122 | + geolocation: { |
| 123 | + country: 'US', |
| 124 | + state: 'CA', |
| 125 | + city: 'Los Angeles', |
| 126 | + }, |
| 127 | + }, |
| 128 | + ], |
| 129 | + }); |
| 130 | + |
| 131 | + browser = await connect({ |
| 132 | + browserWSEndpoint: laSession.connectUrl, |
| 133 | + defaultViewport: null, |
| 134 | + }); |
| 135 | + |
| 136 | + const city = await extractFromTable(browser, 'City'); |
| 137 | + console.log('Detected city:', city); |
| 138 | + await browser.disconnect(); |
| 139 | + console.log(`View session replay at https://browserbase.com/sessions/${laSession.id}`); |
| 140 | + |
| 141 | + // Test 5: Geolocation - London |
| 142 | + console.log('\nTesting proxy geolocation - London...'); |
| 143 | + const londonSession = await bb.sessions.create({ |
| 144 | + projectId: BROWSERBASE_PROJECT_ID, |
| 145 | + proxies: [ |
| 146 | + { |
| 147 | + type: 'browserbase', |
| 148 | + geolocation: { |
| 149 | + country: 'GB', |
| 150 | + city: 'London', |
| 151 | + }, |
| 152 | + }, |
| 153 | + ], |
| 154 | + }); |
| 155 | + |
| 156 | + browser = await connect({ |
| 157 | + browserWSEndpoint: londonSession.connectUrl, |
| 158 | + defaultViewport: null, |
| 159 | + }); |
| 160 | + |
| 161 | + const londonCity = await extractFromTable(browser, 'City'); |
| 162 | + console.log('Detected city:', londonCity); |
| 163 | + await browser.disconnect(); |
| 164 | + console.log(`View session replay at https://browserbase.com/sessions/${londonSession.id}`); |
| 165 | + |
| 166 | + console.log('\nProxy tests completed successfully!'); |
| 167 | + } catch (error) { |
| 168 | + console.error('An error occurred:', error); |
| 169 | + process.exit(1); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// Run the script |
| 174 | +testProxies(); |
0 commit comments