Project

General

Profile

« Previous | Next » 

Revision 3611dd6a

Added by koszko over 1 year ago

facilitate running test environment with Haketilo loaded into browser

View differences:

Makefile.in
1 1
# This file is part of Haketilo
2 2
#
3
# Copyright (C) 2021, jahoti
4
# Copyright (C) 2021, Wojtek Kosior
3
# Copyright (C) 2021 jahoti
4
# Copyright (C) 2021, 2022 Wojtek Kosior
5 5
#
6 6
# This program is free software: you can redistribute it and/or modify
7 7
# it under the terms of the CC0 1.0 Universal License as published by
......
76 76
test-environment: test/certs/rootCA.pem test/certs/site.key
77 77
	python3 -m test
78 78

  
79
test-environment-with-haketilo: test/certs/rootCA.pem test/certs/site.key \
80
		$(default_target)-build.zip
81
	python3 -m test --load-haketilo
82

  
79 83
# helper targets
80 84
clean mostlyclean:
81 85
	rm -rf mozilla-unpacked chromium-unpacked haketilo-$(version)
test/__main__.py
36 36
from .server import do_an_internet
37 37
from .misc_constants import *
38 38
from .profiles import firefox_safe_mode
39
from .extension_crafting import get_extension_base_url
39 40

  
40 41
def fail(msg, error_code):
41 42
    print('Error:', msg)
42
    print('Usage:', sys.argv[0], '[certificates_directory] [proxy_port]')
43
    print('Usage:', sys.argv[0], '[--load-haketilo]', '[certificates_directory] [proxy_port]')
43 44
    sys.exit(error_code)
44 45

  
45
certdir = Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else default_cert_dir
46
load_haketilo = False
47
argv_idx = 1
48
if len(sys.argv) > argv_idx and sys.argv[argv_idx] == '--load-haketilo':
49
    load_haketilo = True
50
    argv_idx += 1
51

  
52
certdir = Path(sys.argv[argv_idx]).resolve() if len(sys.argv) > argv_idx \
53
    else default_cert_dir
54

  
46 55
if not certdir.is_dir():
47 56
    fail('selected certificate directory does not exist.', 2)
48 57

  
49
port = sys.argv[2] if len(sys.argv) > 2 else str(default_proxy_port)
58
argv_idx += 1
59

  
60
port = sys.argv[argv_idx] if len(sys.argv) > argv_idx \
61
    else str(default_proxy_port)
62

  
50 63
if not port.isnumeric():
51 64
    fail('port must be an integer.', 3)
52 65

  
53 66
httpd = do_an_internet(certdir, int(port))
54 67
driver = firefox_safe_mode(proxy_port=int(port))
55 68

  
69
if load_haketilo:
70
    driver.install_addon(str(here.parent / 'mozilla-build.zip'), temporary=True)
71
    driver.get(get_extension_base_url(driver) + 'html/settings.html')
72

  
56 73
print("You can now control the browser through 'driver' object")
57 74

  
58 75
# Here we enable readline-enhanced editing:
test/extension_crafting.py
6 6

  
7 7
# This file is part of Haketilo.
8 8
#
9
# Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
9
# Copyright (C) 2021, 2022 Wojtek Kosior <koszko@koszko.org>
10 10
#
11 11
# This program is free software: you can redistribute it and/or modify
12 12
# it under the terms of the GNU General Public License as published by
......
27 27

  
28 28
import json
29 29
import zipfile
30
import re
31
import shutil
32
import subprocess
33

  
30 34
from pathlib import Path
31 35
from uuid import uuid4
32 36
from tempfile import TemporaryDirectory
33
import shutil
34
import subprocess
37

  
38
from selenium.webdriver.support.ui import WebDriverWait
39
from selenium.common.exceptions import NoSuchElementException
35 40

  
36 41
from .misc_constants import *
37 42

  
......
170 175
            html.add_to_xpi(xpi)
171 176

  
172 177
    return destination_path
178

  
179
extract_base_url_re = re.compile(r'^(.*)manifest.json$')
180

  
181
def get_extension_base_url(driver):
182
    """
183
    Extension's internall UUID is not directly exposed in Selenium. Instead, we
184
    can navigate to about:debugging and inspect the manifest URL present there
185
    to get the base url like:
186
        moz-extension://b225c78f-d108-4caa-8406-f38b37d8dee5/
187
    which can then be used to navigate to extension-bundled pages.
188
    """
189
    # For newer Firefoxes
190
    driver.get('about:debugging#/runtime/this-firefox')
191

  
192
    def get_manifest_link_newer_ff(driver):
193
        try:
194
            return driver.find_element_by_class_name('qa-manifest-url')
195
        except NoSuchElementException:
196
            pass
197

  
198
        try:
199
            details = driver.find_element_by_class_name('error-page-details')
200
        except NoSuchElementException:
201
            return False
202

  
203
        if '#/runtime/this-firefox' in details.text:
204
            return "not_newer_ff"
205

  
206
    manifest_link = WebDriverWait(driver, 10).until(get_manifest_link_newer_ff)
207

  
208
    if manifest_link == "not_newer_ff":
209
        driver.get("about:debugging#addons")
210
        driver.implicitly_wait(10)
211
        manifest_link = driver.find_element_by_class_name('manifest-url')
212
        driver.implicitly_wait(0)
213

  
214
    manifest_url = manifest_link.get_attribute('href')
215
    return extract_base_url_re.match(manifest_url).group(1)
test/test_integration.py
18 18
# CC0 1.0 Universal License for more details.
19 19

  
20 20
import pytest
21
import re
22 21

  
23
from selenium.webdriver.support.ui import WebDriverWait
24
from selenium.common.exceptions import NoSuchElementException
25

  
26
extract_base_url_re = re.compile(r'^(.*)manifest.json$')
27

  
28
def get_extension_base_url(driver):
29
    """
30
    Extension's internall UUID is not directly exposed in Selenium. Instead, we
31
    can navigate to about:debugging and inspect the manifest URL present there
32
    to get the base url like:
33
        moz-extension://b225c78f-d108-4caa-8406-f38b37d8dee5/
34
    which can then be used to navigate to extension-bundled pages.
35
    """
36
    driver.implicitly_wait(10)
37
    try:
38
        # For newer Firefoxes
39
        driver.get('about:debugging#/runtime/this-firefox')
40
        manifest_link = driver.find_element_by_class_name('qa-manifest-url')
41
    except NoSuchElementException:
42
        driver.get("about:debugging#addons")
43
        manifest_link = driver.find_element_by_class_name('manifest-url')
44
    driver.implicitly_wait(0)
45

  
46
    manifest_url = manifest_link.get_attribute('href')
47
    return extract_base_url_re.match(manifest_url).group(1)
22
from .extension_crafting import get_extension_base_url
48 23

  
49 24
@pytest.mark.usefixtures('haketilo')
50 25
def test_integration(driver):

Also available in: Unified diff