Skip to content

Commit c0903b6

Browse files
committed
[py] use ClientConfig for keep_alive, proxy, timeout and certificate path settings
1 parent 44ea09a commit c0903b6

File tree

15 files changed

+236
-103
lines changed

15 files changed

+236
-103
lines changed

py/selenium/webdriver/chrome/webdriver.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2121
from selenium.webdriver.common.driver_finder import DriverFinder
2222

23+
from ..remote.client_config import ClientConfig
2324
from .options import Options
2425
from .service import DEFAULT_EXECUTABLE_PATH
2526
from .service import Service
2627

2728
DEFAULT_PORT = 0
2829
DEFAULT_SERVICE_LOG_PATH = None
29-
DEFAULT_KEEP_ALIVE = None
3030

3131

3232
class WebDriver(ChromiumDriver):
@@ -46,7 +46,8 @@ def __init__(
4646
service_log_path=DEFAULT_SERVICE_LOG_PATH,
4747
chrome_options=None,
4848
service: Service = None,
49-
keep_alive=DEFAULT_KEEP_ALIVE,
49+
keep_alive=None,
50+
client_config: ClientConfig = ClientConfig(),
5051
) -> None:
5152
"""Creates a new instance of the chrome driver. Starts the service and
5253
then creates new instance of chrome driver.
@@ -69,12 +70,6 @@ def __init__(
6970
if chrome_options:
7071
warnings.warn("use options instead of chrome_options", DeprecationWarning, stacklevel=2)
7172
options = chrome_options
72-
if keep_alive != DEFAULT_KEEP_ALIVE:
73-
warnings.warn(
74-
"keep_alive has been deprecated, please pass in a Service object", DeprecationWarning, stacklevel=2
75-
)
76-
else:
77-
keep_alive = True
7873
if not options:
7974
options = self.create_options()
8075
if not service:
@@ -91,6 +86,7 @@ def __init__(
9186
service_log_path,
9287
service,
9388
keep_alive,
89+
client_config,
9490
)
9591

9692
def create_options(self) -> Options:

py/selenium/webdriver/chromium/remote_connection.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717
import typing
1818

19+
from selenium.webdriver.remote.client_config import ClientConfig
1920
from selenium.webdriver.remote.remote_connection import RemoteConnection
2021

2122

@@ -25,10 +26,9 @@ def __init__(
2526
remote_server_addr: str,
2627
vendor_prefix: str,
2728
browser_name: str,
28-
keep_alive: bool = True,
29-
ignore_proxy: typing.Optional[bool] = False,
29+
client_config: ClientConfig = ClientConfig(),
3030
) -> None:
31-
super().__init__(remote_server_addr, keep_alive, ignore_proxy=ignore_proxy)
31+
super().__init__(remote_server_addr, client_config=client_config)
3232
self.browser_name = browser_name
3333
self._commands["launchApp"] = ("POST", "/session/$sessionId/chromium/launch_app")
3434
self._commands["setPermissions"] = ("POST", "/session/$sessionId/permissions")

py/selenium/webdriver/chromium/webdriver.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
import warnings
1919

2020
from selenium.webdriver.chrome.options import Options as ChromeOptions
21-
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
2221
from selenium.webdriver.common.options import BaseOptions
2322
from selenium.webdriver.common.service import Service
2423
from selenium.webdriver.edge.options import Options as EdgeOptions
24+
from selenium.webdriver.remote.client_config import ClientConfig
2525
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
2626

2727
DEFAULT_PORT = 0
2828
DEFAULT_SERVICE_LOG_PATH = None
29-
DEFAULT_KEEP_ALIVE = None
3029

3130

3231
class ChromiumDriver(RemoteWebDriver):
@@ -43,7 +42,8 @@ def __init__(
4342
desired_capabilities=None,
4443
service_log_path=DEFAULT_SERVICE_LOG_PATH,
4544
service: Service = None,
46-
keep_alive=DEFAULT_KEEP_ALIVE,
45+
keep_alive=None,
46+
client_config: ClientConfig = ClientConfig(),
4747
) -> None:
4848
"""Creates a new WebDriver instance of the ChromiumDriver. Starts the
4949
service and then creates new WebDriver instance of ChromiumDriver.
@@ -74,12 +74,6 @@ def __init__(
7474
DeprecationWarning,
7575
stacklevel=2,
7676
)
77-
if keep_alive != DEFAULT_KEEP_ALIVE and type(self) == __class__:
78-
warnings.warn(
79-
"keep_alive has been deprecated, please pass in a Service object", DeprecationWarning, stacklevel=2
80-
)
81-
else:
82-
keep_alive = True
8377

8478
self.vendor_prefix = vendor_prefix
8579

