Skip to content

Commit cef94af

Browse files
authored
[py] Re-enable remote tests in bazel and fix broken tests (#15657)
* Updated bazel config to include tests in remote directory * Fixed broken remote tests * Removed duplicate conftests * Added firefox_options pytest fixture * Cleaned up conftest
1 parent 7a60563 commit cef94af

13 files changed

+81
-142
lines changed

py/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ py_library(
321321
"test/selenium/webdriver/__init__.py",
322322
"test/selenium/webdriver/chrome/__init__.py",
323323
"test/selenium/webdriver/common/__init__.py",
324-
"test/selenium/webdriver/common/conftest.py",
325324
"test/selenium/webdriver/common/network.py",
326325
"test/selenium/webdriver/common/webserver.py",
327326
"test/selenium/webdriver/firefox/__init__.py",
@@ -330,7 +329,6 @@ py_library(
330329
"test/selenium/webdriver/marionette/conftest.py",
331330
"test/selenium/webdriver/safari/conftest.py",
332331
"test/selenium/webdriver/support/__init__.py",
333-
"test/selenium/webdriver/support/conftest.py",
334332
],
335333
data = [
336334
"pyproject.toml",
@@ -547,6 +545,7 @@ py_test_suite(
547545
srcs = glob(
548546
[
549547
"test/selenium/webdriver/common/**/*.py",
548+
"test/selenium/webdriver/remote/**/*.py",
550549
"test/selenium/webdriver/support/**/*.py",
551550
],
552551
exclude = BIDI_TESTS,

py/conftest.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ def pytest_ignore_collect(path, config):
9696
return len([d for d in _drivers if d.lower() in parts]) > 0
9797

9898

99+
def pytest_generate_tests(metafunc):
100+
if "driver" in metafunc.fixturenames and metafunc.config.option.drivers:
101+
metafunc.parametrize("driver", metafunc.config.option.drivers, indirect=True)
102+
103+
99104
def get_driver_class(driver_option):
100105
"""Generate the driver class name from the lowercase driver option."""
101106
if driver_option == "webkitgtk":
@@ -113,20 +118,29 @@ def get_driver_class(driver_option):
113118
@pytest.fixture(scope="function")
114119
def driver(request):
115120
kwargs = {}
121+
driver_option = getattr(request, "param", "Chrome")
122+
116123
# browser can be changed with `--driver=firefox` as an argument or to addopts in pytest.ini
117-
driver_class = get_driver_class(getattr(request, "param", "Chrome"))
118-
# skip tests if not available on the platform
124+
driver_class = get_driver_class(driver_option)
125+
126+
# skip tests in the 'remote' directory if run with a local driver
127+
if request.node.path.parts[-2] == "remote" and driver_class != "Remote":
128+
pytest.skip(f"Remote tests can't be run with driver '{driver_option}'")
129+
130+
# skip tests that can't run on certain platforms
119131
_platform = platform.system()
120132
if driver_class == "Safari" and _platform != "Darwin":
121133
pytest.skip("Safari tests can only run on an Apple OS")
122134
if (driver_class == "Ie") and _platform != "Windows":
123135
pytest.skip("IE and EdgeHTML Tests can only run on Windows")
124136
if "WebKit" in driver_class and _platform == "Windows":
125137
pytest.skip("WebKit tests cannot be run on Windows")
138+
126139
# skip tests for drivers that don't support BiDi when --bidi is enabled
127140
if request.config.option.bidi:
128141
if driver_class in ("Ie", "Safari", "WebKitGTK", "WPEWebKit"):
129142
pytest.skip(f"{driver_class} does not support BiDi")
143+
130144
# conditionally mark tests as expected to fail based on driver
131145
marker = request.node.get_closest_marker(f"xfail_{driver_class.lower()}")
132146

@@ -177,6 +191,7 @@ def fin():
177191
kwargs["options"] = options
178192

179193
driver_instance = getattr(webdriver, driver_class)(**kwargs)
194+
180195
yield driver_instance
181196
# Close the browser after BiDi tests. Those make event subscriptions
182197
# and doesn't seems to be stable enough, causing the flakiness of the
@@ -217,7 +232,6 @@ def get_options(driver_class, config):
217232
if headless:
218233
if not options:
219234
options = getattr(webdriver, f"{driver_class}Options")()
220-
221235
if driver_class == "Chrome" or driver_class == "Edge":
222236
options.add_argument("--headless=new")
223237
if driver_class == "Firefox":
@@ -226,7 +240,6 @@ def get_options(driver_class, config):
226240
if bidi:
227241
if not options:
228242
options = getattr(webdriver, f"{driver_class}Options")()
229-
230243
options.web_socket_url = True
231244
options.unhandled_prompt_behavior = "ignore"
232245

@@ -382,3 +395,11 @@ def clean_driver(request):
382395

383396
if request.node.get_closest_marker("no_driver_after_test"):
384397
driver_reference = None
398+
399+
400+
@pytest.fixture
401+
def firefox_options(request):
402+
options = webdriver.FirefoxOptions()
403+
if request.config.option.headless:
404+
options.add_argument("-headless")
405+
return options

py/test/selenium/webdriver/chrome/conftest.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

py/test/selenium/webdriver/common/conftest.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

py/test/selenium/webdriver/edge/conftest.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

py/test/selenium/webdriver/firefox/firefox_sizing_tests.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,27 @@
2222
import pytest
2323

2424
from selenium import webdriver
25-
from selenium.webdriver.firefox.options import Options
2625

2726

2827
def is_running_wayland():
2928
return platform.system() == "Linux" and os.getenv("WAYLAND_DISPLAY")
3029

3130

3231
@pytest.mark.skipif(not is_running_wayland(), reason="This test only runs on Linux under Wayland")
33-
def test_firefox_opens_large_when_running_xwayland(request): # noqa: F821
34-
options = Options()
35-
if request.config.getoption("--headless"):
36-
options.add_argument("-headless")
32+
def test_firefox_opens_large_when_running_xwayland(firefox_options):
3733
# setting environment variable `MOZ_ENABLE_WAYLAND=0` forces Firefox
3834
# to run under XWayland on Wayland based systems
3935
with patch.dict("os.environ", {"MOZ_ENABLE_WAYLAND": "0"}):
40-
try:
41-
driver = webdriver.Firefox(options=options)
36+
with webdriver.Firefox(options=firefox_options) as driver:
4237
size = driver.get_window_size()
4338
assert size["height"] > 500
4439
assert size["width"] > 500
45-
finally:
46-
driver.quit()
4740

4841

4942
@pytest.mark.skipif(not is_running_wayland(), reason="This test only runs on Linux under Wayland")
5043
@pytest.mark.xfail(reason="https://bugzilla.mozilla.org/show_bug.cgi?id=1959040")
51-
# Firefox opens in a small window when running on Linux/Wayland
52-
def test_firefox_opens_large_when_running_wayland(request): # noqa: F821
53-
options = Options()
54-
if request.config.getoption("--headless"):
55-
options.add_argument("-headless")
56-
try:
57-
driver = webdriver.Firefox(options=options)
44+
def test_firefox_opens_large_when_running_wayland(firefox_options):
45+
with webdriver.Firefox(options=firefox_options) as driver:
5846
size = driver.get_window_size()
5947
assert size["height"] > 500
6048
assert size["width"] > 500
61-
finally:
62-
driver.quit()

py/test/selenium/webdriver/remote/remote_connection_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
1718
import base64
1819

1920
import filetype
2021

2122

2223
def test_browser_specific_method(driver, pages):
24+
"""This only works on Firefox"""
2325
pages.load("simpleTest.html")
2426
screenshot = driver.execute("FULL_PAGE_SCREENSHOT")["value"]
2527
result = base64.b64decode(screenshot)

py/test/selenium/webdriver/remote/custom_element_tests.py renamed to py/test/selenium/webdriver/remote/remote_custom_element_tests.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
18+
import pytest
19+
1720
from selenium.webdriver.common.by import By
1821
from selenium.webdriver.remote.webelement import WebElement
1922

@@ -24,20 +27,27 @@ def custom_method(self):
2427
return "Custom element method"
2528

2629

27-
def test_find_element_with_custom_class(driver, pages):
30+
@pytest.fixture()
31+
def custom_element_driver(driver):
32+
try:
33+
driver._web_element_cls = MyCustomElement
34+
yield driver
35+
finally:
36+
driver._web_element_cls = WebElement
37+
38+
39+
def test_find_element_with_custom_class(custom_element_driver, pages):
2840
"""Test to ensure custom element class is used for a single element."""
29-
driver._web_element_cls = MyCustomElement
3041
pages.load("simpleTest.html")
31-
element = driver.find_element(By.TAG_NAME, "body")
42+
element = custom_element_driver.find_element(By.TAG_NAME, "body")
3243
assert isinstance(element, MyCustomElement)
3344
assert element.custom_method() == "Custom element method"
3445

3546

36-
def test_find_elements_with_custom_class(driver, pages):
47+
def test_find_elements_with_custom_class(custom_element_driver, pages):
3748
"""Test to ensure custom element class is used for multiple elements."""
38-
driver._web_element_cls = MyCustomElement
3949
pages.load("simpleTest.html")
40-
elements = driver.find_elements(By.TAG_NAME, "div")
50+
elements = custom_element_driver.find_elements(By.TAG_NAME, "div")
4151
assert all(isinstance(el, MyCustomElement) for el in elements)
4252
assert all(el.custom_method() == "Custom element method" for el in elements)
4353

py/test/selenium/webdriver/remote/remote_custom_locator_tests.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
18+
import pytest
19+
20+
from selenium import webdriver
1721
from selenium.webdriver.remote.locator_converter import LocatorConverter
1822

1923

@@ -25,16 +29,25 @@ def convert(self, by, value):
2529
return super().convert(by, value)
2630

2731

28-
def test_find_element_with_custom_locator(driver):
29-
driver.get("data:text/html,<div custom-attr='example'>Test</div>")
30-
element = driver.find_element("custom", "example")
32+
@pytest.fixture()
33+
def custom_locator_driver(firefox_options):
34+
driver = webdriver.Remote(options=firefox_options, locator_converter=CustomLocatorConverter())
35+
yield driver
36+
driver.quit()
37+
38+
39+
def test_find_element_with_custom_locator(custom_locator_driver):
40+
custom_locator_driver.get("data:text/html,<div custom-attr='example'>Test</div>")
41+
element = custom_locator_driver.find_element("custom", "example")
3142
assert element is not None
3243
assert element.text == "Test"
3344

3445

35-
def test_find_elements_with_custom_locator(driver):
36-
driver.get("data:text/html,<div custom-attr='example'>Test1</div><div custom-attr='example'>Test2</div>")
37-
elements = driver.find_elements("custom", "example")
46+
def test_find_elements_with_custom_locator(custom_locator_driver):
47+
custom_locator_driver.get(
48+
"data:text/html,<div custom-attr='example'>Test1</div><div custom-attr='example'>Test2</div>"
49+
)
50+
elements = custom_locator_driver.find_elements("custom", "example")
3851
assert len(elements) == 2
3952
assert elements[0].text == "Test1"
4053
assert elements[1].text == "Test2"

py/test/selenium/webdriver/remote/remote_downloads_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
1718
import os
1819
import tempfile
1920

0 commit comments

Comments
 (0)