Skip to content

Commit

Permalink
[py] use ClientConfig for keep_alive, proxy, timeout and certificate …
Browse files Browse the repository at this point in the history
…path settings
  • Loading branch information
titusfortner committed May 14, 2023
1 parent 44ea09a commit c0903b6
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 103 deletions.
12 changes: 4 additions & 8 deletions py/selenium/webdriver/chrome/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.driver_finder import DriverFinder

from ..remote.client_config import ClientConfig
from .options import Options
from .service import DEFAULT_EXECUTABLE_PATH
from .service import Service

DEFAULT_PORT = 0
DEFAULT_SERVICE_LOG_PATH = None
DEFAULT_KEEP_ALIVE = None


class WebDriver(ChromiumDriver):
Expand All @@ -46,7 +46,8 @@ def __init__(
service_log_path=DEFAULT_SERVICE_LOG_PATH,
chrome_options=None,
service: Service = None,
keep_alive=DEFAULT_KEEP_ALIVE,
keep_alive=None,
client_config: ClientConfig = ClientConfig(),
) -> None:
"""Creates a new instance of the chrome driver. Starts the service and
then creates new instance of chrome driver.
Expand All @@ -69,12 +70,6 @@ def __init__(
if chrome_options:
warnings.warn("use options instead of chrome_options", DeprecationWarning, stacklevel=2)
options = chrome_options
if keep_alive != DEFAULT_KEEP_ALIVE:
warnings.warn(
"keep_alive has been deprecated, please pass in a Service object", DeprecationWarning, stacklevel=2
)
else:
keep_alive = True
if not options:
options = self.create_options()
if not service:
Expand All @@ -91,6 +86,7 @@ def __init__(
service_log_path,
service,
keep_alive,
client_config,
)

def create_options(self) -> Options:
Expand Down
6 changes: 3 additions & 3 deletions py/selenium/webdriver/chromium/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.
import typing

from selenium.webdriver.remote.client_config import ClientConfig
from selenium.webdriver.remote.remote_connection import RemoteConnection


Expand All @@ -25,10 +26,9 @@ def __init__(
remote_server_addr: str,
vendor_prefix: str,
browser_name: str,
keep_alive: bool = True,
ignore_proxy: typing.Optional[bool] = False,
client_config: ClientConfig = ClientConfig(),
) -> None:
super().__init__(remote_server_addr, keep_alive, ignore_proxy=ignore_proxy)
super().__init__(remote_server_addr, client_config=client_config)
self.browser_name = browser_name
self._commands["launchApp"] = ("POST", "/session/$sessionId/chromium/launch_app")
self._commands["setPermissions"] = ("POST", "/session/$sessionId/permissions")
Expand Down
19 changes: 9 additions & 10 deletions py/selenium/webdriver/chromium/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
import warnings

from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
from selenium.webdriver.common.options import BaseOptions
from selenium.webdriver.common.service import Service
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.remote.client_config import ClientConfig
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver

DEFAULT_PORT = 0
DEFAULT_SERVICE_LOG_PATH = None
DEFAULT_KEEP_ALIVE = None


class ChromiumDriver(RemoteWebDriver):
Expand All @@ -43,7 +42,8 @@ def __init__(
desired_capabilities=None,
service_log_path=DEFAULT_SERVICE_LOG_PATH,
service: Service = None,
keep_alive=DEFAULT_KEEP_ALIVE,
keep_alive=None,
client_config: ClientConfig = ClientConfig(),
) -> None:
"""Creates a new WebDriver instance of the ChromiumDriver. Starts the
service and then creates new WebDriver instance of ChromiumDriver.
Expand Down Expand Up @@ -74,12 +74,6 @@ def __init__(
DeprecationWarning,
stacklevel=2,
)
if keep_alive != DEFAULT_KEEP_ALIVE and type(self) == __class__:
warnings.warn(
"keep_alive has been deprecated, please pass in a Service object", DeprecationWarning, stacklevel=2
)
else:
keep_alive = True

self.vendor_prefix = vendor_prefix

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

try:
super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive,)
super().__init__(
command_executor=self.service.service_url,
options=options,
keep_alive=keep_alive,
client_config=client_config,
)
except Exception:
self.quit()
raise
Expand Down
7 changes: 7 additions & 0 deletions py/selenium/webdriver/common/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
import typing
import warnings
from abc import ABCMeta
from abc import abstractmethod

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

def to_capabilities(self):
Expand Down
7 changes: 5 additions & 2 deletions py/selenium/webdriver/edge/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.driver_finder import DriverFinder

from ..remote.client_config import ClientConfig
from .options import Options
from .service import DEFAULT_EXECUTABLE_PATH
from .service import Service
Expand All @@ -44,7 +45,8 @@ def __init__(
capabilities=None,
service_log_path=DEFAULT_SERVICE_LOG_PATH,
service: Service = None,
keep_alive=False,
keep_alive=None,
client_config: ClientConfig = ClientConfig(),
verbose=False, # Todo: Why is this now unused?
) -> None:
"""Creates a new instance of the edge driver. Starts the service and
Expand All @@ -59,7 +61,7 @@ def __init__(
capabilities only, such as "proxy" or "loggingPref".
- service_log_path - Deprecated: Where to log information from the driver.
- service - Service object for handling the browser driver if you need to pass extra details
- keep_alive - Whether to configure EdgeRemoteConnection to use HTTP keep-alive.
- keep_alive - Deprecated: Whether to configure EdgeRemoteConnection to use HTTP keep-alive.
- verbose - whether to set verbose logging in the service.
"""
if executable_path != "msedgedriver":
Expand All @@ -83,6 +85,7 @@ def __init__(
service_log_path,
service,
keep_alive,
client_config,
)