@@ -101,7 +95,12 @@ def __init__(
10195
self.service.start()
10296

10397
try:
104-
super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive,)
98+
super().__init__(
99+
command_executor=self.service.service_url,
100+
options=options,
101+
keep_alive=keep_alive,
102+
client_config=client_config,
103+
)
105104
except Exception:
106105
self.quit()
107106
raise

py/selenium/webdriver/common/options.py

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
import typing
18+
import warnings
1819
from abc import ABCMeta
1920
from abc import abstractmethod
2021

@@ -251,6 +252,12 @@ def add_argument(self, argument):
251252
def ignore_local_proxy_environment_variables(self) -> None:
252253
"""By calling this you will ignore HTTP_PROXY and HTTPS_PROXY from
253254
being picked up and used."""
255+
warnings.warn(
256+
"setting ignore proxy in Options has been deprecated, "
257+
"set ProxyType.DIRECT in ClientConfig and pass to WebDriver constructor instead",
258+
DeprecationWarning,
259+
stacklevel=2,
260+
)
254261
self._ignore_local_proxy = True
255262

256263
def to_capabilities(self):

py/selenium/webdriver/edge/webdriver.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2121
from selenium.webdriver.common.driver_finder import DriverFinder
2222

23+
from ..remote.client_config import ClientConfig
2324
from .options import Options
2425
from .service import DEFAULT_EXECUTABLE_PATH
2526
from .service import Service
@@ -44,7 +45,8 @@ def __init__(
4445
capabilities=None,
4546
service_log_path=DEFAULT_SERVICE_LOG_PATH,
4647
service: Service = None,
47-
keep_alive=False,
48+
keep_alive=None,
49+
client_config: ClientConfig = ClientConfig(),
4850
verbose=False, # Todo: Why is this now unused?
4951
) -> None:
5052
"""Creates a new instance of the edge driver. Starts the service and
@@ -59,7 +61,7 @@ def __init__(
5961
capabilities only, such as "proxy" or "loggingPref".
6062
- service_log_path - Deprecated: Where to log information from the driver.
6163
- service - Service object for handling the browser driver if you need to pass extra details
62-
- keep_alive - Whether to configure EdgeRemoteConnection to use HTTP keep-alive.
64+
- keep_alive - Deprecated: Whether to configure EdgeRemoteConnection to use HTTP keep-alive.
6365
- verbose - whether to set verbose logging in the service.
6466
"""
6567
if executable_path != "msedgedriver":
@@ -83,6 +85,7 @@ def __init__(
8385
service_log_path,
8486
service,
8587
keep_alive,
88+
client_config,
8689
)
8790

8891
def create_options(self) -> Options:

py/selenium/webdriver/firefox/remote_connection.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
# under the License.
1717

1818
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
19+
from selenium.webdriver.remote.client_config import ClientConfig
1920
from selenium.webdriver.remote.remote_connection import RemoteConnection
2021

2122

2223
class FirefoxRemoteConnection(RemoteConnection):
2324
browser_name = DesiredCapabilities.FIREFOX["browserName"]
2425

25-
def __init__(self, remote_server_addr, keep_alive=True, ignore_proxy=False) -> None:
26-
super().__init__(remote_server_addr, keep_alive, ignore_proxy=ignore_proxy)
26+
def __init__(self, remote_server_addr: str, client_config: ClientConfig = ClientConfig()) -> None:
27+
super().__init__(remote_server_addr, client_config=client_config)
2728

2829
self._commands["GET_CONTEXT"] = ("GET", "/session/$sessionId/moz/context")
2930
self._commands["SET_CONTEXT"] = ("POST", "/session/$sessionId/moz/context")

py/selenium/webdriver/firefox/webdriver.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
from selenium.webdriver.common.driver_finder import DriverFinder
2828
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
2929

30+
from ..remote.client_config import ClientConfig
3031
from .firefox_binary import FirefoxBinary
3132
from .firefox_profile import FirefoxProfile
3233
from .options import Options
33-
from .remote_connection import FirefoxRemoteConnection
3434
from .service import DEFAULT_EXECUTABLE_PATH
3535
from .service import Service
3636

@@ -58,7 +58,8 @@ def __init__(
5858
service=None,
5959
desired_capabilities=None,
6060
log_path=DEFAULT_LOG_PATH,
61-
keep_alive=True, # Todo: Why is this now unused?
61+
keep_alive=None,
62+
client_config: ClientConfig = ClientConfig(),
6263
) -> None:
6364
"""Starts a new local session of Firefox.
6465
@@ -106,7 +107,7 @@ def __init__(
106107
:param desired_capabilities: Deprecated: alias of capabilities. In future
107108
versions of this library, this will replace 'capabilities'.
108109
This will make the signature consistent with RemoteWebDriver.
109-
:param keep_alive: Whether to configure remote_connection.RemoteConnection to use
110+
:param keep_alive - Deprecated: Whether to configure remote_connection.RemoteConnection to use
110111
HTTP keep-alive.
111112
"""
112113

