Skip to content

Commit f26e6bd

Browse files
[py] Add fixtures for clean objects
1 parent 73a5f70 commit f26e6bd

File tree

4 files changed

+120
-65
lines changed

4 files changed

+120
-65
lines changed

py/conftest.py

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,30 @@ def pytest_addoption(parser):
5050
metavar="DRIVER",
5151
help="driver to run tests against ({})".format(", ".join(drivers)),
5252
)
53-
parser.addoption("--browser-binary", action="store", dest="binary", help="location of the browser binary")
5453
parser.addoption(
55-
"--driver-binary", action="store", dest="executable", help="location of the service executable binary"
54+
"--browser-binary",
55+
action="store",
56+
dest="binary",
57+
help="location of the browser binary",
58+
)
59+
parser.addoption(
60+
"--driver-binary",
61+
action="store",
62+
dest="executable",
63+
help="location of the service executable binary",
64+
)
65+
parser.addoption(
66+
"--browser-args",
67+
action="store",
68+
dest="args",
69+
help="arguments to start the browser with",
70+
)
71+
parser.addoption(
72+
"--headless",
73+
action="store",
74+
dest="headless",
75+
help="Allow tests to run in headless",
5676
)
57-
parser.addoption("--browser-args", action="store", dest="args", help="arguments to start the browser with")
58-
parser.addoption("--headless", action="store", dest="headless", help="Allow tests to run in headless")
5977

6078

6179
def pytest_ignore_collect(path, config):
@@ -119,7 +137,9 @@ def fin():
119137
if driver_class == "Chrome":
120138
options = get_options(driver_class, request.config)
121139
if driver_class == "Remote":
122-
options = get_options("Firefox", request.config) or webdriver.FirefoxOptions()
140+
options = (
141+
get_options("Firefox", request.config) or webdriver.FirefoxOptions()
142+
)
123143
options.set_capability("moz:firefoxOptions", {})
124144
options.enable_downloads = True
125145
if driver_class == "WebKitGTK":
@@ -262,7 +282,9 @@ def wait_for_server(url, timeout):
262282
]
263283
)
264284
print(f"Selenium server running as process: {process.pid}")
265-
assert wait_for_server(url, 10), f"Timed out waiting for Selenium server at {url}"
285+
assert wait_for_server(
286+
url, 10
287+
), f"Timed out waiting for Selenium server at {url}"
266288
print("Selenium server is ready")
267289
yield process
268290
process.terminate()
@@ -283,3 +305,32 @@ def edge_service():
283305
from selenium.webdriver.edge.service import Service as EdgeService
284306

285307
return EdgeService
308+
309+
310+
@pytest.fixture(scope="function")
311+
def driver_executable(request):
312+
return request.config.option.executable
313+
314+
315+
@pytest.fixture(scope="function")
316+
def clean_service(request):
317+
try:
318+
driver_class = request.config.option.drivers[0].capitalize()
319+
except AttributeError:
320+
raise Exception("This test requires a --driver to be specified.")
321+
322+
yield get_service(driver_class, request.config.option.executable)
323+
324+
325+
@pytest.fixture(scope="function")
326+
def clean_driver(request):
327+
try:
328+
driver_class = request.config.option.drivers[0].capitalize()
329+
except AttributeError:
330+
raise Exception("This test requires a --driver to be specified.")
331+
332+
driver_reference = getattr(webdriver, driver_class)
333+
yield driver_reference
334+
335+
if request.node.get_closest_marker("no_driver_after_test"):
336+
driver_reference = None

py/test/selenium/webdriver/chrome/chrome_launcher_tests.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,22 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from selenium.webdriver import Chrome
18+
import pytest
19+
1920
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2021

2122

22-
def test_launch_and_close_browser():
23-
driver = Chrome()
23+
@pytest.mark.no_driver_after_test
24+
def test_launch_and_close_browser(clean_driver, clean_service):
25+
driver = clean_driver(service=clean_service)
2426
driver.quit()
2527

2628

27-
def test_we_can_launch_multiple_chrome_instances():
28-
driver1 = Chrome()
29-
driver2 = Chrome()
30-
driver3 = Chrome()
29+
@pytest.mark.no_driver_after_test
30+
def test_we_can_launch_multiple_chrome_instances(clean_driver, clean_service):
31+
driver1 = clean_driver(service=clean_service)
32+
driver2 = clean_driver(service=clean_service)
33+
driver3 = clean_driver(service=clean_service)
3134
driver1.quit()
3235
driver2.quit()
3336
driver3.quit()
34-
35-
36-
def test_launch_chrome_do_not_affect_default_capabilities():
37-
expected = DesiredCapabilities.CHROME.copy()
38-
driver = Chrome()
39-
actual = DesiredCapabilities.CHROME.copy()
40-
driver.quit()
41-
assert actual == expected

py/test/selenium/webdriver/chrome/chrome_service_tests.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,54 @@
2323
from selenium.webdriver.chrome.service import Service
2424
from selenium.common.exceptions import WebDriverException
2525

