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