Revision 463e6830
Added by koszko almost 2 years ago
CHROMIUM_exports_init.js | ||
---|---|---|
1 | 1 |
// SPDX-License-Identifier: CC0-1.0 |
2 | 2 |
|
3 |
window.killtheweb={is_chrome: true, browser: window.chrome}; |
|
3 |
window.haketilo_exports = {is_chrome: true, browser: window.chrome}; |
MOZILLA_exports_init.js | ||
---|---|---|
54 | 54 |
} |
55 | 55 |
} |
56 | 56 |
|
57 |
window.killtheweb={is_mozilla: true, browser: this.browser}; |
|
57 |
window.haketilo_exports = {is_mozilla: true, browser: this.browser}; |
background/main.js | ||
---|---|---|
186 | 186 |
const code = `\ |
187 | 187 |
console.warn("Hi, I'm Mr Dynamic!"); |
188 | 188 |
|
189 |
console.debug("let's see how window.killtheweb looks like now");
|
|
189 |
console.debug("let's see how window.haketilo_exports looks like now");
|
|
190 | 190 |
|
191 |
console.log("killtheweb", window.killtheweb);
|
|
191 |
console.log("haketilo_exports", window.haketilo_exports);
|
|
192 | 192 |
` |
193 | 193 |
|
194 | 194 |
async function test_dynamic_content_scripts() |
compute_scripts.awk | ||
---|---|---|
92 | 92 |
count = import_counts[filename] |
93 | 93 |
for (i = 1; i <= count; i++) { |
94 | 94 |
import_name = imports[filename,i] |
95 |
printf "const %s = window.killtheweb.%s;\n", import_name, import_name |
|
95 |
printf "const %s = window.haketilo_exports.%s;\n", |
|
96 |
import_name, import_name |
|
96 | 97 |
} |
97 | 98 |
} |
98 | 99 |
|
... | ... | |
100 | 101 |
count = export_counts[filename] |
101 | 102 |
for (i = 1; i <= count; i++) { |
102 | 103 |
export_name = exports[filename,i] |
103 |
printf "window.killtheweb.%s = %s;\n", export_name, export_name
|
|
104 |
printf "window.haketilo_exports.%s = %s;\n", export_name, export_name
|
|
104 | 105 |
} |
105 | 106 |
} |
106 | 107 |
|
copyright | ||
---|---|---|
79 | 79 |
Copyright: 2021 Wojtek Kosior <koszko@koszko.org> |
80 | 80 |
License: CC0 |
81 | 81 |
|
82 |
Files: test/profiles.py |
|
82 |
Files: test/profiles.py test/script_loader.py
|
|
83 | 83 |
Copyright: 2021 Wojtek Kosior <koszko@koszko.org> |
84 | 84 |
License: GPL-3+ |
85 | 85 |
Comment: Wojtek Kosior promises not to sue even in case of violations |
test/script_loader.py | ||
---|---|---|
1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
|
2 |
|
|
3 |
""" |
|
4 |
Loading of parts of Haketilo source for testing in browser |
|
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 this code in a |
|
26 |
# proprietary program, I am not going to enforce this in court. |
|
27 |
|
|
28 |
from pathlib import Path |
|
29 |
import subprocess, re |
|
30 |
|
|
31 |
from .misc_constants import * |
|
32 |
|
|
33 |
script_root = here.parent |
|
34 |
awk_script = script_root / 'compute_scripts.awk' |
|
35 |
|
|
36 |
def make_relative_path(path): |
|
37 |
path = Path(path) |
|
38 |
|
|
39 |
if path.is_absolute(): |
|
40 |
path = path.relative_to(script_root) |
|
41 |
|
|
42 |
return path |
|
43 |
|
|
44 |
"""Used to ignore hidden files and emacs auto-save files.""" |
|
45 |
script_name_regex = re.compile(r'^[^.#].*\.js$') |
|
46 |
|
|
47 |
def available_scripts(directory): |
|
48 |
for script in directory.rglob('*.js'): |
|
49 |
if script_name_regex.match(script.name): |
|
50 |
yield script |
|
51 |
|
|
52 |
def get_wrapped_script(script_path): |
|
53 |
if script_path == 'exports_init.js': |
|
54 |
with open(script_root / 'MOZILLA_exports_init.js') as script: |
|
55 |
return script.read() |
|
56 |
|
|
57 |
awk = subprocess.run(['awk', '-f', str(awk_script), 'wrapped_code', |
|
58 |
str(script_path)], |
|
59 |
stdout=subprocess.PIPE, cwd=script_root, check=True) |
|
60 |
|
|
61 |
return awk.stdout.decode() |
|
62 |
|
|
63 |
def load_script(path, import_dirs): |
|
64 |
""" |
|
65 |
`path` and `import_dirs` are .js file path and a list of directory paths, |
|
66 |
respectively. They may be absolute or specified relative to Haketilo's |
|
67 |
project directory. |
|
68 |
|
|
69 |
Return a string containing script from `path` together with all other |
|
70 |
scripts it depends on, wrapped in the same way Haketilo's build system wraps |
|
71 |
them, with imports properly satisfied. |
|
72 |
""" |
|
73 |
path = make_relative_path(path) |
|
74 |
|
|
75 |
import_dirs = [make_relative_path(dir) for dir in import_dirs] |
|
76 |
available = [s for dir in import_dirs for s in available_scripts(dir)] |
|
77 |
|
|
78 |
awk = subprocess.run(['awk', '-f', str(awk_script), 'script_dependencies', |
|
79 |
str(path), *[str(s) for s in available]], |
|
80 |
stdout=subprocess.PIPE, cwd=script_root, check=True) |
|
81 |
|
|
82 |
output = awk.stdout.decode() |
|
83 |
|
|
84 |
return '\n'.join([get_wrapped_script(path) for path in output.split()]) |
test/test_unit.py | ||
---|---|---|
19 | 19 |
# CC0 1.0 Universal License for more details. |
20 | 20 |
|
21 | 21 |
import pytest |
22 |
from .profiles import firefox_safe_mode |
|
23 |
from .server import do_an_internet |
|
22 |
from .profiles import firefox_safe_mode |
|
23 |
from .server import do_an_internet |
|
24 |
from .script_loader import load_script |
|
24 | 25 |
|
25 |
@pytest.fixture |
|
26 |
@pytest.fixture(scope="module")
|
|
26 | 27 |
def proxy(): |
27 | 28 |
httpd = do_an_internet() |
28 | 29 |
yield httpd |
29 | 30 |
httpd.shutdown() |
30 | 31 |
|
31 |
@pytest.fixture |
|
32 |
@pytest.fixture(scope="module")
|
|
32 | 33 |
def driver(proxy): |
33 | 34 |
with firefox_safe_mode() as driver: |
34 | 35 |
yield driver |
35 | 36 |
driver.quit() |
36 | 37 |
|
37 |
def test_basic(driver): |
|
38 |
driver.get('https://gotmyowndoma.in') |
|
39 |
element = driver.find_element_by_tag_name('title') |
|
40 |
title = driver.execute_script('return arguments[0].innerText;', element) |
|
41 |
assert "Schrodinger's Document" in title |
|
38 |
def test_proxy(driver): |
|
39 |
""" |
|
40 |
A trivial test case that verifies mocked web pages served by proxy can be |
|
41 |
accessed by the browser driven. |
|
42 |
""" |
|
43 |
for proto in ['http://', 'https://']: |
|
44 |
driver.get(proto + 'gotmyowndoma.in') |
|
45 |
element = driver.find_element_by_tag_name('title') |
|
46 |
title = driver.execute_script('return arguments[0].innerText;', element) |
|
47 |
assert "Schrodinger's Document" in title |
|
48 |
|
|
49 |
def test_script_loader(driver): |
|
50 |
""" |
|
51 |
A trivial test case that verifies Haketilo's .js files can be properly |
|
52 |
loaded into a test page together with their dependencies. |
|
53 |
""" |
|
54 |
driver.get('http://gotmyowndoma.in') |
|
55 |
driver.execute_script(load_script('common/stored_types.js', ['common'])) |
|
56 |
get_var_prefix = 'return window.haketilo_exports.TYPE_PREFIX.VAR;' |
|
57 |
assert driver.execute_script(get_var_prefix) == '_' |
Also available in: Unified diff
facilitate testing javascript functions
Haketilo's .js files can now be loaded together with their dependencies and
executed on a page opened in a selenium-driven Firefox instance.