Path: blob/trunk/py/test/selenium/webdriver/common/bidi_webextension_tests.py
1865 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 pytest23from python.runfiles import Runfiles2425from selenium import webdriver26from selenium.webdriver.common.by import By27from selenium.webdriver.support.wait import WebDriverWait2829EXTENSION_ID = "[email protected]"30EXTENSION_PATH = "webextensions-selenium-example-signed"31EXTENSION_ARCHIVE_PATH = "webextensions-selenium-example.xpi"3233# Use bazel Runfiles to locate the test extension directory34r = Runfiles.Create()35extensions = r.Rlocation("selenium/py/test/extensions")363738def install_extension(driver, **kwargs):39result = driver.webextension.install(**kwargs)40assert result.get("extension") == EXTENSION_ID41return result424344def verify_extension_injection(driver, pages):45pages.load("blank.html")46injected = WebDriverWait(driver, timeout=2).until(47lambda dr: dr.find_element(By.ID, "webextensions-selenium-example")48)49assert injected.text == "Content injected by webextensions-selenium-example"505152def uninstall_extension_and_verify_extension_uninstalled(driver, extension_info):53driver.webextension.uninstall(extension_info)5455context_id = driver.current_window_handle56driver.browsing_context.reload(context_id)57assert len(driver.find_elements(By.ID, "webextensions-selenium-example")) == 0585960def test_webextension_initialized(driver):61"""Test that the webextension module is initialized properly."""62assert driver.webextension is not None636465@pytest.mark.xfail_chrome66@pytest.mark.xfail_edge67class TestFirefoxWebExtension:68"""Firefox-specific WebExtension tests."""6970def test_install_extension_path(self, driver, pages):71"""Test installing an extension from a directory path."""7273path = os.path.join(extensions, EXTENSION_PATH)74ext_info = install_extension(driver, path=path)75verify_extension_injection(driver, pages)76uninstall_extension_and_verify_extension_uninstalled(driver, ext_info)7778def test_install_archive_extension_path(self, driver, pages):79"""Test installing an extension from an archive path."""8081path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH)82ext_info = install_extension(driver, archive_path=path)83verify_extension_injection(driver, pages)84uninstall_extension_and_verify_extension_uninstalled(driver, ext_info)8586def test_install_base64_extension_path(self, driver, pages):87"""Test installing an extension from a base64 encoded string."""8889path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH)90with open(path, "rb") as file:91base64_encoded = base64.b64encode(file.read()).decode("utf-8")92ext_info = install_extension(driver, base64_value=base64_encoded)93# TODO: the extension is installed but the script is not injected, check and fix94# verify_extension_injection(driver, pages)95uninstall_extension_and_verify_extension_uninstalled(driver, ext_info)9697def test_install_unsigned_extension(self, driver, pages):98"""Test installing an unsigned extension."""99100path = os.path.join(extensions, "webextensions-selenium-example")101ext_info = install_extension(driver, path=path)102verify_extension_injection(driver, pages)103uninstall_extension_and_verify_extension_uninstalled(driver, ext_info)104105def test_install_with_extension_id_uninstall(self, driver, pages):106"""Test uninstalling an extension using just the extension ID."""107108path = os.path.join(extensions, EXTENSION_PATH)109ext_info = install_extension(driver, path=path)110extension_id = ext_info.get("extension")111# Uninstall using the extension ID112uninstall_extension_and_verify_extension_uninstalled(driver, extension_id)113114115@pytest.mark.xfail_firefox116class TestChromiumWebExtension:117"""Chrome/Edge-specific WebExtension tests with custom driver."""118119@pytest.fixture120def pages_chromium(self, webserver, chromium_driver):121class Pages:122def load(self, name):123chromium_driver.get(webserver.where_is(name, localhost=False))124125return Pages()126127@pytest.fixture128def chromium_driver(self, chromium_options, request):129"""Create a Chrome/Edge driver with webextension support enabled."""130driver_option = request.config.option.drivers[0].lower()131132if driver_option == "chrome":133browser_class = webdriver.Chrome134browser_service = webdriver.ChromeService135elif driver_option == "edge":136browser_class = webdriver.Edge137browser_service = webdriver.EdgeService138139temp_dir = tempfile.mkdtemp(prefix="chromium-profile-")140141chromium_options.enable_bidi = True142chromium_options.enable_webextensions = True143chromium_options.add_argument(f"--user-data-dir={temp_dir}")144chromium_options.add_argument("--no-sandbox")145chromium_options.add_argument("--disable-dev-shm-usage")146147binary = request.config.option.binary148if binary:149chromium_options.binary_location = binary150151executable = request.config.option.executable152if executable:153service = browser_service(executable_path=executable)154else:155service = browser_service()156157chromium_driver = browser_class(options=chromium_options, service=service)158159yield chromium_driver160chromium_driver.quit()161162# delete the temp directory163if os.path.exists(temp_dir):164shutil.rmtree(temp_dir)165166def test_install_extension_path(self, chromium_driver, pages_chromium):167"""Test installing an extension from a directory path."""168path = os.path.join(extensions, EXTENSION_PATH)169ext_info = chromium_driver.webextension.install(path=path)170171verify_extension_injection(chromium_driver, pages_chromium)172uninstall_extension_and_verify_extension_uninstalled(chromium_driver, ext_info)173174def test_install_unsigned_extension(self, chromium_driver, pages_chromium):175"""Test installing an unsigned extension."""176path = os.path.join(extensions, "webextensions-selenium-example")177ext_info = chromium_driver.webextension.install(path=path)178179verify_extension_injection(chromium_driver, pages_chromium)180uninstall_extension_and_verify_extension_uninstalled(chromium_driver, ext_info)181182def test_install_with_extension_id_uninstall(self, chromium_driver):183"""Test uninstalling an extension using just the extension ID."""184path = os.path.join(extensions, EXTENSION_PATH)185ext_info = chromium_driver.webextension.install(path=path)186extension_id = ext_info.get("extension")187# Uninstall using the extension ID188uninstall_extension_and_verify_extension_uninstalled(chromium_driver, extension_id)189190191