|
20 | 20 |
|
21 | 21 | import pytest |
22 | 22 | from python.runfiles import Runfiles |
23 | | - |
| 23 | +from selenium import webdriver |
| 24 | +from selenium.webdriver.chrome.options import Options as ChromeOptions |
| 25 | +from selenium.webdriver.edge.options import Options as EdgeOptions |
24 | 26 | from selenium.webdriver.common.by import By |
25 | 27 | from selenium.webdriver.support.wait import WebDriverWait |
26 | 28 |
|
@@ -60,92 +62,105 @@ def test_webextension_initialized(driver): |
60 | 62 | assert driver.webextension is not None |
61 | 63 |
|
62 | 64 |
|
63 | | -@pytest.mark.webextension |
64 | | -def test_install_extension_path(driver, pages): |
65 | | - """Test installing an extension from a directory path. |
| 65 | +@pytest.mark.xfail_chrome |
| 66 | +@pytest.mark.xfail_edge |
| 67 | +class TestFirefoxWebExtension: |
| 68 | + """Firefox-specific WebExtension tests.""" |
66 | 69 |
|
67 | | - Note: For Chrome and Edge, webextensions are enabled when the test is marked with @pytest.mark.webextension. |
68 | | - You can also manually enable them using: |
| 70 | + def test_install_extension_path(self, driver, pages): |
| 71 | + """Test installing an extension from a directory path.""" |
69 | 72 |
|
70 | | - from selenium.webdriver.chrome.options import Options |
71 | | - options = Options() |
72 | | - options.enable_webextensions = True |
73 | | - driver = webdriver.Chrome(options=options) |
| 73 | + path = os.path.join(extensions, EXTENSION_PATH) |
| 74 | + ext_info = install_extension(driver, path=path) |
| 75 | + verify_extension_injection(driver, pages) |
| 76 | + uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) |
74 | 77 |
|
75 | | - Or directly pass the required flags when creating the driver: |
| 78 | + def test_install_archive_extension_path(self, driver, pages): |
| 79 | + """Test installing an extension from an archive path.""" |
76 | 80 |
|
77 | | - from selenium import webdriver |
78 | | - from selenium.webdriver.chrome.options import Options |
| 81 | + path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH) |
| 82 | + ext_info = install_extension(driver, archive_path=path) |
| 83 | + verify_extension_injection(driver, pages) |
| 84 | + uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) |
79 | 85 |
|
80 | | - options = Options() |
81 | | - options.add_argument("--remote-debugging-pipe") |
82 | | - options.add_argument("--enable-unsafe-extension-debugging") |
| 86 | + def test_install_base64_extension_path(self, driver, pages): |
| 87 | + """Test installing an extension from a base64 encoded string.""" |
83 | 88 |
|
84 | | - driver = webdriver.Chrome(options=options) |
85 | | - """ |
86 | | - path = os.path.join(extensions, EXTENSION_PATH) |
| 89 | + path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH) |
| 90 | + with open(path, "rb") as file: |
| 91 | + base64_encoded = base64.b64encode(file.read()).decode("utf-8") |
| 92 | + ext_info = install_extension(driver, base64_value=base64_encoded) |
| 93 | + # TODO: the extension is installed but the script is not injected, check and fix |
| 94 | + # verify_extension_injection(driver, pages) |
| 95 | + uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) |
87 | 96 |
|
88 | | - if driver.capabilities["browserName"].lower() in ("chrome", "microsoftedge"): |
89 | | - # chrome/edge does not uses extension id from manifest.json so we cannot assert the id |
90 | | - ext_info = driver.webextension.install(path=path) |
91 | | - else: |
| 97 | + def test_install_unsigned_extension(self, driver, pages): |
| 98 | + """Test installing an unsigned extension.""" |
| 99 | + |
| 100 | + path = os.path.join(extensions, "webextensions-selenium-example") |
92 | 101 | ext_info = install_extension(driver, path=path) |
93 | | - verify_extension_injection(driver, pages) |
94 | | - uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) |
| 102 | + verify_extension_injection(driver, pages) |
| 103 | + uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) |
95 | 104 |
|
| 105 | + def test_install_with_extension_id_uninstall(self, driver, pages): |
| 106 | + """Test uninstalling an extension using just the extension ID.""" |
96 | 107 |
|
97 | | -@pytest.mark.xfail_chrome |
98 | | -@pytest.mark.xfail_edge |
99 | | -def test_install_archive_extension_path(driver, pages): |
100 | | - """Test installing an extension from an archive path.""" |
101 | | - path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH) |
102 | | - |
103 | | - ext_info = install_extension(driver, archive_path=path) |
104 | | - verify_extension_injection(driver, pages) |
105 | | - uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) |
| 108 | + path = os.path.join(extensions, EXTENSION_PATH) |
| 109 | + ext_info = install_extension(driver, path=path) |
| 110 | + extension_id = ext_info.get("extension") |
| 111 | + # Uninstall using the extension ID |
| 112 | + uninstall_extension_and_verify_extension_uninstalled(driver, extension_id) |
106 | 113 |
|
107 | 114 |
|
108 | | -@pytest.mark.xfail_chrome |
109 | | -@pytest.mark.xfail_edge |
110 | | -def test_install_base64_extension_path(driver, pages): |
111 | | - """Test installing an extension from a base64 encoded string.""" |
112 | | - path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH) |
| 115 | +@pytest.mark.xfail_firefox |
| 116 | +class TestChromiumWebExtension: |
| 117 | + """Chrome/Edge-specific WebExtension tests with custom driver.""" |
113 | 118 |
|
114 | | - with open(path, "rb") as file: |
115 | | - base64_encoded = base64.b64encode(file.read()).decode("utf-8") |
| 119 | + @pytest.fixture |
| 120 | + def chromium_driver(self, request): |
| 121 | + driver_option = request.config.option.drivers[0].lower() |
116 | 122 |
|
117 | | - ext_info = install_extension(driver, base64_value=base64_encoded) |
| 123 | + if driver_option == "chrome": |
| 124 | + options = ChromeOptions() |
| 125 | + browser_class = webdriver.Chrome |
| 126 | + elif driver_option == "edge": |
| 127 | + options = EdgeOptions() |
| 128 | + browser_class = webdriver.Edge |
| 129 | + else: |
| 130 | + pytest.skip(f"This test requires Chrome or Edge, got {driver_option}") |
118 | 131 |
|
119 | | - # TODO: the extension is installed but the script is not injected, check and fix |
120 | | - # verify_extension_injection(driver, pages) |
| 132 | + options.enable_bidi = True |
| 133 | + options.enable_webextensions = True |
121 | 134 |
|
122 | | - uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) |
| 135 | + driver = browser_class(options=options) |
123 | 136 |
|
| 137 | + yield driver |
| 138 | + driver.quit() |
124 | 139 |
|
125 | | -@pytest.mark.webextension |
126 | | -def test_install_unsigned_extension(driver, pages): |
127 | | - """Test installing an unsigned extension.""" |
128 | | - path = os.path.join(extensions, "webextensions-selenium-example") |
| 140 | + def test_install_extension_path(self, chromium_driver, pages): |
| 141 | + """Test installing an extension from a directory path.""" |
| 142 | + path = os.path.join(extensions, EXTENSION_PATH) |
| 143 | + ext_info = chromium_driver.webextension.install(path=path) |
129 | 144 |
|
130 | | - if driver.capabilities["browserName"].lower() in ["chrome", "microsoftedge"]: |
131 | | - ext_info = driver.webextension.install(path=path) |
132 | | - else: |
133 | | - ext_info = install_extension(driver, path=path) |
134 | | - verify_extension_injection(driver, pages) |
135 | | - uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) |
| 145 | + chromium_driver.get("https://www.webpagetest.org/blank.html") |
136 | 146 |
|
| 147 | + verify_extension_injection(chromium_driver, pages) |
| 148 | + uninstall_extension_and_verify_extension_uninstalled(chromium_driver, ext_info) |
137 | 149 |
|
138 | | -@pytest.mark.webextension |
139 | | -def test_install_with_extension_id_uninstall(driver, pages): |
140 | | - """Test uninstalling an extension using just the extension ID.""" |
141 | | - path = os.path.join(extensions, EXTENSION_PATH) |
| 150 | + def test_install_unsigned_extension(self, chromium_driver, pages): |
| 151 | + """Test installing an unsigned extension.""" |
| 152 | + path = os.path.join(extensions, "webextensions-selenium-example") |
| 153 | + ext_info = chromium_driver.webextension.install(path=path) |
142 | 154 |
|
143 | | - if driver.capabilities["browserName"].lower() in ["chrome", "microsoftedge"]: |
144 | | - ext_info = driver.webextension.install(path=path) |
145 | | - else: |
146 | | - ext_info = install_extension(driver, path=path) |
| 155 | + chromium_driver.get("https://www.webpagetest.org/blank.html") |
147 | 156 |
|
148 | | - extension_id = ext_info.get("extension") |
| 157 | + verify_extension_injection(chromium_driver, pages) |
| 158 | + uninstall_extension_and_verify_extension_uninstalled(chromium_driver, ext_info) |
149 | 159 |
|
150 | | - # Uninstall using the extension ID |
151 | | - uninstall_extension_and_verify_extension_uninstalled(driver, extension_id) |
| 160 | + def test_install_with_extension_id_uninstall(self, chromium_driver, pages): |
| 161 | + """Test uninstalling an extension using just the extension ID.""" |
| 162 | + path = os.path.join(extensions, EXTENSION_PATH) |
| 163 | + ext_info = chromium_driver.webextension.install(path=path) |
| 164 | + extension_id = ext_info.get("extension") |
| 165 | + # Uninstall using the extension ID |
| 166 | + uninstall_extension_and_verify_extension_uninstalled(chromium_driver, extension_id) |
0 commit comments