From 3611dd6a83bf3782bd61f413ada809b87fa599da Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 15 Feb 2022 15:49:23 +0100 Subject: facilitate running test environment with Haketilo loaded into browser --- Makefile.in | 8 ++++++-- test/__main__.py | 23 +++++++++++++++++++--- test/extension_crafting.py | 49 +++++++++++++++++++++++++++++++++++++++++++--- test/test_integration.py | 27 +------------------------ 4 files changed, 73 insertions(+), 34 deletions(-) diff --git a/Makefile.in b/Makefile.in index 76aaf8e..cac0c01 100644 --- a/Makefile.in +++ b/Makefile.in @@ -1,7 +1,7 @@ # This file is part of Haketilo # -# Copyright (C) 2021, jahoti -# Copyright (C) 2021, Wojtek Kosior +# Copyright (C) 2021 jahoti +# Copyright (C) 2021, 2022 Wojtek Kosior # # This program is free software: you can redistribute it and/or modify # it under the terms of the CC0 1.0 Universal License as published by @@ -76,6 +76,10 @@ test: test/certs/rootCA.pem test/certs/site.key $(default_target)-build.zip test-environment: test/certs/rootCA.pem test/certs/site.key python3 -m test +test-environment-with-haketilo: test/certs/rootCA.pem test/certs/site.key \ + $(default_target)-build.zip + python3 -m test --load-haketilo + # helper targets clean mostlyclean: rm -rf mozilla-unpacked chromium-unpacked haketilo-$(version) diff --git a/test/__main__.py b/test/__main__.py index f8b8976..7afda55 100644 --- a/test/__main__.py +++ b/test/__main__.py @@ -36,23 +36,40 @@ import readline from .server import do_an_internet from .misc_constants import * from .profiles import firefox_safe_mode +from .extension_crafting import get_extension_base_url def fail(msg, error_code): print('Error:', msg) - print('Usage:', sys.argv[0], '[certificates_directory] [proxy_port]') + print('Usage:', sys.argv[0], '[--load-haketilo]', '[certificates_directory] [proxy_port]') sys.exit(error_code) -certdir = Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else default_cert_dir +load_haketilo = False +argv_idx = 1 +if len(sys.argv) > argv_idx and sys.argv[argv_idx] == '--load-haketilo': + load_haketilo = True + argv_idx += 1 + +certdir = Path(sys.argv[argv_idx]).resolve() if len(sys.argv) > argv_idx \ + else default_cert_dir + if not certdir.is_dir(): fail('selected certificate directory does not exist.', 2) -port = sys.argv[2] if len(sys.argv) > 2 else str(default_proxy_port) +argv_idx += 1 + +port = sys.argv[argv_idx] if len(sys.argv) > argv_idx \ + else str(default_proxy_port) + if not port.isnumeric(): fail('port must be an integer.', 3) httpd = do_an_internet(certdir, int(port)) driver = firefox_safe_mode(proxy_port=int(port)) +if load_haketilo: + driver.install_addon(str(here.parent / 'mozilla-build.zip'), temporary=True) + driver.get(get_extension_base_url(driver) + 'html/settings.html') + print("You can now control the browser through 'driver' object") # Here we enable readline-enhanced editing: diff --git a/test/extension_crafting.py b/test/extension_crafting.py index bf54691..680c45e 100644 --- a/test/extension_crafting.py +++ b/test/extension_crafting.py @@ -6,7 +6,7 @@ Making temporary WebExtensions for use in the test suite # This file is part of Haketilo. # -# Copyright (C) 2021 Wojtek Kosior +# Copyright (C) 2021, 2022 Wojtek Kosior # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -27,11 +27,16 @@ Making temporary WebExtensions for use in the test suite import json import zipfile +import re +import shutil +import subprocess + from pathlib import Path from uuid import uuid4 from tempfile import TemporaryDirectory -import shutil -import subprocess + +from selenium.webdriver.support.ui import WebDriverWait +from selenium.common.exceptions import NoSuchElementException from .misc_constants import * @@ -170,3 +175,41 @@ def make_extension(destination_dir, html.add_to_xpi(xpi) return destination_path + +extract_base_url_re = re.compile(r'^(.*)manifest.json$') + +def get_extension_base_url(driver): + """ + Extension's internall UUID is not directly exposed in Selenium. Instead, we + can navigate to about:debugging and inspect the manifest URL present there + to get the base url like: + moz-extension://b225c78f-d108-4caa-8406-f38b37d8dee5/ + which can then be used to navigate to extension-bundled pages. + """ + # For newer Firefoxes + driver.get('about:debugging#/runtime/this-firefox') + + def get_manifest_link_newer_ff(driver): + try: + return driver.find_element_by_class_name('qa-manifest-url') + except NoSuchElementException: + pass + + try: + details = driver.find_element_by_class_name('error-page-details') + except NoSuchElementException: + return False + + if '#/runtime/this-firefox' in details.text: + return "not_newer_ff" + + manifest_link = WebDriverWait(driver, 10).until(get_manifest_link_newer_ff) + + if manifest_link == "not_newer_ff": + driver.get("about:debugging#addons") + driver.implicitly_wait(10) + manifest_link = driver.find_element_by_class_name('manifest-url') + driver.implicitly_wait(0) + + manifest_url = manifest_link.get_attribute('href') + return extract_base_url_re.match(manifest_url).group(1) diff --git a/test/test_integration.py b/test/test_integration.py index 8d8d08d..87d1827 100644 --- a/test/test_integration.py +++ b/test/test_integration.py @@ -18,33 +18,8 @@ Haketilo integration tests # CC0 1.0 Universal License for more details. import pytest -import re -from selenium.webdriver.support.ui import WebDriverWait -from selenium.common.exceptions import NoSuchElementException - -extract_base_url_re = re.compile(r'^(.*)manifest.json$') - -def get_extension_base_url(driver): - """ - Extension's internall UUID is not directly exposed in Selenium. Instead, we - can navigate to about:debugging and inspect the manifest URL present there - to get the base url like: - moz-extension://b225c78f-d108-4caa-8406-f38b37d8dee5/ - which can then be used to navigate to extension-bundled pages. - """ - driver.implicitly_wait(10) - try: - # For newer Firefoxes - driver.get('about:debugging#/runtime/this-firefox') - manifest_link = driver.find_element_by_class_name('qa-manifest-url') - except NoSuchElementException: - driver.get("about:debugging#addons") - manifest_link = driver.find_element_by_class_name('manifest-url') - driver.implicitly_wait(0) - - manifest_url = manifest_link.get_attribute('href') - return extract_base_url_re.match(manifest_url).group(1) +from .extension_crafting import get_extension_base_url @pytest.mark.usefixtures('haketilo') def test_integration(driver): -- cgit v1.2.3