# SPDX-License-Identifier: GPL-3.0-or-later """ Common fixtures for Haketilo unit tests """ # This file is part of Haketilo. # # Copyright (C) 2021 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 # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # I, Wojtek Kosior, thereby promise not to sue for violation of this file's # license. Although I request that you do not make use this code in a # 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 ..extension_crafting import make_extension @pytest.fixture(scope="package") def proxy(): httpd = do_an_internet() yield httpd httpd.shutdown() @pytest.fixture(scope="package") def driver(proxy): with firefox_safe_mode() as driver: 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 * separately-loaded scripts to be able to access global variables defined * before, including those declared with `const` or `let`. To achieve that, we * run our scripts by injecting them into the page inside a