|
| 1 | +from typing import Dict, Any |
| 2 | +import os |
| 3 | +import pytest |
| 4 | +from playwright.sync_api import Page, Playwright, sync_playwright |
| 5 | +from browserbase.types.session_create_params import ( |
| 6 | + ProxiesUnionMember1, |
| 7 | + ProxiesUnionMember1BrowserbaseProxyConfig, |
| 8 | + ProxiesUnionMember1BrowserbaseProxyConfigGeolocation, |
| 9 | + ProxiesUnionMember1ExternalProxyConfig, |
| 10 | +) |
| 11 | +import time |
| 12 | + |
| 13 | +from examples import ( |
| 14 | + BROWSERBASE_API_KEY, |
| 15 | + BROWSERBASE_PROJECT_ID, |
| 16 | + BROWSERBASE_CONNECT_URL, |
| 17 | + bb, |
| 18 | +) |
| 19 | + |
| 20 | +GRACEFUL_SHUTDOWN_TIMEOUT = 30000 # Assuming 30 seconds, adjust as needed |
| 21 | +CI = os.environ.get("CI", "false").lower() == "true" |
| 22 | + |
| 23 | + |
| 24 | +def check_proxy_bytes(session_id: str) -> None: |
| 25 | + time.sleep(GRACEFUL_SHUTDOWN_TIMEOUT / 1000) |
| 26 | + updated_session = bb.sessions.retrieve(id=session_id) |
| 27 | + assert ( |
| 28 | + updated_session.proxy_bytes is not None and updated_session.proxy_bytes > 0 |
| 29 | + ), f"Proxy bytes: {updated_session.proxy_bytes}" |
| 30 | + |
| 31 | + |
| 32 | +def generate_proxy_config(proxy_data: Dict[str, Any]) -> ProxiesUnionMember1: |
| 33 | + """ |
| 34 | + Generate the appropriate ProxiesUnionMember1 type given a deeply nested JSON. |
| 35 | +
|
| 36 | + :param proxy_data: A dictionary containing proxy configuration data |
| 37 | + :return: An instance of ProxiesUnionMember1 |
| 38 | + """ |
| 39 | + if proxy_data.get("type") == "browserbase": |
| 40 | + for key in ["geolocation", "domainPattern"]: |
| 41 | + if proxy_data.get(key) is None: |
| 42 | + raise ValueError(f"Missing required key in proxy config: {key}") |
| 43 | + |
| 44 | + geolocation = proxy_data["geolocation"] |
| 45 | + for key in ["country", "city", "state"]: |
| 46 | + if geolocation.get(key) is None: |
| 47 | + raise ValueError(f"Missing required key in geolocation: {key}") |
| 48 | + return ProxiesUnionMember1BrowserbaseProxyConfig( |
| 49 | + type="browserbase", |
| 50 | + domain_pattern=proxy_data.get("domainPattern", ""), |
| 51 | + geolocation=ProxiesUnionMember1BrowserbaseProxyConfigGeolocation( |
| 52 | + country=geolocation.get("country", ""), |
| 53 | + city=geolocation.get("city", ""), |
| 54 | + state=geolocation.get("state", ""), |
| 55 | + ), |
| 56 | + ) |
| 57 | + elif proxy_data.get("type") == "external": |
| 58 | + for key in ["server", "domainPattern", "username", "password"]: |
| 59 | + if proxy_data.get(key) is None: |
| 60 | + raise ValueError(f"Missing required key in proxy config: {key}") |
| 61 | + return ProxiesUnionMember1ExternalProxyConfig( |
| 62 | + type="external", |
| 63 | + server=proxy_data["server"], |
| 64 | + domain_pattern=proxy_data["domainPattern"], |
| 65 | + username=proxy_data["username"], |
| 66 | + password=proxy_data["password"], |
| 67 | + ) |
| 68 | + else: |
| 69 | + raise ValueError(f"Invalid proxy type: {proxy_data.get('type')}") |
| 70 | + |
| 71 | + |
| 72 | +def run(playwright: Playwright): |
| 73 | + def test_enable_via_create_session(): |
| 74 | + session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID, proxies=True) |
| 75 | + |
| 76 | + browser = playwright.chromium.connect_over_cdp( |
| 77 | + f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" |
| 78 | + ) |
| 79 | + |
| 80 | + context = browser.contexts[0] |
| 81 | + page = context.pages[0] |
| 82 | + page.goto("https://www.google.com") |
| 83 | + page_title = page.title() |
| 84 | + |
| 85 | + page.close() |
| 86 | + browser.close() |
| 87 | + |
| 88 | + assert page_title == "Google" |
| 89 | + check_proxy_bytes(session.id) |
| 90 | + |
| 91 | + def test_enable_via_querystring_with_created_session(): |
| 92 | + session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID, proxies=True) |
| 93 | + |
| 94 | + browser = playwright.chromium.connect_over_cdp( |
| 95 | + f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}&enableProxy=true" |
| 96 | + ) |
| 97 | + |
| 98 | + context = browser.contexts[0] |
| 99 | + page = context.pages[0] |
| 100 | + page.goto("https://www.google.com/") |
| 101 | + page_title = page.title() |
| 102 | + |
| 103 | + page.close() |
| 104 | + browser.close() |
| 105 | + |
| 106 | + assert page_title == "Google" |
| 107 | + check_proxy_bytes(session.id) |
| 108 | + |
| 109 | + def extract_from_table(page: Page, cell: str) -> str: |
| 110 | + page.goto("https://www.showmyip.com/") |
| 111 | + page.wait_for_selector("table.iptab") |
| 112 | + |
| 113 | + td = page.locator(f"table.iptab tr:has-text('{cell}') td:last-child") |
| 114 | + |
| 115 | + text = td.text_content() |
| 116 | + if not text: |
| 117 | + raise Exception(f"Failed to extract {cell}") |
| 118 | + return text.strip() |
| 119 | + |
| 120 | + @pytest.mark.skipif(CI, reason="Flaky and fails on CI") |
| 121 | + def test_geolocation_country(): |
| 122 | + session = bb.sessions.create( |
| 123 | + project_id=BROWSERBASE_PROJECT_ID, |
| 124 | + proxies=[ |
| 125 | + generate_proxy_config( |
| 126 | + { |
| 127 | + "geolocation": { |
| 128 | + "country": "CA", |
| 129 | + }, |
| 130 | + "type": "browserbase", |
| 131 | + } |
| 132 | + ) |
| 133 | + ], |
| 134 | + ) |
| 135 | + |
| 136 | + browser = playwright.chromium.connect_over_cdp( |
| 137 | + f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" |
| 138 | + ) |
| 139 | + |
| 140 | + context = browser.contexts[0] |
| 141 | + page = context.pages[0] |
| 142 | + |
| 143 | + country = extract_from_table(page, "Country") |
| 144 | + |
| 145 | + page.close() |
| 146 | + browser.close() |
| 147 | + |
| 148 | + assert country == "Canada" |
| 149 | + |
| 150 | + @pytest.mark.skipif(CI, reason="Flaky and fails on CI") |
| 151 | + def test_geolocation_state(): |
| 152 | + session = bb.sessions.create( |
| 153 | + project_id=BROWSERBASE_PROJECT_ID, |
| 154 | + proxies=[ |
| 155 | + generate_proxy_config( |
| 156 | + { |
| 157 | + "geolocation": { |
| 158 | + "country": "US", |
| 159 | + "state": "NY", |
| 160 | + }, |
| 161 | + "type": "browserbase", |
| 162 | + } |
| 163 | + ) |
| 164 | + ], |
| 165 | + ) |
| 166 | + |
| 167 | + browser = playwright.chromium.connect_over_cdp( |
| 168 | + f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" |
| 169 | + ) |
| 170 | + |
| 171 | + context = browser.contexts[0] |
| 172 | + page = context.pages[0] |
| 173 | + |
| 174 | + state = extract_from_table(page, "Region") |
| 175 | + |
| 176 | + page.close() |
| 177 | + browser.close() |
| 178 | + |
| 179 | + assert state == "New York" |
| 180 | + |
| 181 | + @pytest.mark.skipif(CI, reason="Flaky and fails on CI") |
| 182 | + def test_geolocation_american_city(): |
| 183 | + session = bb.sessions.create( |
| 184 | + project_id=BROWSERBASE_PROJECT_ID, |
| 185 | + proxies=[ |
| 186 | + generate_proxy_config( |
| 187 | + { |
| 188 | + "geolocation": { |
| 189 | + "city": "Los Angeles", |
| 190 | + "country": "US", |
| 191 | + "state": "CA", |
| 192 | + }, |
| 193 | + "type": "browserbase", |
| 194 | + } |
| 195 | + ) |
| 196 | + ], |
| 197 | + ) |
| 198 | + |
| 199 | + browser = playwright.chromium.connect_over_cdp( |
| 200 | + f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" |
| 201 | + ) |
| 202 | + |
| 203 | + context = browser.contexts[0] |
| 204 | + page = context.pages[0] |
| 205 | + |
| 206 | + city = extract_from_table(page, "City") |
| 207 | + |
| 208 | + page.close() |
| 209 | + browser.close() |
| 210 | + |
| 211 | + assert city == "Los Angeles" |
| 212 | + |
| 213 | + @pytest.mark.skipif(CI, reason="Flaky and fails on CI") |
| 214 | + def test_geolocation_non_american_city(): |
| 215 | + session = bb.sessions.create( |
| 216 | + project_id=BROWSERBASE_PROJECT_ID, |
| 217 | + proxies=[ |
| 218 | + generate_proxy_config( |
| 219 | + { |
| 220 | + "geolocation": { |
| 221 | + "city": "London", |
| 222 | + "country": "GB", |
| 223 | + }, |
| 224 | + "type": "browserbase", |
| 225 | + } |
| 226 | + ) |
| 227 | + ], |
| 228 | + ) |
| 229 | + |
| 230 | + browser = playwright.chromium.connect_over_cdp( |
| 231 | + f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" |
| 232 | + ) |
| 233 | + |
| 234 | + context = browser.contexts[0] |
| 235 | + page = context.pages[0] |
| 236 | + |
| 237 | + city = extract_from_table(page, "City") |
| 238 | + |
| 239 | + page.close() |
| 240 | + browser.close() |
| 241 | + |
| 242 | + assert city == "London" |
| 243 | + |
| 244 | + # Run the tests |
| 245 | + test_enable_via_create_session() |
| 246 | + test_enable_via_querystring_with_created_session() |
| 247 | + test_geolocation_country() |
| 248 | + test_geolocation_state() |
| 249 | + test_geolocation_american_city() |
| 250 | + test_geolocation_non_american_city() |
| 251 | + |
| 252 | + |
| 253 | +if __name__ == "__main__": |
| 254 | + with sync_playwright() as playwright: |
| 255 | + run(playwright) |
0 commit comments