|
| 1 | +import { chromium, type Browser } from 'playwright-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 context = browser.contexts()[0]!; |
| 14 | + const page = context.pages()[0]!; |
| 15 | + await page.goto('https://www.showmyip.com/'); |
| 16 | + await page.waitForSelector('table.iptab'); |
| 17 | + |
| 18 | + const td = page.locator(`table.iptab tr:has-text("${cell}") td:last-child`); |
| 19 | + const text = await td.textContent(); |
| 20 | + if (!text) { |
| 21 | + throw new Error(`Failed to extract ${cell}`); |
| 22 | + } |
| 23 | + return text.trim(); |
| 24 | +} |
| 25 | + |
| 26 | +async function testProxies() { |
| 27 | + try { |
| 28 | + // Test 1: Basic proxy functionality |
| 29 | + console.log('\nTesting basic proxy functionality...'); |
| 30 | + const session = await bb.sessions.create({ |
| 31 | + projectId: BROWSERBASE_PROJECT_ID, |
| 32 | + proxies: true, |
| 33 | + }); |
| 34 | + |
| 35 | + let browser = await chromium.connectOverCDP(session.connectUrl); |
| 36 | + const page = browser.contexts()[0]!.pages()[0]!; |
| 37 | + |
| 38 | + await page.goto('https://www.google.com'); |
| 39 | + const pageTitle = await page.title(); |
| 40 | + console.log('Page title:', pageTitle); |
| 41 | + |
| 42 | + await page.close(); |
| 43 | + await browser.close(); |
| 44 | + |
| 45 | + const updatedSession = await bb.sessions.retrieve(session.id); |
| 46 | + console.log('Proxy bytes used:', updatedSession.proxyBytes); |
| 47 | + console.log(`View session replay at https://browserbase.com/sessions/${session.id}`); |
| 48 | + |
| 49 | + // Test 2: Geolocation - Canada |
| 50 | + console.log('\nTesting proxy geolocation - Canada...'); |
| 51 | + const canadaSession = await bb.sessions.create({ |
| 52 | + projectId: BROWSERBASE_PROJECT_ID, |
| 53 | + proxies: [ |
| 54 | + { |
| 55 | + type: 'browserbase', |
| 56 | + geolocation: { |
| 57 | + country: 'CA', |
| 58 | + }, |
| 59 | + }, |
| 60 | + ], |
| 61 | + }); |
| 62 | + |
| 63 | + browser = await chromium.connectOverCDP(canadaSession.connectUrl); |
| 64 | + const country = await extractFromTable(browser, 'Country'); |
| 65 | + console.log('Detected country:', country); |
| 66 | + await browser.close(); |
| 67 | + console.log(`View session replay at https://browserbase.com/sessions/${canadaSession.id}`); |
| 68 | + |
| 69 | + // Test 3: Geolocation - New York |
| 70 | + console.log('\nTesting proxy geolocation - New York...'); |
| 71 | + const nySession = await bb.sessions.create({ |
| 72 | + projectId: BROWSERBASE_PROJECT_ID, |
| 73 | + proxies: [ |
| 74 | + { |
| 75 | + type: 'browserbase', |
| 76 | + geolocation: { |
| 77 | + country: 'US', |
| 78 | + state: 'NY', |
| 79 | + }, |
| 80 | + }, |
| 81 | + ], |
| 82 | + }); |
| 83 | + |
| 84 | + browser = await chromium.connectOverCDP(nySession.connectUrl); |
| 85 | + const state = await extractFromTable(browser, 'Region'); |
| 86 | + console.log('Detected state:', state); |
| 87 | + await browser.close(); |
| 88 | + console.log(`View session replay at https://browserbase.com/sessions/${nySession.id}`); |
| 89 | + |
| 90 | + // Test 4: Geolocation - Los Angeles |
| 91 | + console.log('\nTesting proxy geolocation - Los Angeles...'); |
| 92 | + const laSession = await bb.sessions.create({ |
| 93 | + projectId: BROWSERBASE_PROJECT_ID, |
| 94 | + proxies: [ |
| 95 | + { |
| 96 | + type: 'browserbase', |
| 97 | + geolocation: { |
| 98 | + country: 'US', |
| 99 | + state: 'CA', |
| 100 | + city: 'Los Angeles', |
| 101 | + }, |
| 102 | + }, |
| 103 | + ], |
| 104 | + }); |
| 105 | + |
| 106 | + browser = await chromium.connectOverCDP(laSession.connectUrl); |
| 107 | + const city = await extractFromTable(browser, 'City'); |
| 108 | + console.log('Detected city:', city); |
| 109 | + await browser.close(); |
| 110 | + console.log(`View session replay at https://browserbase.com/sessions/${laSession.id}`); |
| 111 | + |
| 112 | + // Test 5: Geolocation - London |
| 113 | + console.log('\nTesting proxy geolocation - London...'); |
| 114 | + const londonSession = await bb.sessions.create({ |
| 115 | + projectId: BROWSERBASE_PROJECT_ID, |
| 116 | + proxies: [ |
| 117 | + { |
| 118 | + type: 'browserbase', |
| 119 | + geolocation: { |
| 120 | + country: 'GB', |
| 121 | + city: 'London', |
| 122 | + }, |
| 123 | + }, |
| 124 | + ], |
| 125 | + }); |
| 126 | + |
| 127 | + browser = await chromium.connectOverCDP(londonSession.connectUrl); |
| 128 | + const londonCity = await extractFromTable(browser, 'City'); |
| 129 | + console.log('Detected city:', londonCity); |
| 130 | + await browser.close(); |
| 131 | + console.log(`View session replay at https://browserbase.com/sessions/${londonSession.id}`); |
| 132 | + |
| 133 | + console.log('\nProxy tests completed successfully!'); |
| 134 | + } catch (error) { |
| 135 | + console.error('An error occurred:', error); |
| 136 | + process.exit(1); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// Run the script |
| 141 | +testProxies(); |
0 commit comments