Skip to content

Commit 3c8f861

Browse files
committed
add separate test class and driver for chrome/edge tests
1 parent 26d4dc9 commit 3c8f861

File tree

3 files changed

+90
-85
lines changed

3 files changed

+90
-85
lines changed

py/conftest.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,21 @@ def fin():
167167
global driver_instance
168168
if driver_instance is None:
169169
if driver_class == "Firefox":
170-
options = get_options(driver_class, request.config, request)
170+
options = get_options(driver_class, request.config)
171171
if platform.system() == "Linux":
172172
# There are issues with window size/position when running Firefox
173173
# under Wayland, so we use XWayland instead.
174174
os.environ["MOZ_ENABLE_WAYLAND"] = "0"
175175
if driver_class == "Chrome":
176-
options = get_options(driver_class, request.config, request)
176+
options = get_options(driver_class, request.config)
177177
if driver_class == "Edge":
178-
options = get_options(driver_class, request.config, request)
178+
options = get_options(driver_class, request.config)
179179
if driver_class == "WebKitGTK":
180-
options = get_options(driver_class, request.config, request)
180+
options = get_options(driver_class, request.config)
181181
if driver_class == "WPEWebKit":
182-
options = get_options(driver_class, request.config, request)
182+
options = get_options(driver_class, request.config)
183183
if driver_class == "Remote":
184-
options = get_options("Firefox", request.config, request) or webdriver.FirefoxOptions()
184+
options = get_options("Firefox", request.config) or webdriver.FirefoxOptions()
185185
options.set_capability("moz:firefoxOptions", {})
186186
options.enable_downloads = True
187187
if driver_path is not None:
@@ -210,7 +210,7 @@ def fin():
210210
driver_instance = None
211211

212212

213-
def get_options(driver_class, config, request=None):
213+
def get_options(driver_class, config):
214214
browser_path = config.option.binary
215215
browser_args = config.option.args
216216
headless = config.option.headless
@@ -237,15 +237,6 @@ def get_options(driver_class, config, request=None):
237237
options.web_socket_url = True
238238
options.unhandled_prompt_behavior = "ignore"
239239

240-
# Only enable webextensions for Chromium-based browsers when the test is marked with @pytest.mark.webextension
241-
if (
242-
request
243-
and request.node.get_closest_marker("webextension")
244-
and driver_class in ("Chrome", "Edge")
245-
and hasattr(options, "enable_webextensions")
246-
):
247-
options.enable_webextensions = True
248-
249240
return options
250241

251242

@@ -363,7 +354,7 @@ def clean_service(request):
363354
@pytest.fixture(scope="function")
364355
def clean_options(request):
365356
driver_class = get_driver_class(request.config.option.drivers[0])
366-
yield get_options(driver_class, request.config, request)
357+
yield get_options(driver_class, request.config)
367358

368359

369360
@pytest.fixture

py/pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ markers = [
8383
"xfail_safari: Tests expected to fail in Safari",
8484
"xfail_webkitgtk: Tests expected to fail in WebKitGTK",
8585
"xfail_wpewebkit: Tests expected to fail in WPEWebKit",
86-
"no_driver_after_test: If there are no drivers after the test it will create a new one.",
87-
"webextension: Tests that require webextension support (adds special flags for Chrome/Edge)"
86+
"no_driver_after_test: If there are no drivers after the test it will create a new one."
8887
]
8988
python_files = ["test_*.py", "*_test.py", "*_tests.py"]
9089
testpaths = ["test"]

py/test/selenium/webdriver/common/bidi_webextension_tests.py

Lines changed: 81 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
import pytest
2222
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
2426
from selenium.webdriver.common.by import By
2527
from selenium.webdriver.support.wait import WebDriverWait
2628

@@ -60,92 +62,105 @@ def test_webextension_initialized(driver):
6062
assert driver.webextension is not None
6163

6264

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."""
6669

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."""
6972

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)
7477

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."""
7680

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)
7985

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."""
8388

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)
8796

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")
92101
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)
95104

105+
def test_install_with_extension_id_uninstall(self, driver, pages):
106+
"""Test uninstalling an extension using just the extension ID."""
96107

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)
106113

107114

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."""
113118

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()
116122

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}")
118131

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
121134

122-
uninstall_extension_and_verify_extension_uninstalled(driver, ext_info)
135+
driver = browser_class(options=options)
123136

137+
yield driver
138+
driver.quit()
124139

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)
129144

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")
136146

147+
verify_extension_injection(chromium_driver, pages)
148+
uninstall_extension_and_verify_extension_uninstalled(chromium_driver, ext_info)
137149

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)
142154

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")
147156

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)
149159

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

Comments
 (0)