From c699b6409e98fe64a70417a18b6e335b4c60f86d Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 13 Dec 2021 17:58:29 +0100 Subject: facilitate creating and installing WebExtensions during tests It is now possible to more conveniently test WebExtension APIs code by wrapping it into a test WebExtension and temporarily installing in the driven browser. --- test/profiles.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'test/profiles.py') diff --git a/test/profiles.py b/test/profiles.py index 1530aea..795a0db 100755 --- a/test/profiles.py +++ b/test/profiles.py @@ -27,7 +27,8 @@ Browser profiles and Selenium driver initialization from selenium import webdriver from selenium.webdriver.firefox.options import Options -import time +import json +from shutil import rmtree from .misc_constants import * @@ -35,7 +36,8 @@ class HaketiloFirefox(webdriver.Firefox): """ This wrapper class around selenium.webdriver.Firefox adds a `loaded_scripts` instance property that gets resetted to an empty array every time the - `get()` method is called. + `get()` method is called and also facilitates removing the temporary + profile directory after Firefox quits. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -48,6 +50,11 @@ class HaketiloFirefox(webdriver.Firefox): self.reset_loaded_scripts() super().get(*args, **kwargs) + def quit(self, *args, **kwargs): + profile_path = self.firefox_profile.path + super().quit(*args, **kwargs) + rmtree(profile_path, ignore_errors=True) + def set_profile_proxy(profile, proxy_host, proxy_port): """ Create a Firefox profile that uses the specified HTTP proxy for all @@ -67,6 +74,20 @@ def set_profile_proxy(profile, proxy_host, proxy_port): def set_profile_console_logging(profile): profile.set_preference('devtools.console.stdout.content', True) +# The function below seems not to work for extensions that are +# temporarily-installed in Firefox safe mode. Testing is needed to see if it +# works with non-temporary extensions (without safe mode). +def set_webextension_uuid(profile, extension_id, uuid=default_extension_uuid): + """ + Firefox would normally assign a unique, random UUID to installed extension. + This UUID is needed to easily navigate to extension's settings page (and + other extension's pages). Since there's no way to learn such UUID with + current WebDriver implementation, this function works around this by telling + Firefox to use a predefined UUID for a certain extension. + """ + profile.set_preference('extensions.webextensions.uuids', + json.dumps({extension_id: uuid})) + def firefox_safe_mode(firefox_binary=default_firefox_binary, proxy_host=default_proxy_host, proxy_port=default_proxy_port): @@ -97,6 +118,7 @@ def firefox_with_profile(firefox_binary=default_firefox_binary, profile = webdriver.FirefoxProfile(profile_dir) set_profile_proxy(profile, proxy_host, proxy_port) set_profile_console_logging(profile) + set_webextension_uuid(profile, default_haketilo_id) return HaketiloFirefox(firefox_profile=profile, firefox_binary=firefox_binary) -- cgit v1.2.3