Path: blob/trunk/py/test/selenium/webdriver/common/bidi_webextension_tests.py
4174 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import base6418import os19import shutil20import tempfile2122import pytest2324from selenium import webdriver25from selenium.webdriver.common.by import By26from selenium.webdriver.support.wait import WebDriverWait2728from conftest import get_extensions_location2930EXTENSIONS = get_extensions_location()31EXTENSION_ID = "[email protected]"32EXTENSION_PATH = "webextensions-selenium-example-signed"33EXTENSION_ARCHIVE_PATH = "webextensions-selenium-example.xpi"343536def install_extension(driver, **kwargs):37result = driver.webextension.install(**kwargs)38assert result.get("extension") == EXTENSION_ID39return result404142def verify_extension_injection(driver, pages):43pages.load("blank.html")44injected = WebDriverWait(driver, timeout=2).until(45lambda dr: dr.find_element(By.ID, "webextensions-selenium-example")46)47assert injected.text == "Content injected by webextensions-selenium-example"484950def uninstall_extension_and_verify_extension_uninstalled(driver, extension_info):51driver.webextension.uninstall(extension_info)5253context_id = driver.current_window_handle54driver.browsing_context.reload(context_id)55assert len(driver.find_elements(By.ID, "webextensions-selenium-example")) == 0565758def test_webextension_initialized(driver):59"""Test that the webextension module is initialized properly."""60assert driver.webextension is not None616263@pytest.mark.xfail_chrome64@pytest.mark.xfail_edge65class TestFirefoxWebExtension:66"""Firefox-specific WebExtension tests."""6768def test_install_extension_path(self, driver, pages):69"""Test installing an extension from a directory path."""70path = os.path.join(EXTENSIONS, EXTENSION_PATH)71ext_info = install_extension(driver, path=path)72verify_extension_injection(driver, pages)73uninstall_extension_and_verify_extension_uninstalled(driver, ext_info)7475def test_install_archive_extension_path(self, driver, pages):76"""Test installing an extension from an archive path."""77path = os.path.join(EXTENSIONS, EXTENSION_ARCHIVE_PATH)78ext_info = install_extension(driver, archive_path=path)79verify_extension_injection(driver, pages)80uninstall_extension_and_verify_extension_uninstalled(driver, ext_info)8182def test_install_base64_extension_path(self, driver, pages):83"""Test installing an extension from a base64 encoded string."""84path = os.path.join(EXTENSIONS, EXTENSION_ARCHIVE_PATH)85with open(path, "rb") as file:86base64_encoded = base64.b64encode(file.read()).decode("utf-8")87ext_info = install_extension(driver, base64_value=base64_encoded)88# TODO: the extension is installed but the script is not injected, check and fix89# verify_extension_injection(driver, pages)90uninstall_extension_and_verify_extension_uninstalled(driver, ext_info)9192def test_install_unsigned_extension(self, driver, pages):93"""Test installing an unsigned extension."""94path = os.path.join(EXTENSIONS, "webextensions-selenium-example")95ext_info = install_extension(driver, path=path)96verify_extension_injection(driver, pages)97uninstall_extension_and_verify_extension_uninstalled(driver, ext_info)9899def test_install_with_extension_id_uninstall(self, driver, pages):100"""Test uninstalling an extension using just the extension ID."""101path = os.path.join(EXTENSIONS, EXTENSION_PATH)102ext_info = install_extension(driver, path=path)103extension_id = ext_info.get("extension")104# Uninstall using the extension ID105uninstall_extension_and_verify_extension_uninstalled(driver, extension_id)106107108@pytest.mark.xfail_firefox109class TestChromiumWebExtension:110"""Chrome/Edge-specific WebExtension tests with custom driver."""111112@pytest.fixture113def pages_chromium(self, webserver, chromium_driver):114class Pages:115def load(self, name):116chromium_driver.get(webserver.where_is(name, localhost=False))117118return Pages()119120@pytest.fixture121def chromium_driver(self, chromium_options, request):122"""Create a Chrome/Edge driver with webextension support enabled."""123driver_option = request.config.option.drivers[0].lower()124125if driver_option == "chrome":126browser_class = webdriver.Chrome127browser_service = webdriver.ChromeService128elif driver_option == "edge":129browser_class = webdriver.Edge130browser_service = webdriver.EdgeService131132temp_dir = tempfile.mkdtemp(prefix="chromium-profile-")133134chromium_options.enable_bidi = True135chromium_options.enable_webextensions = True136chromium_options.add_argument(f"--user-data-dir={temp_dir}")137chromium_options.add_argument("--no-sandbox")138chromium_options.add_argument("--disable-dev-shm-usage")139140binary = request.config.option.binary141if binary:142chromium_options.binary_location = binary143144executable = request.config.option.executable145if executable:146service = browser_service(executable_path=executable)147else:148service = browser_service()149150chromium_driver = browser_class(options=chromium_options, service=service)151152yield chromium_driver153chromium_driver.quit()154155# delete the temp directory156if os.path.exists(temp_dir):157shutil.rmtree(temp_dir)158159def test_install_extension_path(self, chromium_driver, pages_chromium):160"""Test installing an extension from a directory path."""161path = os.path.join(EXTENSIONS, EXTENSION_PATH)162ext_info = chromium_driver.webextension.install(path=path)163164verify_extension_injection(chromium_driver, pages_chromium)165uninstall_extension_and_verify_extension_uninstalled(chromium_driver, ext_info)166167def test_install_unsigned_extension(self, chromium_driver, pages_chromium):168"""Test installing an unsigned extension."""169path = os.path.join(EXTENSIONS, "webextensions-selenium-example")170ext_info = chromium_driver.webextension.install(path=path)171172verify_extension_injection(chromium_driver, pages_chromium)173uninstall_extension_and_verify_extension_uninstalled(chromium_driver, ext_info)174175def test_install_with_extension_id_uninstall(self, chromium_driver):176"""Test uninstalling an extension using just the extension ID."""177path = os.path.join(EXTENSIONS, EXTENSION_PATH)178ext_info = chromium_driver.webextension.install(path=path)179extension_id = ext_info.get("extension")180# Uninstall using the extension ID181uninstall_extension_and_verify_extension_uninstalled(chromium_driver, extension_id)182183184