26-
# @pytest.mark.xfail_chrome(raises=WebDriverException)
27-
# def test_uses_chromedriver_logging() -> None:
28-
# log_file = "chromedriver.log"
29-
# service_args = ["--append-log"]
30-
#
31-
# service = Service(log_output=log_file, service_args=service_args)
32-
# driver2 = None
33-
# try:
34-
# driver1 = Chrome(service=service)
35-
# with open(log_file) as fp:
36-
# lines = len(fp.readlines())
37-
# driver2 = Chrome(service=service)
38-
# with open(log_file) as fp:
39-
# assert len(fp.readlines()) >= 2 * lines
40-
# finally:
41-
# driver1.quit()
42-
# if driver2:
43-
# driver2.quit()
44-
# os.remove(log_file)
26+
27+
@pytest.mark.xfail_chrome(raises=WebDriverException)
28+
@pytest.mark.no_driver_after_test
29+
def test_uses_chromedriver_logging(clean_driver, driver_executable) -> None:
30+
log_file = "chromedriver.log"
31+
service_args = ["--append-log"]
32+
33+
service = Service(
34+
log_output=log_file,
35+
service_args=service_args,
36+
executable_path=driver_executable,
37+
)
38+
driver2 = None
39+
try:
40+
driver1 = clean_driver(service=service)
41+
with open(log_file) as fp:
42+
lines = len(fp.readlines())
43+
driver2 = clean_driver(service=service)
44+
with open(log_file) as fp:
45+
assert len(fp.readlines()) >= 2 * lines
46+
finally:
47+
driver1.quit()
48+
if driver2:
49+
driver2.quit()
50+
os.remove(log_file)
4551

4652

47-
def test_log_output_as_filename() -> None:
53+
@pytest.mark.no_driver_after_test
54+
def test_log_output_as_filename(clean_driver, driver_executable) -> None:
4855
log_file = "chromedriver.log"
49-
service = Service(log_output=log_file)
56+
service = Service(log_output=log_file, executable_path=driver_executable)
5057
try:
5158
assert "--log-path=chromedriver.log" in service.service_args
52-
driver = Chrome(service=service)
59+
driver = clean_driver(service=service)
5360
with open(log_file) as fp:
5461
assert "Starting ChromeDriver" in fp.readline()
5562
finally:
5663
driver.quit()
5764
os.remove(log_file)
5865

5966

60-
def test_log_output_as_file() -> None:
67+
@pytest.mark.no_driver_after_test
68+
def test_log_output_as_file(clean_driver, driver_executable) -> None:
6169
log_name = "chromedriver.log"
6270
log_file = open(log_name, "w", encoding="utf-8")
63-
service = Service(log_output=log_file)
71+
service = Service(log_output=log_file, executable_path=driver_executable)
6472
try:
65-
driver = Chrome(service=service)
73+
driver = clean_driver(service=service)
6674
time.sleep(1)
6775
with open(log_name) as fp:
6876
assert "Starting ChromeDriver" in fp.readline()
@@ -72,18 +80,18 @@ def test_log_output_as_file() -> None:
7280
os.remove(log_name)
7381

7482

75-
def test_log_output_as_stdout(capfd) -> None:
76-
service = Service(log_output=subprocess.STDOUT)
77-
driver = Chrome(service=service)
83+
@pytest.mark.no_driver_after_test
84+
def test_log_output_as_stdout(clean_driver, capfd, driver_executable) -> None:
85+
service = Service(log_output=subprocess.STDOUT, executable_path=driver_executable)
86+
driver = clean_driver(service=service)
7887

7988
out, err = capfd.readouterr()
8089
assert "Starting ChromeDriver" in out
8190
driver.quit()
8291

8392

84-
def test_log_output_null_default(capfd) -> None:
85-
driver = Chrome()
86-
93+
@pytest.mark.no_driver_after_test
94+
def test_log_output_null_default(driver, capfd) -> None:
8795
out, err = capfd.readouterr()
8896
assert "Starting ChromeDriver" not in out
8997
driver.quit()

py/test/selenium/webdriver/chrome/proxy_tests.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,28 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
import pytest
1819
import os
19-
2020
import urllib3
2121

22-
from selenium import webdriver
23-
2422

25-
def test_bad_proxy_doesnt_interfere():
23+
@pytest.mark.no_driver_after_test
24+
def test_bad_proxy_doesnt_interfere(clean_driver, clean_service):
2625
# these values should be ignored if ignore_local_proxy_environment_variables() is called.
2726
os.environ["https_proxy"] = "bad"
2827
os.environ["http_proxy"] = "bad"
2928
options = webdriver.ChromeOptions()
3029

3130
options.ignore_local_proxy_environment_variables()
3231

33-
chrome_kwargs = {"options": options}
34-
driver = webdriver.Chrome(**chrome_kwargs)
35-
36-
assert hasattr(driver, "command_executor")
37-
assert hasattr(driver.command_executor, "_proxy_url")
38-
assert isinstance(driver.command_executor._conn, urllib3.PoolManager)
39-
os.environ.pop("https_proxy")
40-
os.environ.pop("http_proxy")
41-
driver.quit()
32+
chrome_kwargs = {"options": options, "service": clean_service}
33+
driver = clean_driver(**chrome_kwargs)
34+
35+
try:
36+
assert hasattr(driver, "command_executor")
37+
assert hasattr(driver.command_executor, "_proxy_url")
38+
assert isinstance(driver.command_executor._conn, urllib3.PoolManager)
39+
finally:
40+
os.environ.pop("https_proxy")
41+
os.environ.pop("http_proxy")
42+
driver.quit()

0 commit comments

Comments
 (0)