diff --git a/py/test/selenium/webdriver/chrome/chrome_service_tests.py b/py/test/selenium/webdriver/chrome/chrome_service_tests.py index ce0a6fcc25237..eb143c56475f1 100644 --- a/py/test/selenium/webdriver/chrome/chrome_service_tests.py +++ b/py/test/selenium/webdriver/chrome/chrome_service_tests.py @@ -17,6 +17,7 @@ import os import subprocess import time +from unittest.mock import patch import pytest @@ -125,8 +126,6 @@ def test_uses_path_from_env_variable(self, service): assert "chromedriver" in service.path def test_updates_path_after_setting_env_variable(self, service): - new_path = "/foo/bar" - os.environ["SE_CHROMEDRIVER"] = new_path service.executable_path = self.service_path # Simulating the update - - assert "chromedriver" in service.executable_path + with patch.dict("os.environ", {"SE_CHROMEDRIVER": "/foo/bar"}): + assert "chromedriver" in service.executable_path diff --git a/py/test/selenium/webdriver/chrome/proxy_tests.py b/py/test/selenium/webdriver/chrome/proxy_tests.py index 6a6f40af3e89e..10454e4285f1f 100644 --- a/py/test/selenium/webdriver/chrome/proxy_tests.py +++ b/py/test/selenium/webdriver/chrome/proxy_tests.py @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. -import os +from unittest.mock import patch import pytest import urllib3 @@ -25,20 +25,15 @@ @pytest.mark.no_driver_after_test def test_bad_proxy_doesnt_interfere(clean_driver, clean_service): - # these values should be ignored if ignore_local_proxy_environment_variables() is called. - os.environ["https_proxy"] = "bad" - os.environ["http_proxy"] = "bad" + # Proxy environment variables should be ignored if + # ignore_local_proxy_environment_variables() is called. options = webdriver.ChromeOptions() - options.ignore_local_proxy_environment_variables() - chrome_kwargs = {"options": options, "service": clean_service} - driver = clean_driver(**chrome_kwargs) - + with patch.dict("os.environ", {"http_proxy": "bad", "https_proxy": "bad"}): + driver = clean_driver(**chrome_kwargs) assert hasattr(driver, "command_executor") assert hasattr(driver.command_executor, "_proxy_url") assert isinstance(driver.command_executor._conn, urllib3.PoolManager) - os.environ.pop("https_proxy") - os.environ.pop("http_proxy") driver.quit() diff --git a/py/test/selenium/webdriver/edge/edge_service_tests.py b/py/test/selenium/webdriver/edge/edge_service_tests.py index bcf00b4af087a..2dbb32083c716 100644 --- a/py/test/selenium/webdriver/edge/edge_service_tests.py +++ b/py/test/selenium/webdriver/edge/edge_service_tests.py @@ -17,6 +17,7 @@ import os import subprocess import time +from unittest.mock import patch import pytest @@ -125,8 +126,6 @@ def test_uses_path_from_env_variable(self, service): assert "msedgedriver" in service.path def test_updates_path_after_setting_env_variable(self, service): - new_path = "/foo/bar" - os.environ["SE_EDGEDRIVER"] = new_path service.executable_path = self.service_path # Simulating the update - - assert "msedgedriver" in service.executable_path + with patch.dict("os.environ", {"SE_EDGEDRIVER": "/foo/bar"}): + assert "msedgedriver" in service.executable_path diff --git a/py/test/selenium/webdriver/firefox/firefox_service_tests.py b/py/test/selenium/webdriver/firefox/firefox_service_tests.py index 509a138d375d5..4c64ef4c211f4 100644 --- a/py/test/selenium/webdriver/firefox/firefox_service_tests.py +++ b/py/test/selenium/webdriver/firefox/firefox_service_tests.py @@ -16,6 +16,7 @@ # under the License. import os import subprocess +from unittest.mock import patch import pytest @@ -79,8 +80,6 @@ def test_uses_path_from_env_variable(self, service): assert "geckodriver" in service.path def test_updates_path_after_setting_env_variable(self, service): - new_path = "/foo/bar" - os.environ["SE_GECKODRIVER"] = new_path service.executable_path = self.service_path # Simulating the update - - assert "geckodriver" in service.executable_path + with patch.dict("os.environ", {"SE_GECKODRIVER": "/foo/bar"}): + assert "geckodriver" in service.executable_path diff --git a/py/test/selenium/webdriver/safari/safari_service_tests.py b/py/test/selenium/webdriver/safari/safari_service_tests.py index 494cbe6be6143..73d6ba5855d5c 100644 --- a/py/test/selenium/webdriver/safari/safari_service_tests.py +++ b/py/test/selenium/webdriver/safari/safari_service_tests.py @@ -16,6 +16,7 @@ # under the License. import os +from unittest.mock import patch import pytest @@ -41,11 +42,9 @@ def test_uses_path_from_env_variable(self, service): assert "safaridriver" in service.path def test_updates_path_after_setting_env_variable(self, service): - new_path = "/foo/bar" - os.environ["SE_SAFARIDRIVER"] = new_path service.executable_path = self.service_path # Simulating the update - - assert "safaridriver" in service.executable_path + with patch.dict("os.environ", {"SE_SAFARIDRIVER": "/foo/bar"}): + assert "safaridriver" in service.executable_path def test_enable_logging(): diff --git a/py/test/unit/selenium/webdriver/remote/remote_connection_tests.py b/py/test/unit/selenium/webdriver/remote/remote_connection_tests.py index fb7e865c68b04..d7d8583300a59 100644 --- a/py/test/unit/selenium/webdriver/remote/remote_connection_tests.py +++ b/py/test/unit/selenium/webdriver/remote/remote_connection_tests.py @@ -15,7 +15,6 @@ # specific language governing permissions and limitations # under the License. -import os from unittest.mock import patch from urllib import parse @@ -168,18 +167,17 @@ def test_get_proxy_direct_via_client_config(): def test_get_proxy_system_matches_no_proxy_via_client_config(): - os.environ["HTTP_PROXY"] = "http://admin:admin@system_proxy.com:8080" - os.environ["NO_PROXY"] = "localhost,127.0.0.1" - client_config = ClientConfig( - remote_server_addr="http://localhost:4444", proxy=Proxy({"proxyType": ProxyType.SYSTEM}) - ) - remote_connection = RemoteConnection(client_config=client_config) - conn = remote_connection._get_connection_manager() - assert isinstance(conn, urllib3.PoolManager) - proxy_url = remote_connection._client_config.get_proxy_url() - assert proxy_url is None - os.environ.pop("HTTP_PROXY") - os.environ.pop("NO_PROXY") + with patch.dict( + "os.environ", {"HTTP_PROXY": "http://admin:admin@system_proxy.com:8080", "NO_PROXY": "localhost,127.0.0.1"} + ): + client_config = ClientConfig( + remote_server_addr="http://localhost:4444", proxy=Proxy({"proxyType": ProxyType.SYSTEM}) + ) + remote_connection = RemoteConnection(client_config=client_config) + conn = remote_connection._get_connection_manager() + assert isinstance(conn, urllib3.PoolManager) + proxy_url = remote_connection._client_config.get_proxy_url() + assert proxy_url is None def test_get_proxy_url_none(mock_proxy_settings_missing):