|
| 1 | +from pathlib import Path |
| 2 | +from playwright.sync_api import Playwright, sync_playwright |
| 3 | + |
| 4 | +from examples import ( |
| 5 | + BROWSERBASE_API_KEY, |
| 6 | + BROWSERBASE_PROJECT_ID, |
| 7 | + BROWSERBASE_CONNECT_URL, |
| 8 | + bb, |
| 9 | +) |
| 10 | + |
| 11 | +PATH_TO_UPLOAD = Path.cwd() / "examples" / "packages" / "logo.png" |
| 12 | + |
| 13 | + |
| 14 | +def run(playwright: Playwright) -> None: |
| 15 | + # Create a session |
| 16 | + session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) |
| 17 | + |
| 18 | + # Construct the URL |
| 19 | + url = ( |
| 20 | + f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" |
| 21 | + ) |
| 22 | + |
| 23 | + # Connect to the browser |
| 24 | + browser = playwright.chromium.connect_over_cdp(url) |
| 25 | + context = browser.contexts[0] |
| 26 | + page = context.pages[0] |
| 27 | + |
| 28 | + try: |
| 29 | + # Navigate to the upload test page |
| 30 | + page.goto("https://browser-tests-alpha.vercel.app/api/upload-test") |
| 31 | + |
| 32 | + # Locate the file input element |
| 33 | + file_input = page.locator("#fileUpload") |
| 34 | + file_input.set_input_files(str(PATH_TO_UPLOAD)) |
| 35 | + |
| 36 | + # Get the uploaded file name |
| 37 | + file_name_span = page.locator("#fileName") |
| 38 | + file_name = file_name_span.inner_text() |
| 39 | + |
| 40 | + # Get the uploaded file size |
| 41 | + file_size_span = page.locator("#fileSize") |
| 42 | + file_size = int(file_size_span.inner_text()) |
| 43 | + |
| 44 | + # Assert the file name and size |
| 45 | + assert ( |
| 46 | + file_name == "logo.png" |
| 47 | + ), f"Expected file name to be 'logo.png', but got '{file_name}'" |
| 48 | + assert ( |
| 49 | + file_size > 0 |
| 50 | + ), f"Expected file size to be greater than 0, but got {file_size}" |
| 51 | + |
| 52 | + print("File upload test passed successfully!") |
| 53 | + |
| 54 | + finally: |
| 55 | + # Clean up |
| 56 | + page.close() |
| 57 | + browser.close() |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + with sync_playwright() as playwright: |
| 62 | + run(playwright) |
0 commit comments