Skip to content

Commit 6ce5e08

Browse files
committed
[py] Add headless fixture
1 parent 2e7f609 commit 6ce5e08

File tree

5 files changed

+22
-23
lines changed

5 files changed

+22
-23
lines changed

py/conftest.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from test.selenium.webdriver.common.network import get_lan_ip
2929
from test.selenium.webdriver.common.webserver import SimpleWebServer
3030

31+
3132
drivers = (
3233
"chrome",
3334
"edge",
@@ -96,6 +97,11 @@ def pytest_ignore_collect(path, config):
9697
return len([d for d in _drivers if d.lower() in parts]) > 0
9798

9899

100+
def pytest_generate_tests(metafunc):
101+
if "driver" in metafunc.fixturenames and metafunc.config.option.drivers:
102+
metafunc.parametrize("driver", metafunc.config.option.drivers, indirect=True)
103+
104+
99105
def get_driver_class(driver_option):
100106
"""Generate the driver class name from the lowercase driver option."""
101107
if driver_option == "webkitgtk":
@@ -391,6 +397,6 @@ def clean_driver(request):
391397
driver_reference = None
392398

393399

394-
def pytest_generate_tests(metafunc):
395-
if "driver" in metafunc.fixturenames and metafunc.config.option.drivers:
396-
metafunc.parametrize("driver", metafunc.config.option.drivers, indirect=True)
400+
@pytest.fixture
401+
def headless(request):
402+
return request.config.option.headless

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,26 @@ def is_running_wayland():
3030

3131

3232
@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")
33+
def test_firefox_opens_large_when_running_xwayland(headless):
3734
# setting environment variable `MOZ_ENABLE_WAYLAND=0` forces Firefox
3835
# to run under XWayland on Wayland based systems
3936
with patch.dict("os.environ", {"MOZ_ENABLE_WAYLAND": "0"}):
40-
try:
41-
driver = webdriver.Firefox(options=options)
37+
options = Options()
38+
if headless:
39+
options.add_argument("-headless")
40+
with webdriver.Firefox(options=options) as driver:
4241
size = driver.get_window_size()
4342
assert size["height"] > 500
4443
assert size["width"] > 500
45-
finally:
46-
driver.quit()
4744

4845

4946
@pytest.mark.skipif(not is_running_wayland(), reason="This test only runs on Linux under Wayland")
5047
@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
48+
def test_firefox_opens_large_when_running_wayland(headless):
5349
options = Options()
54-
if request.config.getoption("--headless"):
50+
if headless:
5551
options.add_argument("-headless")
56-
try:
57-
driver = webdriver.Firefox(options=options)
52+
with webdriver.Firefox(options=options) as driver:
5853
size = driver.get_window_size()
5954
assert size["height"] > 500
6055
assert size["width"] > 500
61-
finally:
62-
driver.quit()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ def convert(self, by, value):
2828
return super().convert(by, value)
2929

3030

31-
@pytest.mark.xfail
31+
@pytest.mark.skip(reason="Needs to be updated")
3232
def test_find_element_with_custom_locator(driver):
3333
driver.get("data:text/html,<div custom-attr='example'>Test</div>")
3434
element = driver.find_element("custom", "example")
3535
assert element is not None
3636
assert element.text == "Test"
3737

3838

39-
@pytest.mark.xfail
39+
@pytest.mark.skip(reason="Needs to be updated")
4040
def test_find_elements_with_custom_locator(driver):
4141
driver.get("data:text/html,<div custom-attr='example'>Test1</div><div custom-attr='example'>Test2</div>")
4242
elements = driver.find_elements("custom", "example")

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import os
1919
import tempfile
2020

21-
2221
from selenium.webdriver.common.by import By
2322
from selenium.webdriver.support.wait import WebDriverWait
2423

py/test/selenium/webdriver/remote/remote_firefox_profile_tests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
from selenium.webdriver.firefox.options import Options
2121

2222

23-
def test_profile_is_used():
23+
def test_profile_is_used(headless):
2424
ff_profile = FirefoxProfile()
2525
ff_profile.set_preference("browser.startup.page", "1")
2626
options = Options()
2727
options.profile = ff_profile
28-
options.add_argument("-headless")
28+
if headless:
29+
options.add_argument("-headless")
2930
with webdriver.Remote(options=options) as driver:
3031
assert "browser/content/blanktab.html" in driver.current_url

0 commit comments

Comments
 (0)