|  | 
| 17 | 17 | 
 | 
| 18 | 18 | import base64 | 
| 19 | 19 | import os | 
|  | 20 | +import shutil | 
|  | 21 | +import tempfile | 
| 20 | 22 | 
 | 
| 21 | 23 | import pytest | 
| 22 | 24 | from python.runfiles import Runfiles | 
| 23 | 25 | 
 | 
|  | 26 | +from selenium import webdriver | 
| 24 | 27 | from selenium.webdriver.common.by import By | 
| 25 | 28 | from selenium.webdriver.support.wait import WebDriverWait | 
| 26 | 29 | 
 | 
| @@ -62,62 +65,113 @@ def test_webextension_initialized(driver): | 
| 62 | 65 | 
 | 
| 63 | 66 | @pytest.mark.xfail_chrome | 
| 64 | 67 | @pytest.mark.xfail_edge | 
| 65 |  | -def test_install_extension_path(driver, pages): | 
| 66 |  | -    """Test installing an extension from a directory path.""" | 
| 67 |  | -    path = os.path.join(extensions, EXTENSION_PATH) | 
|  | 68 | +class TestFirefoxWebExtension: | 
|  | 69 | +    """Firefox-specific WebExtension tests.""" | 
| 68 | 70 | 
 | 
| 69 |  | -    ext_info = install_extension(driver, path=path) | 
| 70 |  | -    verify_extension_injection(driver, pages) | 
| 71 |  | -    uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | 
|  | 71 | +    def test_install_extension_path(self, driver, pages): | 
|  | 72 | +        """Test installing an extension from a directory path.""" | 
| 72 | 73 | 
 | 
