Revision c699b640
Added by koszko over 1 year ago
test/profiles.py | ||
---|---|---|
27 | 27 |
|
28 | 28 |
from selenium import webdriver |
29 | 29 |
from selenium.webdriver.firefox.options import Options |
30 |
import time |
|
30 |
import json |
|
31 |
from shutil import rmtree |
|
31 | 32 |
|
32 | 33 |
from .misc_constants import * |
33 | 34 |
|
... | ... | |
35 | 36 |
""" |
36 | 37 |
This wrapper class around selenium.webdriver.Firefox adds a `loaded_scripts` |
37 | 38 |
instance property that gets resetted to an empty array every time the |
38 |
`get()` method is called. |
|
39 |
`get()` method is called and also facilitates removing the temporary |
|
40 |
profile directory after Firefox quits. |
|
39 | 41 |
""" |
40 | 42 |
def __init__(self, *args, **kwargs): |
41 | 43 |
super().__init__(*args, **kwargs) |
... | ... | |
48 | 50 |
self.reset_loaded_scripts() |
49 | 51 |
super().get(*args, **kwargs) |
50 | 52 |
|
53 |
def quit(self, *args, **kwargs): |
|
54 |
profile_path = self.firefox_profile.path |
|
55 |
super().quit(*args, **kwargs) |
|
56 |
rmtree(profile_path, ignore_errors=True) |
|
57 |
|
|
51 | 58 |
def set_profile_proxy(profile, proxy_host, proxy_port): |
52 | 59 |
""" |
53 | 60 |
Create a Firefox profile that uses the specified HTTP proxy for all |
... | ... | |
67 | 74 |
def set_profile_console_logging(profile): |
68 | 75 |
profile.set_preference('devtools.console.stdout.content', True) |
69 | 76 |
|
77 |
# The function below seems not to work for extensions that are |
|
78 |
# temporarily-installed in Firefox safe mode. Testing is needed to see if it |
|
79 |
# works with non-temporary extensions (without safe mode). |
|
80 |
def set_webextension_uuid(profile, extension_id, uuid=default_extension_uuid): |
|
81 |
""" |
|
82 |
Firefox would normally assign a unique, random UUID to installed extension. |
|
83 |
This UUID is needed to easily navigate to extension's settings page (and |
|
84 |
other extension's pages). Since there's no way to learn such UUID with |
|
85 |
current WebDriver implementation, this function works around this by telling |
|
86 |
Firefox to use a predefined UUID for a certain extension. |
|
87 |
""" |
|
88 |
profile.set_preference('extensions.webextensions.uuids', |
|
89 |
json.dumps({extension_id: uuid})) |
|
90 |
|
|
70 | 91 |
def firefox_safe_mode(firefox_binary=default_firefox_binary, |
71 | 92 |
proxy_host=default_proxy_host, |
72 | 93 |
proxy_port=default_proxy_port): |
... | ... | |
97 | 118 |
profile = webdriver.FirefoxProfile(profile_dir) |
98 | 119 |
set_profile_proxy(profile, proxy_host, proxy_port) |
99 | 120 |
set_profile_console_logging(profile) |
121 |
set_webextension_uuid(profile, default_haketilo_id) |
|
100 | 122 |
|
101 | 123 |
return HaketiloFirefox(firefox_profile=profile, |
102 | 124 |
firefox_binary=firefox_binary) |
Also available in: Unified diff
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.