|
| 1 | +import os |
| 2 | +import zipfile |
| 3 | +import time |
| 4 | +from pathlib import Path |
| 5 | +from playwright.sync_api import sync_playwright, Playwright |
| 6 | +from browserbase.types.extension import Extension |
| 7 | +from browserbase.types.session import Session |
| 8 | +from examples import ( |
| 9 | + BROWSERBASE_API_KEY, |
| 10 | + BROWSERBASE_PROJECT_ID, |
| 11 | + BROWSERBASE_CONNECT_URL, |
| 12 | + bb, |
| 13 | +) |
| 14 | +from io import BytesIO |
| 15 | + |
| 16 | +PATH_TO_EXTENSION = ( |
| 17 | + Path.cwd() / "examples" / "packages" / "extensions" / "browserbase-test" |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def zip_extension(path: Path = PATH_TO_EXTENSION, save_local: bool = False) -> BytesIO: |
| 22 | + """ |
| 23 | + Create an in-memory zip file from the contents of the given folder. |
| 24 | + Mark save_local=True to save the zip file to a local file. |
| 25 | + """ |
| 26 | + # Ensure we're looking at an extension |
| 27 | + assert "manifest.json" in os.listdir( |
| 28 | + path |
| 29 | + ), "No manifest.json found in the extension folder." |
| 30 | + |
| 31 | + # Create a BytesIO object to hold the zip file in memory |
| 32 | + memory_zip = BytesIO() |
| 33 | + |
| 34 | + # Create a ZipFile object |
| 35 | + with zipfile.ZipFile(memory_zip, "w", zipfile.ZIP_DEFLATED) as zf: |
| 36 | + # Recursively walk through the directory |
| 37 | + for root, _, files in os.walk(path): |
| 38 | + for file in files: |
| 39 | + # Create the full file path |
| 40 | + file_path = os.path.join(root, file) |
| 41 | + # Calculate the archive name (path relative to the root directory) |
| 42 | + archive_name = os.path.relpath(file_path, path) |
| 43 | + # Add the file to the zip |
| 44 | + zf.write(file_path, archive_name) |
| 45 | + |
| 46 | + if save_local: |
| 47 | + with open(f"{path}.zip", "wb") as f: |
| 48 | + f.write(memory_zip.getvalue()) |
| 49 | + |
| 50 | + return memory_zip |
| 51 | + |
| 52 | + |
| 53 | +def create_extension(): |
| 54 | + zip_data = zip_extension(save_local=True) |
| 55 | + extension: Extension = bb.extensions.create( |
| 56 | + file=("extension.zip", zip_data.getvalue()) |
| 57 | + ) |
| 58 | + return extension.id |
| 59 | + |
| 60 | + |
| 61 | +def get_extension(id: str) -> Extension: |
| 62 | + return bb.extensions.retrieve(id) |
| 63 | + |
| 64 | + |
| 65 | +def delete_extension(id: str): |
| 66 | + bb.extensions.delete(id) |
| 67 | + |
| 68 | + |
| 69 | +def run(playwright: Playwright): |
| 70 | + extension_id = None |
| 71 | + |
| 72 | + # Create extension |
| 73 | + extension_id = create_extension() |
| 74 | + print(f"Created extension with ID: {extension_id}") |
| 75 | + |
| 76 | + # Get extension |
| 77 | + extension = get_extension(extension_id) |
| 78 | + print(f"Retrieved extension: {extension}") |
| 79 | + |
| 80 | + # Use extension |
| 81 | + session: Session = bb.sessions.create( |
| 82 | + project_id=BROWSERBASE_PROJECT_ID, |
| 83 | + extension_id=extension.id, |
| 84 | + ) |
| 85 | + |
| 86 | + browser = playwright.chromium.connect_over_cdp( |
| 87 | + f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" |
| 88 | + ) |
| 89 | + context = browser.contexts[0] |
| 90 | + page = context.pages[0] |
| 91 | + |
| 92 | + console_messages: list[str] = [] |
| 93 | + page.on("console", lambda msg: console_messages.append(msg.text)) |
| 94 | + |
| 95 | + page.goto("https://www.browserbase.com/") |
| 96 | + |
| 97 | + # Wait for the extension to load and log a message |
| 98 | + start = time.time() |
| 99 | + while time.time() - start < 10: |
| 100 | + if "browserbase test extension image loaded" in console_messages: |
| 101 | + break |
| 102 | + assert ( |
| 103 | + "browserbase test extension image loaded" in console_messages |
| 104 | + ), f"Expected message not found in console logs. Messages: {console_messages}" |
| 105 | + |
| 106 | + page.close() |
| 107 | + browser.close() |
| 108 | + |
| 109 | + # Use extension with proxies |
| 110 | + session_with_proxy: Session = bb.sessions.create( |
| 111 | + project_id=BROWSERBASE_PROJECT_ID, |
| 112 | + extension_id=extension_id, |
| 113 | + proxies=True, |
| 114 | + ) |
| 115 | + |
| 116 | + browser = playwright.chromium.connect_over_cdp( |
| 117 | + f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session_with_proxy.id}" |
| 118 | + ) |
| 119 | + context = browser.contexts[0] |
| 120 | + page = context.pages[0] |
| 121 | + |
| 122 | + console_messages: list[str] = [] |
| 123 | + page.on("console", lambda msg: console_messages.append(msg.text)) |
| 124 | + |
| 125 | + page.goto("https://www.browserbase.com/") |
| 126 | + |
| 127 | + # Wait for the extension to load and log a message (longer timeout for proxies) |
| 128 | + start = time.time() |
| 129 | + while time.time() - start < 10: |
| 130 | + if "browserbase test extension image loaded" in console_messages: |
| 131 | + break |
| 132 | + assert ( |
| 133 | + "browserbase test extension image loaded" in console_messages |
| 134 | + ), f"Expected message not found in console logs. Messages: {console_messages}" |
| 135 | + |
| 136 | + page.close() |
| 137 | + browser.close() |
| 138 | + |
| 139 | + # Delete extension |
| 140 | + delete_extension(extension_id) |
| 141 | + print(f"Deleted extension with ID: {extension_id}") |
| 142 | + |
| 143 | + # Verify deleted extension is unusable |
| 144 | + try: |
| 145 | + get_extension(extension_id) |
| 146 | + raise AssertionError("Expected to fail when retrieving deleted extension") |
| 147 | + except Exception as e: |
| 148 | + print(f"Failed to get deleted extension as expected: {str(e)}") |
| 149 | + |
| 150 | + try: |
| 151 | + bb.sessions.create( |
| 152 | + project_id=BROWSERBASE_PROJECT_ID, |
| 153 | + extension_id=extension_id, |
| 154 | + ) |
| 155 | + raise AssertionError( |
| 156 | + "Expected to fail when creating session with deleted extension" |
| 157 | + ) |
| 158 | + except Exception as e: |
| 159 | + print(f"Failed to create session with deleted extension as expected: {str(e)}") |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + with sync_playwright() as playwright: |
| 164 | + run(playwright) |
0 commit comments