| 73 |  | - | 
| 74 |  | -@pytest.mark.xfail_chrome | 
| 75 |  | -@pytest.mark.xfail_edge | 
| 76 |  | -def test_install_archive_extension_path(driver, pages): | 
| 77 |  | -    """Test installing an extension from an archive path.""" | 
| 78 |  | -    path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH) | 
| 79 |  | - | 
| 80 |  | -    ext_info = install_extension(driver, archive_path=path) | 
| 81 |  | -    verify_extension_injection(driver, pages) | 
| 82 |  | -    uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | 
| 83 |  | - | 
| 84 |  | - | 
| 85 |  | -@pytest.mark.xfail_chrome | 
| 86 |  | -@pytest.mark.xfail_edge | 
| 87 |  | -def test_install_base64_extension_path(driver, pages): | 
| 88 |  | -    """Test installing an extension from a base64 encoded string.""" | 
| 89 |  | -    path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH) | 
| 90 |  | - | 
| 91 |  | -    with open(path, "rb") as file: | 
| 92 |  | -        base64_encoded = base64.b64encode(file.read()).decode("utf-8") | 
| 93 |  | - | 
| 94 |  | -    ext_info = install_extension(driver, base64_value=base64_encoded) | 
| 95 |  | - | 
| 96 |  | -    # TODO: the extension is installed but the script is not injected, check and fix | 
| 97 |  | -    # verify_extension_injection(driver, pages) | 
| 98 |  | - | 
| 99 |  | -    uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | 
| 100 |  | - | 
| 101 |  | - | 
| 102 |  | -@pytest.mark.xfail_chrome | 
| 103 |  | -@pytest.mark.xfail_edge | 
| 104 |  | -def test_install_unsigned_extension(driver, pages): | 
| 105 |  | -    """Test installing an unsigned extension.""" | 
| 106 |  | -    path = os.path.join(extensions, "webextensions-selenium-example") | 
| 107 |  | - | 
| 108 |  | -    ext_info = install_extension(driver, path=path) | 
| 109 |  | -    verify_extension_injection(driver, pages) | 
| 110 |  | -    uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | 
| 111 |  | - | 
| 112 |  | - | 
| 113 |  | -@pytest.mark.xfail_chrome | 
| 114 |  | -@pytest.mark.xfail_edge | 
| 115 |  | -def test_install_with_extension_id_uninstall(driver, pages): | 
| 116 |  | -    """Test uninstalling an extension using just the extension ID.""" | 
| 117 |  | -    path = os.path.join(extensions, EXTENSION_PATH) | 
| 118 |  | - | 
| 119 |  | -    ext_info = install_extension(driver, path=path) | 
| 120 |  | -    extension_id = ext_info.get("extension") | 
| 121 |  | - | 
| 122 |  | -    # Uninstall using the extension ID | 
| 123 |  | -    uninstall_extension_and_verify_extension_uninstalled(driver, extension_id) | 
|  | 74 | +        path = os.path.join(extensions, EXTENSION_PATH) | 
|  | 75 | +        ext_info = install_extension(driver, path=path) | 
|  | 76 | +        verify_extension_injection(driver, pages) | 
|  | 77 | +        uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | 
|  | 78 | + | 
|  | 79 | +    def test_install_archive_extension_path(self, driver, pages): | 
|  | 80 | +        """Test installing an extension from an archive path.""" | 
|  | 81 | + | 
|  | 82 | +        path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH) | 
|  | 83 | +        ext_info = install_extension(driver, archive_path=path) | 
|  | 84 | +        verify_extension_injection(driver, pages) | 
|  | 85 | +        uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | 
|  | 86 | + | 
|  | 87 | +    def test_install_base64_extension_path(self, driver, pages): | 
|  | 88 | +        """Test installing an extension from a base64 encoded string.""" | 
|  | 89 | + | 
|  | 90 | +        path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH) | 
|  | 91 | +        with open(path, "rb") as file: | 
|  | 92 | +            base64_encoded = base64.b64encode(file.read()).decode("utf-8") | 
|  | 93 | +        ext_info = install_extension(driver, base64_value=base64_encoded) | 
|  | 94 | +        # TODO: the extension is installed but the script is not injected, check and fix | 
|  | 95 | +        # verify_extension_injection(driver, pages) | 
|  | 96 | +        uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | 
|  | 97 | + | 
|  | 98 | +    def test_install_unsigned_extension(self, driver, pages): | 
|  | 99 | +        """Test installing an unsigned extension.""" | 
|  | 100 | + | 
|  | 101 | +        path = os.path.join(extensions, "webextensions-selenium-example") | 
|  | 102 | +        ext_info = install_extension(driver, path=path) | 
|  | 103 | +        verify_extension_injection(driver, pages) | 
|  | 104 | +        uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | 
|  | 105 | + | 
|  | 106 | +    def test_install_with_extension_id_uninstall(self, driver, pages): | 
|  | 107 | +        """Test uninstalling an extension using just the extension ID.""" | 
|  | 108 | + | 
|  | 109 | +        path = os.path.join(extensions, EXTENSION_PATH) | 
|  | 110 | +        ext_info = install_extension(driver, path=path) | 
|  | 111 | +        extension_id = ext_info.get("extension") | 
|  | 112 | +        # Uninstall using the extension ID | 
|  | 113 | +        uninstall_extension_and_verify_extension_uninstalled(driver, extension_id) | 
|  | 114 | + | 
|  | 115 | + | 
|  | 116 | +@pytest.mark.xfail_firefox | 
|  | 117 | +class TestChromiumWebExtension: | 
|  | 118 | +    """Chrome/Edge-specific WebExtension tests with custom driver.""" | 
|  | 119 | + | 
|  | 120 | +    @pytest.fixture | 
|  | 121 | +    def pages_chromium(self, webserver, chromium_driver): | 
|  | 122 | +        class Pages: | 
|  | 123 | +            def load(self, name): | 
|  | 124 | +                chromium_driver.get(webserver.where_is(name, localhost=False)) | 
|  | 125 | + | 
|  | 126 | +        return Pages() | 
|  | 127 | + | 
|  | 128 | +    @pytest.fixture | 
|  | 129 | +    def chromium_driver(self, chromium_options, request): | 
|  | 130 | +        """Create a Chrome/Edge driver with webextension support enabled.""" | 
|  | 131 | +        driver_option = request.config.option.drivers[0].lower() | 
|  | 132 | + | 
|  | 133 | +        if driver_option == "chrome": | 
|  | 134 | +            browser_class = webdriver.Chrome | 
|  | 135 | +        elif driver_option == "edge": | 
|  | 136 | +            browser_class = webdriver.Edge | 
|  | 137 | + | 
|  | 138 | +        temp_dir = tempfile.mkdtemp(prefix="chrome-profile-") | 
|  | 139 | + | 
|  | 140 | +        chromium_options.enable_bidi = True | 
|  | 141 | +        chromium_options.enable_webextensions = True | 
|  | 142 | +        chromium_options.add_argument(f"--user-data-dir={temp_dir}") | 
|  | 143 | +        chromium_options.add_argument("--no-sandbox") | 
|  | 144 | +        chromium_options.add_argument("--disable-dev-shm-usage") | 
|  | 145 | + | 
|  | 146 | +        chromium_driver = browser_class(options=chromium_options) | 
|  | 147 | + | 
|  | 148 | +        yield chromium_driver | 
|  | 149 | +        chromium_driver.quit() | 
|  | 150 | + | 
|  | 151 | +        # delete the temp directory | 
|  | 152 | +        if os.path.exists(temp_dir): | 
|  | 153 | +            shutil.rmtree(temp_dir) | 
|  | 154 | + | 
|  | 155 | +    def test_install_extension_path(self, chromium_driver, pages_chromium): | 
|  | 156 | +        """Test installing an extension from a directory path.""" | 
|  | 157 | +        path = os.path.join(extensions, EXTENSION_PATH) | 
|  | 158 | +        ext_info = chromium_driver.webextension.install(path=path) | 
|  | 159 | + | 
|  | 160 | +        verify_extension_injection(chromium_driver, pages_chromium) | 
|  | 161 | +        uninstall_extension_and_verify_extension_uninstalled(chromium_driver, ext_info) | 
|  | 162 | + | 
|  | 163 | +    def test_install_unsigned_extension(self, chromium_driver, pages_chromium): | 
|  | 164 | +        """Test installing an unsigned extension.""" | 
|  | 165 | +        path = os.path.join(extensions, "webextensions-selenium-example") | 
|  | 166 | +        ext_info = chromium_driver.webextension.install(path=path) | 
|  | 167 | + | 
|  | 168 | +        verify_extension_injection(chromium_driver, pages_chromium) | 
|  | 169 | +        uninstall_extension_and_verify_extension_uninstalled(chromium_driver, ext_info) | 
|  | 170 | + | 
|  | 171 | +    def test_install_with_extension_id_uninstall(self, chromium_driver): | 
|  | 172 | +        """Test uninstalling an extension using just the extension ID.""" | 
|  | 173 | +        path = os.path.join(extensions, EXTENSION_PATH) | 
|  | 174 | +        ext_info = chromium_driver.webextension.install(path=path) | 
|  | 175 | +        extension_id = ext_info.get("extension") | 
|  | 176 | +        # Uninstall using the extension ID | 
|  | 177 | +        uninstall_extension_and_verify_extension_uninstalled(chromium_driver, extension_id) | 
0 commit comments