diff options
author | Wojtek Kosior <koszko@koszko.org> | 2021-12-13 17:58:29 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2021-12-13 17:58:29 +0100 |
commit | c699b6409e98fe64a70417a18b6e335b4c60f86d (patch) | |
tree | d01108437eaaa89fb167b35a7e4439e69b5d38d2 /test/profiles.py | |
parent | 4c933941e64ba2b4b5da3c19e5ed5924a30e0752 (diff) | |
download | browser-extension-c699b6409e98fe64a70417a18b6e335b4c60f86d.tar.gz browser-extension-c699b6409e98fe64a70417a18b6e335b4c60f86d.zip |
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.
Diffstat (limited to 'test/profiles.py')
-rwxr-xr-x | test/profiles.py | 26 |
1 files changed, 24 insertions, 2 deletions
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) |