|
| 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 | +from python.runfiles import Runfiles |
| 23 | +from selenium.webdriver.common.by import By |
| 24 | +from selenium.webdriver.support.wait import WebDriverWait |
| 25 | + |
| 26 | + |
| 27 | +EXTENSION_ID = "webextensions-selenium-example-v3@example.com" |
| 28 | +EXTENSION_PATH = "webextensions-selenium-example-signed" |
| 29 | +EXTENSION_ARCHIVE_PATH = "webextensions-selenium-example.xpi" |
| 30 | + |
| 31 | +# Use bazel Runfiles to locate the test extension directory |
| 32 | +r = Runfiles.Create() |
| 33 | +extensions = r.Rlocation("selenium/py/test/extensions") |
| 34 | + |
| 35 | + |
| 36 | +def install_extension(driver, **kwargs): |
| 37 | + result = driver.webextension.install(**kwargs) |
| 38 | + assert result.get("extension") == EXTENSION_ID |
| 39 | + return result |
| 40 | + |
| 41 | + |
| 42 | +def verify_extension_injection(driver, pages): |
| 43 | + pages.load("blank.html") |
| 44 | + injected = WebDriverWait(driver, timeout=2).until( |
| 45 | + lambda dr: dr.find_element(By.ID, "webextensions-selenium-example") |
| 46 | + ) |
| 47 | + assert injected.text == "Content injected by webextensions-selenium-example" |
| 48 | + |
| 49 | + |
| 50 | +def uninstall_extension_and_verify_extension_uninstalled(driver, extension_info): |
| 51 | + driver.webextension.uninstall(extension_info) |
| 52 | + |
| 53 | + context_id = driver.current_window_handle |
| 54 | + driver.browsing_context.reload(context_id) |
| 55 | + assert len(driver.find_elements(By.ID, "webextensions-selenium-example")) == 0 |
| 56 | + |
| 57 | + |
| 58 | +def test_webextension_initialized(driver): |
| 59 | + """Test that the webextension module is initialized properly.""" |
| 60 | + assert driver.webextension is not None |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.xfail_chrome |
| 64 | +@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 | + |
| 69 | + ext_info = install_extension(driver, path=path) |
| 70 | + verify_extension_injection(driver, pages) |
| 71 | + uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) |
| 72 | + |
| 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) |
0 commit comments