Skip to content

Commit 8a23bca

Browse files
authored
py: Modifies install_addon method to support installing unpacked addons (#10308)
* py: Modifies install_addon method to support installing unpacked addons * Fix lint * adds copy_file block to increase code readability * Fix lint * Fix lint
1 parent 203dadb commit 8a23bca

3 files changed

Lines changed: 38 additions & 11 deletions

File tree

py/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ copy_file(
6565
out = "selenium/webdriver/firefox/webdriver_prefs.json",
6666
)
6767

68+
copy_file(
69+
name = "favourite_color",
70+
src = "//third_party/firebug:favourite_colour-1.1-an+fx.xpi",
71+
out = "test/selenium/webdriver/firefox/favourite_colour-1.1-an+fx.xpi",
72+
)
73+
6874
py_library(
6975
name = "selenium",
7076
srcs = glob(
@@ -78,6 +84,7 @@ py_library(
7884
":get-attribute",
7985
":is-displayed",
8086
":mutation-listener",
87+
":favourite_color"
8188
] + [":create-cdp-srcs-" + n for n in BROWSER_VERSIONS],
8289
imports = ["."],
8390
visibility = ["//visibility:public"],

py/selenium/webdriver/firefox/webdriver.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
import base64
18+
from io import BytesIO
19+
import os
1820
from shutil import rmtree
1921
import warnings
2022
from contextlib import contextmanager
2123
from typing import NoReturn
24+
import zipfile
2225

2326
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2427
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
@@ -232,7 +235,7 @@ def context(self, context):
232235
finally:
233236
self.set_context(initial_context)
234237

235-
def install_addon(self, path, temporary=None) -> str:
238+
def install_addon(self, path, temporary=False) -> str:
236239
"""
237240
Installs Firefox addon.
238241
@@ -246,12 +249,21 @@ def install_addon(self, path, temporary=None) -> str:
246249
247250
driver.install_addon('/path/to/firebug.xpi')
248251
"""
249-
with open(path, 'rb') as file:
250-
addon = (base64.b64encode(file.read()).decode('UTF-8'))
251252

252-
payload = {"addon": addon}
253-
if temporary:
254-
payload["temporary"] = temporary
253+
if(os.path.isdir(path)):
254+
fp = BytesIO()
255+
path_root = len(path) + 1 # account for trailing slash
256+
with zipfile.ZipFile(fp, 'w', zipfile.ZIP_DEFLATED) as zipped:
257+
for base, dirs, files in os.walk(path):
258+
for fyle in files:
259+
filename = os.path.join(base, fyle)
260+
zipped.write(filename, filename[path_root:])
261+
addon = base64.b64encode(fp.getvalue()).decode('UTF-8')
262+
else:
263+
with open(path, 'rb') as file:
264+
addon = (base64.b64encode(file.read()).decode('UTF-8'))
265+
266+
payload = {"addon": addon, "temporary": temporary}
255267
return self.execute("INSTALL_ADDON", payload)["value"]
256268

257269
def uninstall_addon(self, identifier) -> NoReturn:

py/test/selenium/webdriver/firefox/ff_installs_addons_tests.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,25 @@
2020
from selenium.common.exceptions import WebDriverException
2121

2222

23+
def test_install_addon_temporary(driver):
24+
extension = os.path.join(os.path.dirname(os.path.abspath(__file__)),
25+
'favourite_colour-1.1-an+fx.xpi')
26+
27+
id = driver.install_addon(extension, True)
28+
assert id == '[email protected]'
29+
30+
2331
def test_install_addon(driver):
24-
extension = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
25-
'../../../../third_party/firebug/favourite_colour-1.1-an+fx.xpi')
32+
extension = os.path.join(os.path.dirname(os.path.abspath(__file__)),
33+
'favourite_colour-1.1-an+fx.xpi')
2634

27-
id = driver.install_addon(extension)
35+
id = driver.install_addon(extension, False)
2836
assert id == '[email protected]'
2937

3038

3139
def test_uninstall_addon(driver):
32-
extension = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
33-
'../../../../third_party/firebug/favourite_colour-1.1-an+fx.xpi')
40+
extension = os.path.join(os.path.dirname(os.path.abspath(__file__)),
41+
'favourite_colour-1.1-an+fx.xpi')
3442

3543
id = driver.install_addon(extension)
3644
try:

0 commit comments

Comments
 (0)