1
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2
|
|
3
|
"""
|
4
|
Common fixtures for Haketilo unit tests
|
5
|
"""
|
6
|
|
7
|
# This file is part of Haketilo.
|
8
|
#
|
9
|
# Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
|
10
|
#
|
11
|
# This program is free software: you can redistribute it and/or modify
|
12
|
# it under the terms of the GNU General Public License as published by
|
13
|
# the Free Software Foundation, either version 3 of the License, or
|
14
|
# (at your option) any later version.
|
15
|
#
|
16
|
# This program is distributed in the hope that it will be useful,
|
17
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19
|
# GNU General Public License for more details.
|
20
|
#
|
21
|
# You should have received a copy of the GNU General Public License
|
22
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
23
|
#
|
24
|
# I, Wojtek Kosior, thereby promise not to sue for violation of this file's
|
25
|
# license. Although I request that you do not make use of this code in a
|
26
|
# proprietary program, I am not going to enforce this in court.
|
27
|
|
28
|
import pytest
|
29
|
from pathlib import Path
|
30
|
from tempfile import TemporaryDirectory
|
31
|
from selenium.webdriver.common.by import By
|
32
|
from selenium.webdriver.support.ui import WebDriverWait
|
33
|
from selenium.webdriver.support import expected_conditions as EC
|
34
|
|
35
|
from .profiles import firefox_safe_mode
|
36
|
from .server import do_an_internet
|
37
|
from .extension_crafting import make_extension
|
38
|
from .world_wide_library import start_serving_script, dump_scripts
|
39
|
from .misc_constants import proj_root
|
40
|
|
41
|
@pytest.fixture(scope="session")
|
42
|
def proxy():
|
43
|
httpd = do_an_internet()
|
44
|
yield httpd
|
45
|
httpd.shutdown()
|
46
|
|
47
|
@pytest.fixture(scope="session")
|
48
|
def _driver(proxy):
|
49
|
with firefox_safe_mode() as driver:
|
50
|
yield driver
|
51
|
driver.quit()
|
52
|
|
53
|
def close_all_but_one_window(driver):
|
54
|
while len(driver.window_handles) > 1:
|
55
|
driver.switch_to.window(driver.window_handles[-1])
|
56
|
driver.close()
|
57
|
driver.switch_to.window(driver.window_handles[0])
|
58
|
|
59
|
@pytest.fixture()
|
60
|
def driver(_driver, request):
|
61
|
nav_target = request.node.get_closest_marker('get_page')
|
62
|
nav_target = nav_target.args[0] if nav_target else 'about:blank'
|
63
|
|
64
|
second_driver = request.node.get_closest_marker('second_driver')
|
65
|
|
66
|
if second_driver:
|
67
|
with firefox_safe_mode() as _driver:
|
68
|
_driver.get(nav_target)
|
69
|
yield _driver
|
70
|
_driver.quit()
|
71
|
else:
|
72
|
close_all_but_one_window(_driver)
|
73
|
_driver.get(nav_target)
|
74
|
_driver.implicitly_wait(0)
|
75
|
yield _driver
|
76
|
|
77
|
@pytest.fixture()
|
78
|
def webextension(driver, request):
|
79
|
ext_data = request.node.get_closest_marker('ext_data')
|
80
|
if ext_data is None:
|
81
|
raise Exception('"webextension" fixture requires "ext_data" marker to be set')
|
82
|
ext_data = ext_data.args[0].copy()
|
83
|
|
84
|
navigate_to = ext_data.get('navigate_to')
|
85
|
if navigate_to is not None:
|
86
|
del ext_data['navigate_to']
|
87
|
|
88
|
driver.get('https://gotmyowndoma.in/')
|
89
|
ext_path = make_extension(Path(driver.firefox_profile.path), **ext_data)
|
90
|
addon_id = driver.install_addon(str(ext_path), temporary=True)
|
91
|
get_url = lambda d: d.execute_script('return window.ext_page_url;')
|
92
|
ext_page_url = WebDriverWait(driver, 10).until(get_url)
|
93
|
driver.get(ext_page_url)
|
94
|
|
95
|
if navigate_to is not None:
|
96
|
driver.get(driver.current_url.replace('testpage.html', navigate_to))
|
97
|
|
98
|
yield
|
99
|
|
100
|
# Unloading an extension might cause its windows to vanish. Make sure
|
101
|
# there's at least one window navigated to some other page before
|
102
|
# uninstalling the addon. Otherwise, we could be left with a windowless
|
103
|
# browser :c
|
104
|
driver.switch_to.window(driver.window_handles[-1])
|
105
|
driver.get('https://gotmyowndoma.in/')
|
106
|
driver.uninstall_addon(addon_id)
|
107
|
ext_path.unlink()
|
108
|
|
109
|
@pytest.fixture()
|
110
|
def haketilo(driver):
|
111
|
addon_id = driver.install_addon(str(Path.cwd() / 'mozilla-build.zip'),
|
112
|
temporary=True)
|
113
|
|
114
|
yield
|
115
|
|
116
|
driver.uninstall_addon(addon_id)
|
117
|
|
118
|
script_injector_script = '''\
|
119
|
/*
|
120
|
* Selenium by default executes scripts in some weird one-time context. We want
|
121
|
* separately-loaded scripts to be able to access global variables defined
|
122
|
* before, including those declared with `const` or `let`. To achieve that, we
|
123
|
* run our scripts by injecting them into the page with a <script> tag that runs
|
124
|
* javascript served by our proxy. We use custom properties of the `window`
|
125
|
* object to communicate with injected code.
|
126
|
*/
|
127
|
const inject = async () => {
|
128
|
delete window.haketilo_selenium_return_value;
|
129
|
delete window.haketilo_selenium_exception;
|
130
|
window.returnval = val => window.haketilo_selenium_return_value = val;
|
131
|
|
132
|
const injectee = document.createElement('script');
|
133
|
injectee.src = arguments[0];
|
134
|
injectee.type = "application/javascript";
|
135
|
injectee.async = true;
|
136
|
const prom = new Promise(cb => injectee.onload = cb);
|
137
|
|
138
|
window.arguments = arguments[1];
|
139
|
document.body.append(injectee);
|
140
|
|
141
|
await prom;
|
142
|
|
143
|
/*
|
144
|
* To ease debugging, we want this script to signal all exceptions from the
|
145
|
* injectee.
|
146
|
*/
|
147
|
if (window.haketilo_selenium_exception !== false)
|
148
|
throw ['haketilo_selenium_error',
|
149
|
'Error in injected script! Check your geckodriver.log and ./injected_scripts/!'];
|
150
|
|
151
|
return window.haketilo_selenium_return_value;
|
152
|
}
|
153
|
return inject();
|
154
|
'''
|
155
|
|
156
|
def _execute_in_page_context(driver, script, args):
|
157
|
script = script + '\n;\nwindow.haketilo_selenium_exception = false;'
|
158
|
script_url = start_serving_script(script)
|
159
|
|
160
|
try:
|
161
|
result = driver.execute_script(script_injector_script, script_url, args)
|
162
|
if type(result) is list and len(result) == 2 and \
|
163
|
result[0] == 'haketilo_selenium_error':
|
164
|
raise Exception(result[1])
|
165
|
return result
|
166
|
except Exception as e:
|
167
|
dump_scripts()
|
168
|
raise e from None
|
169
|
|
170
|
# Some fixtures here just define functions that operate on driver. We should
|
171
|
# consider making them into webdriver wrapper class methods.
|
172
|
|
173
|
@pytest.fixture()
|
174
|
def execute_in_page(driver):
|
175
|
def do_execute(script, *args):
|
176
|
return _execute_in_page_context(driver, script, args)
|
177
|
|
178
|
yield do_execute
|
179
|
|
180
|
@pytest.fixture()
|
181
|
def wait_elem_text(driver):
|
182
|
def do_wait(id, text):
|
183
|
WebDriverWait(driver, 10).until(
|
184
|
EC.text_to_be_present_in_element((By.ID, id), text)
|
185
|
)
|
186
|
|
187
|
yield do_wait
|