From 69b329f5336f680b5143f15c220375e81f6bd51a Mon Sep 17 00:00:00 2001 From: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com> Date: Wed, 9 Apr 2025 19:45:07 -0400 Subject: [PATCH 1/6] [py] Use mock in test_bad_proxy_doesnt_interfere --- py/test/selenium/webdriver/chrome/proxy_tests.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/py/test/selenium/webdriver/chrome/proxy_tests.py b/py/test/selenium/webdriver/chrome/proxy_tests.py index 6a6f40af3e89e..92dc9e3532b80 100644 --- a/py/test/selenium/webdriver/chrome/proxy_tests.py +++ b/py/test/selenium/webdriver/chrome/proxy_tests.py @@ -17,6 +17,8 @@ import os +from unittest.mock import patch + import pytest import urllib3 @@ -25,20 +27,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() From 6e45ca58ecd9b57848576cd0b85b95b38c62e542 Mon Sep 17 00:00:00 2001 From: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com> Date: Wed, 9 Apr 2025 19:45:37 -0400 Subject: [PATCH 2/6] [py] Use mock in test_updates_path_after_setting_env_variable --- py/test/selenium/webdriver/chrome/chrome_service_tests.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/py/test/selenium/webdriver/chrome/chrome_service_tests.py b/py/test/selenium/webdriver/chrome/chrome_service_tests.py index ce0a6fcc25237..79bb6423bf6c4 100644 --- a/py/test/selenium/webdriver/chrome/chrome_service_tests.py +++ b/py/test/selenium/webdriver/chrome/chrome_service_tests.py @@ -18,6 +18,8 @@ import subprocess import time +from unittest.mock import patch + import pytest from selenium.common.exceptions import WebDriverException @@ -125,8 +127,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 From 772029891230d9c434891d24b5b90b763dc21c7b Mon Sep 17 00:00:00 2001 From: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com> Date: Wed, 9 Apr 2025 20:02:28 -0400 Subject: [PATCH 3/6] [py] Use mock in test_updates_path_after_setting_env_variable --- py/test/selenium/webdriver/edge/edge_service_tests.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/py/test/selenium/webdriver/edge/edge_service_tests.py b/py/test/selenium/webdriver/edge/edge_service_tests.py index 43416faf76b70..182d6da5d3e6a 100644 --- a/py/test/selenium/webdriver/edge/edge_service_tests.py +++ b/py/test/selenium/webdriver/edge/edge_service_tests.py @@ -18,6 +18,8 @@ import subprocess import time +from unittest.mock import patch + import pytest from selenium.common.exceptions import WebDriverException @@ -116,8 +118,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_CHROMEDRIVER": "/foo/bar"}): + assert "msedgedriver" in service.executable_path From 416fdfb70e5c1a27ba2fe32d728cc80e4ff6855b Mon Sep 17 00:00:00 2001 From: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com> Date: Wed, 9 Apr 2025 20:16:53 -0400 Subject: [PATCH 4/6] [py] Use mock in test_updates_path_after_setting_env_variable --- py/test/selenium/webdriver/edge/edge_service_tests.py | 2 +- .../selenium/webdriver/firefox/firefox_service_tests.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/py/test/selenium/webdriver/edge/edge_service_tests.py b/py/test/selenium/webdriver/edge/edge_service_tests.py index 182d6da5d3e6a..1e8dd25729305 100644 --- a/py/test/selenium/webdriver/edge/edge_service_tests.py +++ b/py/test/selenium/webdriver/edge/edge_service_tests.py @@ -119,5 +119,5 @@ def test_uses_path_from_env_variable(self, service): def test_updates_path_after_setting_env_variable(self, service): service.executable_path = self.service_path # Simulating the update - with patch.dict("os.environ", {"SE_CHROMEDRIVER": "/foo/bar"}): + 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..75d959fe2584c 100644 --- a/py/test/selenium/webdriver/firefox/firefox_service_tests.py +++ b/py/test/selenium/webdriver/firefox/firefox_service_tests.py @@ -17,6 +17,8 @@ import os import subprocess +from unittest.mock import patch + import pytest from selenium.webdriver import Firefox @@ -79,8 +81,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 From 7f5c8325ba102adbb2a373c6100d687181c85c8a Mon Sep 17 00:00:00 2001 From: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com> Date: Wed, 9 Apr 2025 20:19:38 -0400 Subject: [PATCH 5/6] [py] Use mock in test_updates_path_after_setting_env_variable --- py/test/selenium/webdriver/safari/safari_service_tests.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/py/test/selenium/webdriver/safari/safari_service_tests.py b/py/test/selenium/webdriver/safari/safari_service_tests.py index 494cbe6be6143..c85f3b3d78538 100644 --- a/py/test/selenium/webdriver/safari/safari_service_tests.py +++ b/py/test/selenium/webdriver/safari/safari_service_tests.py @@ -17,6 +17,8 @@ import os +from unittest.mock import patch + import pytest from selenium.webdriver.safari.service import Service @@ -41,11 +43,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(): From f6f00982315b7ccb03ea7a637e51f9da296c0855 Mon Sep 17 00:00:00 2001 From: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com> Date: Wed, 9 Apr 2025 20:49:10 -0400 Subject: [PATCH 6/6] [py] Use mocks in tests --- .../webdriver/chrome/chrome_service_tests.py | 1 - .../selenium/webdriver/chrome/proxy_tests.py | 2 -- .../webdriver/edge/edge_service_tests.py | 1 - .../firefox/firefox_service_tests.py | 1 - .../webdriver/safari/safari_service_tests.py | 1 - .../remote/remote_connection_tests.py | 24 +++++++++---------- 6 files changed, 11 insertions(+), 19 deletions(-) diff --git a/py/test/selenium/webdriver/chrome/chrome_service_tests.py b/py/test/selenium/webdriver/chrome/chrome_service_tests.py index 79bb6423bf6c4..eb143c56475f1 100644 --- a/py/test/selenium/webdriver/chrome/chrome_service_tests.py +++ b/py/test/selenium/webdriver/chrome/chrome_service_tests.py @@ -17,7 +17,6 @@ import os import subprocess import time - from unittest.mock import patch import pytest diff --git a/py/test/selenium/webdriver/chrome/proxy_tests.py b/py/test/selenium/webdriver/chrome/proxy_tests.py index 92dc9e3532b80..10454e4285f1f 100644 --- a/py/test/selenium/webdriver/chrome/proxy_tests.py +++ b/py/test/selenium/webdriver/chrome/proxy_tests.py @@ -15,8 +15,6 @@ # specific language governing permissions and limitations # under the License. -import os - from unittest.mock import patch import pytest diff --git a/py/test/selenium/webdriver/edge/edge_service_tests.py b/py/test/selenium/webdriver/edge/edge_service_tests.py index 1e8dd25729305..e4f255017e1fa 100644 --- a/py/test/selenium/webdriver/edge/edge_service_tests.py +++ b/py/test/selenium/webdriver/edge/edge_service_tests.py @@ -17,7 +17,6 @@ import os import subprocess import time - from unittest.mock import patch import pytest diff --git a/py/test/selenium/webdriver/firefox/firefox_service_tests.py b/py/test/selenium/webdriver/firefox/firefox_service_tests.py index 75d959fe2584c..4c64ef4c211f4 100644 --- a/py/test/selenium/webdriver/firefox/firefox_service_tests.py +++ b/py/test/selenium/webdriver/firefox/firefox_service_tests.py @@ -16,7 +16,6 @@ # under the License. import os import subprocess - from unittest.mock import patch import pytest diff --git a/py/test/selenium/webdriver/safari/safari_service_tests.py b/py/test/selenium/webdriver/safari/safari_service_tests.py index c85f3b3d78538..73d6ba5855d5c 100644 --- a/py/test/selenium/webdriver/safari/safari_service_tests.py +++ b/py/test/selenium/webdriver/safari/safari_service_tests.py @@ -16,7 +16,6 @@ # under the License. import os - from unittest.mock import patch import pytest 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):