Skip to content

Commit 3e766bd

Browse files
committed
e2e tests passing playwright
1 parent 65bcb9c commit 3e766bd

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

examples/e2e/test_playwright.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
playwright_captcha,
1515
playwright_contexts,
1616
playwright_downloads,
17+
playwright_upload,
1718
)
1819

1920
bb = Browserbase(api_key=BROWSERBASE_API_KEY)
@@ -23,7 +24,6 @@
2324

2425

2526
@pytest.fixture(scope="session")
26-
@pytest.mark.skipif(True, reason="Flaky and fails on CI")
2727
def playwright() -> Generator[Playwright, None, None]:
2828
with sync_playwright() as p:
2929
yield p
@@ -33,17 +33,15 @@ def test_playwright_basic(playwright: Playwright) -> None:
3333
playwright_basic.run(playwright)
3434

3535

36-
@pytest.mark.skipif(True, reason="Flaky and fails on CI")
36+
@pytest.mark.skipif(True, reason="Flaky and fails often")
3737
def test_playwright_captcha(playwright: Playwright) -> None:
3838
playwright_captcha.run(playwright)
3939

4040

41-
@pytest.mark.skipif(True, reason="Flaky and fails on CI")
4241
def test_playwright_contexts(playwright: Playwright) -> None:
4342
playwright_contexts.run(playwright)
4443

4544

46-
@pytest.mark.skipif(True, reason="Flaky and fails on CI")
4745
def test_playwright_downloads(playwright: Playwright) -> None:
4846
playwright_downloads.run(playwright)
4947

@@ -74,3 +72,7 @@ def test_playwright_proxy_geolocation_american_city(playwright: Playwright) -> N
7472
@pytest.mark.skipif(CI, reason="Flaky and fails on CI")
7573
def test_playwright_proxy_geolocation_non_american_city(playwright: Playwright) -> None:
7674
playwright_proxy.run_geolocation_non_american_city(playwright)
75+
76+
77+
def test_playwright_upload(playwright: Playwright) -> None:
78+
playwright_upload.run(playwright)

examples/packages/logo.png

5.81 KB
Loading

examples/playwright_basic.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from playwright.sync_api import Playwright, sync_playwright
22

33
from examples import (
4-
BROWSERBASE_API_KEY,
54
BROWSERBASE_PROJECT_ID,
6-
BROWSERBASE_CONNECT_URL,
75
bb,
86
)
97

@@ -15,11 +13,8 @@ def run(playwright: Playwright) -> None:
1513
assert session.status == "RUNNING", f"Session status is {session.status}"
1614

1715
# Connect to the remote session
18-
connect_url = (
19-
f"{BROWSERBASE_CONNECT_URL}?sessionId={session.id}&apiKey={BROWSERBASE_API_KEY}"
20-
)
2116
chromium = playwright.chromium
22-
browser = chromium.connect_over_cdp(connect_url)
17+
browser = chromium.connect_over_cdp(session.connect_url)
2318
context = browser.contexts[0]
2419
page = context.pages[0]
2520

examples/playwright_upload.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)