def create_options(self) -> Options:
Expand Down
5 changes: 3 additions & 2 deletions py/selenium/webdriver/firefox/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
# under the License.

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.remote.client_config import ClientConfig
from selenium.webdriver.remote.remote_connection import RemoteConnection


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

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

self._commands["GET_CONTEXT"] = ("GET", "/session/$sessionId/moz/context")
self._commands["SET_CONTEXT"] = ("POST", "/session/$sessionId/moz/context")
Expand Down
14 changes: 10 additions & 4 deletions py/selenium/webdriver/firefox/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
from selenium.webdriver.common.driver_finder import DriverFinder
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver

from ..remote.client_config import ClientConfig
from .firefox_binary import FirefoxBinary
from .firefox_profile import FirefoxProfile
from .options import Options
from .remote_connection import FirefoxRemoteConnection
from .service import DEFAULT_EXECUTABLE_PATH
from .service import Service

Expand Down Expand Up @@ -58,7 +58,8 @@ def __init__(
service=None,
desired_capabilities=None,
log_path=DEFAULT_LOG_PATH,
keep_alive=True, # Todo: Why is this now unused?
keep_alive=None,
client_config: ClientConfig = ClientConfig(),
) -> None:
"""Starts a new local session of Firefox.
Expand Down Expand Up @@ -106,7 +107,7 @@ def __init__(
:param desired_capabilities: Deprecated: alias of capabilities. In future
versions of this library, this will replace 'capabilities'.
This will make the signature consistent with RemoteWebDriver.
:param keep_alive: Whether to configure remote_connection.RemoteConnection to use
:param keep_alive - Deprecated: Whether to configure remote_connection.RemoteConnection to use
HTTP keep-alive.
"""

Expand Down Expand Up @@ -195,7 +196,12 @@ def __init__(
self.service.path = DriverFinder.get_path(self.service, options)
self.service.start()

super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive)
super().__init__(
command_executor=self.service.service_url,
options=options,
keep_alive=keep_alive,
client_config=client_config,
)

self._is_remote = False

Expand Down
18 changes: 9 additions & 9 deletions py/selenium/webdriver/ie/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from selenium.webdriver.common.driver_finder import DriverFinder
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver

from ..remote.client_config import ClientConfig
from .options import Options
from .service import DEFAULT_EXECUTABLE_PATH
from .service import Service
Expand All @@ -30,7 +31,6 @@
DEFAULT_HOST = None
DEFAULT_LOG_LEVEL = None
DEFAULT_SERVICE_LOG_PATH = None
DEFAULT_KEEP_ALIVE = None


class WebDriver(RemoteWebDriver):
Expand All @@ -49,7 +49,8 @@ def __init__(
options: Options = None,
service: Service = None,
desired_capabilities=None,
keep_alive=DEFAULT_KEEP_ALIVE,
keep_alive=None,
client_config: ClientConfig = ClientConfig(),
) -> None:
"""Creates a new instance of the Ie driver.
Expand Down Expand Up @@ -102,12 +103,6 @@ def __init__(
DeprecationWarning,
stacklevel=2,
)
if keep_alive != DEFAULT_KEEP_ALIVE:
warnings.warn(
"keep_alive has been deprecated, please pass in a Service object", DeprecationWarning, stacklevel=2
)
else:
keep_alive = True

self.host = host
self.port = port
Expand All @@ -127,7 +122,12 @@ def __init__(
self.iedriver.path = DriverFinder.get_path(self.iedriver, options)
self.iedriver.start()

super().__init__(command_executor=self.iedriver.service_url, options=options, keep_alive=keep_alive)
super().__init__(
command_executor=self.iedriver.service_url,
options=options,
keep_alive=keep_alive,
client_config=client_config,
)
self._is_remote = False

def quit(self) -> None:
Expand Down
90 changes: 90 additions & 0 deletions py/selenium/webdriver/remote/client_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType


class ClientConfig:

def __init__(
self, keep_alive: bool = True, timeout=None, proxy=Proxy({"proxyType": ProxyType.SYSTEM}), certificate_path=None
) -> None:
self._keep_alive = keep_alive
self._timeout = timeout
self._proxy = proxy
self._certificate_path = certificate_path

@property
def keep_alive(self) -> bool:
""":Returns: The keep alive value"""
return self._keep_alive

@keep_alive.setter
def keep_alive(self, value: bool) -> None:
"""Toggles the keep alive value.
:Args:
- value: whether to keep the http connection alive
"""
self._keep_alive = value

@property
def timeout(self) -> int:
""":Returns: The amount of time to wait for an http response."""
return self._timeout

@timeout.setter
def timeout(self, time: int) -> None:
"""Sets the amount of time to wait for an http response.
:Args:
- value: number of seconds to wait for an http response
"""
self._timeout = time

@property
def proxy(self) -> Proxy:
""":Returns: The proxy used for communicating to the driver/server"""
return self._proxy

@proxy.setter
def proxy(self, proxy: Proxy) -> None:
"""Provides the information for communicating with the driver or server.
:Args:
- value: the proxy information to use to communicate with the driver or server
"""
self._proxy = proxy

@property
def certificate_path(self) -> bool:
""":Returns: The path of the .pem encoded certificate
used to verify connection to the driver or server
"""
return self._certificate_path

@certificate_path.setter
def certificate_path(self, path: str) -> None:
"""Set the path to the certificate bundle to verify connection to
command executor. Can also be set to None to disable certificate
validation.
:Args:
- path - path of a .pem encoded certificate chain.
"""
self._certificate_path = path
Loading

0 comments on commit c0903b6

Please sign in to comment.