aboutsummaryrefslogtreecommitdiff
path: root/test/unit
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2021-12-13 17:58:29 +0100
committerWojtek Kosior <koszko@koszko.org>2021-12-13 17:58:29 +0100
commitc699b6409e98fe64a70417a18b6e335b4c60f86d (patch)
treed01108437eaaa89fb167b35a7e4439e69b5d38d2 /test/unit
parent4c933941e64ba2b4b5da3c19e5ed5924a30e0752 (diff)
downloadbrowser-extension-c699b6409e98fe64a70417a18b6e335b4c60f86d.tar.gz
browser-extension-c699b6409e98fe64a70417a18b6e335b4c60f86d.zip
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.
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/conftest.py31
-rw-r--r--test/unit/test_basic.py17
2 files changed, 41 insertions, 7 deletions
diff --git a/test/unit/conftest.py b/test/unit/conftest.py
index 1500006..e1c98a1 100644
--- a/test/unit/conftest.py
+++ b/test/unit/conftest.py
@@ -26,10 +26,14 @@ Common fixtures for Haketilo unit tests
# proprietary program, I am not going to enforce this in court.
import pytest
+from pathlib import Path
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.webdriver.support import expected_conditions as EC
-from ..profiles import firefox_safe_mode
-from ..server import do_an_internet
-from ..script_loader import load_script
+from ..profiles import firefox_safe_mode
+from ..server import do_an_internet
+from ..script_loader import load_script
+from ..extension_crafting import make_extension
@pytest.fixture(scope="package")
def proxy():
@@ -43,6 +47,23 @@ def driver(proxy):
yield driver
driver.quit()
+@pytest.fixture()
+def webextension(driver, request):
+ ext_data = request.node.get_closest_marker('ext_data')
+ if ext_data is None:
+ raise Exception('"webextension" fixture requires "ext_data" marker to be set')
+
+ ext_path = make_extension(Path(driver.firefox_profile.path),
+ **ext_data.args[0])
+ driver.get('https://gotmyowndoma.in/')
+ addon_id = driver.install_addon(str(ext_path), temporary=True)
+ WebDriverWait(driver, 10).until(
+ EC.title_contains("Extension's options page for testing")
+ )
+ yield
+ driver.uninstall_addon(addon_id)
+ ext_path.unlink()
+
script_injecting_script = '''\
/*
* Selenium by default executes scripts in some weird one-time context. We want
@@ -63,8 +84,8 @@ window.arguments = arguments[1];
document.body.append(script_elem);
/*
- * To ease debugging, we want this script to forward signal all exceptions from
- * the injectee.
+ * To ease debugging, we want this script to signal all exceptions from the
+ * injectee.
*/
try {
if (window.haketilo_selenium_exception !== false)
diff --git a/test/unit/test_basic.py b/test/unit/test_basic.py
index cbe5c8c..3b09cb6 100644
--- a/test/unit/test_basic.py
+++ b/test/unit/test_basic.py
@@ -26,8 +26,9 @@ def test_driver(driver):
"""
for proto in ['http://', 'https://']:
driver.get(proto + 'gotmyowndoma.in')
- element = driver.find_element_by_tag_name('title')
- title = driver.execute_script('return arguments[0].innerText;', element)
+ title = driver.execute_script(
+ 'return document.getElementsByTagName("title")[0].innerText;'
+ )
assert "Schrodinger's Document" in title
def test_script_loader(execute_in_page, load_into_page):
@@ -39,3 +40,15 @@ def test_script_loader(execute_in_page, load_into_page):
page='https://gotmyowndoma.in')
assert execute_in_page('returnval(TYPE_PREFIX.VAR);') == '_'
+
+@pytest.mark.ext_data({})
+def test_webextension(driver, webextension):
+ """
+ A trivial test case that verifies a test WebExtension created and installed
+ by the `webextension` fixture works and redirects specially-constructed URLs
+ to its test page.
+ """
+ heading = driver.execute_script(
+ 'return document.getElementsByTagName("h1")[0].innerText;'
+ )
+ assert "Extension's options page for testing" in heading