@@ -195,7 +196,12 @@ def __init__(
195196
self.service.path = DriverFinder.get_path(self.service, options)
196197
self.service.start()
197198

198-
super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive)
199+
super().__init__(
200+
command_executor=self.service.service_url,
201+
options=options,
202+
keep_alive=keep_alive,
203+
client_config=client_config,
204+
)
199205

200206
self._is_remote = False
201207

py/selenium/webdriver/ie/webdriver.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from selenium.webdriver.common.driver_finder import DriverFinder
2222
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
2323

24+
from ..remote.client_config import ClientConfig
2425
from .options import Options
2526
from .service import DEFAULT_EXECUTABLE_PATH
2627
from .service import Service
@@ -30,7 +31,6 @@
3031
DEFAULT_HOST = None
3132
DEFAULT_LOG_LEVEL = None
3233
DEFAULT_SERVICE_LOG_PATH = None
33-
DEFAULT_KEEP_ALIVE = None
3434

3535

3636
class WebDriver(RemoteWebDriver):
@@ -49,7 +49,8 @@ def __init__(
4949
options: Options = None,
5050
service: Service = None,
5151
desired_capabilities=None,
52-
keep_alive=DEFAULT_KEEP_ALIVE,
52+
keep_alive=None,
53+
client_config: ClientConfig = ClientConfig(),
5354
) -> None:
5455
"""Creates a new instance of the Ie driver.
5556
@@ -102,12 +103,6 @@ def __init__(
102103
DeprecationWarning,
103104
stacklevel=2,
104105
)
105-
if keep_alive != DEFAULT_KEEP_ALIVE:
106-
warnings.warn(
107-
"keep_alive has been deprecated, please pass in a Service object", DeprecationWarning, stacklevel=2
108-
)
109-
else:
110-
keep_alive = True
111106

112107
self.host = host
113108
self.port = port
@@ -127,7 +122,12 @@ def __init__(
127122
self.iedriver.path = DriverFinder.get_path(self.iedriver, options)
128123
self.iedriver.start()
129124

130-
super().__init__(command_executor=self.iedriver.service_url, options=options, keep_alive=keep_alive)
125+
super().__init__(
126+
command_executor=self.iedriver.service_url,
127+
options=options,
128+
keep_alive=keep_alive,
129+
client_config=client_config,
130+
)
131131
self._is_remote = False
132132

133133
def quit(self) -> None:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
from selenium.webdriver.common.proxy import Proxy
19+
from selenium.webdriver.common.proxy import ProxyType
20+
21+
22+
class ClientConfig:
23+
24+
def __init__(
25+
self, keep_alive: bool = True, timeout=None, proxy=Proxy({"proxyType": ProxyType.SYSTEM}), certificate_path=None
26+
) -> None:
27+
self._keep_alive = keep_alive
28+
self._timeout = timeout
29+
self._proxy = proxy
30+
self._certificate_path = certificate_path
31+
32+
@property
33+
def keep_alive(self) -> bool:
34+
""":Returns: The keep alive value"""
35+
return self._keep_alive
36+
37+
@keep_alive.setter
38+
def keep_alive(self, value: bool) -> None:
39+
"""Toggles the keep alive value.
40+
41+
:Args:
42+
- value: whether to keep the http connection alive
43+
"""
44+
self._keep_alive = value
45+
46+
@property
47+
def timeout(self) -> int:
48+
""":Returns: The amount of time to wait for an http response."""
49+
return self._timeout
50+
51+
@timeout.setter
52+
def timeout(self, time: int) -> None:
53+
"""Sets the amount of time to wait for an http response.
54+
55+
:Args:
56+
- value: number of seconds to wait for an http response
57+
"""
58+
self._timeout = time
59+
60+
@property
61+
def proxy(self) -> Proxy:
62+
""":Returns: The proxy used for communicating to the driver/server"""
63+
return self._proxy
64+
65+
@proxy.setter
66+
def proxy(self, proxy: Proxy) -> None:
67+
"""Provides the information for communicating with the driver or server.
68+
69+
:Args:
70+
- value: the proxy information to use to communicate with the driver or server
71+
"""
72+
self._proxy = proxy
73+
74+
@property
75+
def certificate_path(self) -> bool:
76+
""":Returns: The path of the .pem encoded certificate
77+
used to verify connection to the driver or server
78+
"""
79+
return self._certificate_path
80+
81+
@certificate_path.setter
82+
def certificate_path(self, path: str) -> None:
83+
"""Set the path to the certificate bundle to verify connection to
84+
command executor. Can also be set to None to disable certificate
85+
validation.
86+
87+
:Args:
88+
- path - path of a .pem encoded certificate chain.
89+
"""
90+
self._certificate_path = path

0 commit comments

Comments
 (0)