Skip to content

Commit

Permalink
[py] Add the ability to ignore local proxys that are available
Browse files Browse the repository at this point in the history
This adds the option to ignore HTTP_PROXY and HTTPS_PROXY env var
that might be set. Fixes SeleniumHQ#8768.
  • Loading branch information
AutomatedTester committed Oct 13, 2020
1 parent 5e61342 commit af3e6a5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
8 changes: 7 additions & 1 deletion py/selenium/webdriver/common/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ def default_capabilities(self):
"""Return minimal capabilities necessary as a dictionary."""



class ArgOptions(BaseOptions):

def __init__(self):
super(ArgOptions, self).__init__()
self._arguments = []
self._ignore_local_proxy = False

@property
def arguments(self):
Expand All @@ -75,6 +75,12 @@ def add_argument(self, argument):
else:
raise ValueError('argument can not be null')

def ignore_local_proxy_environment_variables(self):
"""
By calling this you will ignore HTTP_PROXY and HTTPS_PROXY from being picked up and used.
"""
self._ignore_local_proxy = True

def to_capabilities(self):
return self._caps

Expand Down
4 changes: 2 additions & 2 deletions py/selenium/webdriver/remote/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ def _get_connection_manager(self):
return urllib3.PoolManager(**pool_manager_init_args) if self._proxy_url is None else \
urllib3.ProxyManager(self._proxy_url, **pool_manager_init_args)

def __init__(self, remote_server_addr, keep_alive=False, resolve_ip=None):
def __init__(self, remote_server_addr, keep_alive=False, resolve_ip=None, ignore_proxy=False):
if resolve_ip is not None:
import warnings
warnings.warn(
"'resolve_ip' option removed; ip addresses are now always resolved by urllib3.",
DeprecationWarning)
self.keep_alive = keep_alive
self._url = remote_server_addr
self._proxy_url = self._get_proxy_url()
self._proxy_url = self._get_proxy_url() if not ignore_proxy else None
if keep_alive:
self._conn = self._get_connection_manager()

Expand Down
8 changes: 6 additions & 2 deletions py/selenium/webdriver/remote/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _make_w3c_caps(caps):
return {"firstMatch": [{}], "alwaysMatch": always_match}


def get_remote_connection(capabilities, command_executor, keep_alive):
def get_remote_connection(capabilities, command_executor, keep_alive, ignore_local_proxy=False):
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
from selenium.webdriver.safari.remote_connection import SafariRemoteConnection
from selenium.webdriver.firefox.remote_connection import FirefoxRemoteConnection
Expand Down Expand Up @@ -179,16 +179,20 @@ def __init__(self, command_executor='http://127.0.0.1:4444',
- options - instance of a driver options.Options class
"""
capabilities = {}
_ignore_local_proxy = False
if options is not None:
capabilities = options.to_capabilities()
_ignore_local_proxy = options._ignore_local_proxy
if desired_capabilities is not None:
if not isinstance(desired_capabilities, dict):
raise WebDriverException("Desired Capabilities must be a dictionary")
else:
capabilities.update(desired_capabilities)
self.command_executor = command_executor
if isinstance(self.command_executor, (str, bytes)):
self.command_executor = get_remote_connection(capabilities, command_executor=command_executor, keep_alive=keep_alive)
self.command_executor = get_remote_connection(capabilities, command_executor=command_executor,
keep_alive=keep_alive,
ignore_local_proxy=_ignore_local_proxy)
self._is_remote = True
self.session_id = None
self.caps = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ def test_get_connection_manager_with_proxy(mock_proxy_settings):
assert conn.proxy.port == 8080


def test_ignore_proxy_env_vars(mock_proxy_settings):
remote_connection = RemoteConnection("http://remote", ignore_proxy=True)
conn = remote_connection._get_connection_manager()
assert type(conn) == urllib3.PoolManager


class MockResponse:
code = 200
headers = []
Expand Down

0 comments on commit af3e6a5

Please sign in to comment.