|
| 1 | +import re |
| 2 | +import zipfile |
| 3 | +import io |
| 4 | +from playwright.sync_api import Playwright, sync_playwright |
| 5 | +from examples import ( |
| 6 | + BROWSERBASE_API_KEY, |
| 7 | + BROWSERBASE_PROJECT_ID, |
| 8 | + BROWSERBASE_CONNECT_URL, |
| 9 | + bb, |
| 10 | +) |
| 11 | + |
| 12 | +download_re = re.compile(r"sandstorm-(\d{13})+\.mp3") |
| 13 | + |
| 14 | + |
| 15 | +def get_download(session_id: str) -> bytes: |
| 16 | + response = bb.sessions.downloads.list(id=session_id) |
| 17 | + return response.read() |
| 18 | + |
| 19 | + |
| 20 | +def run(playwright: Playwright): |
| 21 | + # Create a session on Browserbase |
| 22 | + session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) |
| 23 | + assert session.id is not None |
| 24 | + assert session.status == "RUNNING", f"Session status is {session.status}" |
| 25 | + |
| 26 | + # Connect to the remote session |
| 27 | + connect_url = ( |
| 28 | + f"{BROWSERBASE_CONNECT_URL}?sessionId={session.id}&apiKey={BROWSERBASE_API_KEY}" |
| 29 | + ) |
| 30 | + browser = playwright.chromium.connect_over_cdp(connect_url) |
| 31 | + context = browser.contexts[0] |
| 32 | + page = context.pages[0] |
| 33 | + |
| 34 | + # Set up CDP session for download behavior |
| 35 | + client = context.new_cdp_session(page) |
| 36 | + client.send( # pyright: ignore |
| 37 | + "Browser.setDownloadBehavior", |
| 38 | + { |
| 39 | + "behavior": "allow", |
| 40 | + "downloadPath": "downloads", |
| 41 | + "eventsEnabled": True, |
| 42 | + }, |
| 43 | + ) |
| 44 | + |
| 45 | + # Navigate to the download test page |
| 46 | + page.goto("https://browser-tests-alpha.vercel.app/api/download-test") |
| 47 | + |
| 48 | + # Start download and wait for it to complete |
| 49 | + with page.expect_download() as download_info: |
| 50 | + page.locator("#download").click() |
| 51 | + download = download_info.value |
| 52 | + |
| 53 | + # Check for download errors |
| 54 | + download_error = download.failure() |
| 55 | + if download_error: |
| 56 | + raise Exception(f"Download for session {session.id} failed: {download_error}") |
| 57 | + |
| 58 | + page.close() |
| 59 | + browser.close() |
| 60 | + |
| 61 | + # Verify the download |
| 62 | + zip_buffer = get_download(session.id) |
| 63 | + if len(zip_buffer) == 0: |
| 64 | + raise Exception(f"Download buffer is empty for session {session.id}") |
| 65 | + |
| 66 | + zip_file = zipfile.ZipFile(io.BytesIO(zip_buffer)) |
| 67 | + zip_entries = zip_file.namelist() |
| 68 | + mp3_entry = next((entry for entry in zip_entries if download_re.match(entry)), None) |
| 69 | + |
| 70 | + if not mp3_entry: |
| 71 | + raise Exception( |
| 72 | + f"Session {session.id} is missing a file matching '{download_re.pattern}' in its zip entries: {zip_entries}" |
| 73 | + ) |
| 74 | + |
| 75 | + expected_file_size = 6137541 |
| 76 | + actual_file_size = zip_file.getinfo(mp3_entry).file_size |
| 77 | + assert ( |
| 78 | + actual_file_size == expected_file_size |
| 79 | + ), f"Expected file size {expected_file_size}, but got {actual_file_size}" |
| 80 | + |
| 81 | + print("Download test passed successfully!") |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + with sync_playwright() as playwright: |
| 86 | + run(playwright) |
0 commit comments