|
| 1 | +# Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The SFC licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import base64 |
| 19 | +import os |
| 20 | +import pytest |
| 21 | + |
| 22 | +EXTENSION_ID = "[email protected]" |
| 23 | +EXTENSION_PATH = "common/extensions/webextensions-selenium-example-signed" |
| 24 | +EXTENSION_ARCHIVE_PATH = "common/extensions/webextensions-selenium-example.xpi" |
| 25 | + |
| 26 | +# Function to find the project root directory |
| 27 | +def find_project_root(): |
| 28 | + current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 29 | + |
| 30 | + while current_dir != os.path.dirname(current_dir): |
| 31 | + extensions_dir = os.path.join(current_dir, "common", "extensions") |
| 32 | + if os.path.isdir(extensions_dir): |
| 33 | + return current_dir |
| 34 | + current_dir = os.path.dirname(current_dir) |
| 35 | + |
| 36 | + return os.path.dirname(os.path.abspath(__file__)) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def locate_project_path(): |
| 41 | + """Locate the project path for the extension files.""" |
| 42 | + project_root = find_project_root() |
| 43 | + return project_root |
| 44 | + |
| 45 | + |
| 46 | +def test_webextension_initialized(driver): |
| 47 | + """Test that the webextension module is initialized properly.""" |
| 48 | + assert driver.webextension is not None |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.xfail_chrome |
| 52 | +@pytest.mark.xfail_edge |
| 53 | +def test_install_extension_path(driver, locate_project_path): |
| 54 | + """Test installing an extension from a directory path.""" |
| 55 | + path = os.path.join(locate_project_path, EXTENSION_PATH) |
| 56 | + |
| 57 | + ex_in = driver.webextension.install(path=path) |
| 58 | + assert ex_in.get("extension") == EXTENSION_ID |
| 59 | + |
| 60 | + driver.webextension.uninstall(ex_in) |
| 61 | + |
| 62 | +@pytest.mark.xfail_chrome |
| 63 | +@pytest.mark.xfail_edge |
| 64 | +def test_install_archive_extension_path(driver, locate_project_path): |
| 65 | + """Test installing an extension from an archive path.""" |
| 66 | + path = os.path.join(locate_project_path, EXTENSION_ARCHIVE_PATH) |
| 67 | + |
| 68 | + ex = driver.webextension.install(archive_path=path) |
| 69 | + assert ex.get("extension") == EXTENSION_ID |
| 70 | + |
| 71 | + driver.webextension.uninstall(ex) |
| 72 | + |
| 73 | +@pytest.mark.xfail_chrome |
| 74 | +@pytest.mark.xfail_edge |
| 75 | +def test_install_base64_extension_path(driver, locate_project_path): |
| 76 | + """Test installing an extension from a base64 encoded string.""" |
| 77 | + path = os.path.join(locate_project_path, EXTENSION_ARCHIVE_PATH) |
| 78 | + |
| 79 | + with open(path, "rb") as file: |
| 80 | + base64_encoded = base64.b64encode(file.read()).decode("utf-8") |
| 81 | + |
| 82 | + ex = driver.webextension.install(base64_value=base64_encoded) |
| 83 | + assert ex.get("extension") == EXTENSION_ID |
| 84 | + |
| 85 | + driver.webextension.uninstall(ex) |
| 86 | + |
| 87 | +@pytest.mark.xfail_chrome |
| 88 | +@pytest.mark.xfail_edge |
| 89 | +def test_install_with_extension_id_uninstall(driver, locate_project_path): |
| 90 | + """Test uninstalling an extension using just the extension ID.""" |
| 91 | + path = os.path.join(locate_project_path, EXTENSION_PATH) |
| 92 | + |
| 93 | + ex = driver.webextension.install(path=path) |
| 94 | + extension_id = ex.get("extension") |
| 95 | + assert extension_id == EXTENSION_ID |
| 96 | + |
| 97 | + # Uninstall using the extension ID |
| 98 | + driver.webextension.uninstall(extension_id) |
0 commit comments