Skip to content

Commit

Permalink
[py] implement proxy and keep alive with client config
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jul 14, 2023
1 parent b5b3f08 commit 460cf1a
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 81 deletions.
7 changes: 5 additions & 2 deletions py/selenium/webdriver/chrome/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
from selenium.webdriver.remote.client_config import ClientConfig


class ChromeRemoteConnection(ChromiumRemoteConnection):
Expand All @@ -26,13 +27,15 @@ class ChromeRemoteConnection(ChromiumRemoteConnection):
def __init__(
self,
remote_server_addr: str,
keep_alive: bool = True,
ignore_proxy: typing.Optional[bool] = False,
keep_alive: typing.Optional[bool] = None,
ignore_proxy: typing.Optional[bool] = None,
client_config: typing.Optional[ClientConfig] = None,
) -> None:
super().__init__(
remote_server_addr=remote_server_addr,
vendor_prefix="goog",
browser_name="chrome",
keep_alive=keep_alive,
ignore_proxy=ignore_proxy,
client_config=client_config,
)
10 changes: 7 additions & 3 deletions py/selenium/webdriver/chrome/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import typing

from selenium.webdriver.chromium.webdriver import ChromiumDriver

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

Expand All @@ -28,17 +30,19 @@ def __init__(
self,
options: Options = None,
service: Service = None,
keep_alive: bool = True,
keep_alive: typing.Optional[bool] = None,
client_config: typing.Optional[ClientConfig] = None,
) -> None:
"""Creates a new instance of the chrome driver. Starts the service and
then creates new instance of chrome driver.
:Args:
- options - this takes an instance of ChromeOptions
- service - Service object for handling the browser driver if you need to pass extra details
- keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
- keep_alive - Deprecated: Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
- client_config - configuration values for the http client
"""
service = service if service else Service()
options = options if options else Options()

super().__init__(options=options, service=service, keep_alive=keep_alive)
super().__init__(options=options, service=service, keep_alive=keep_alive, client_config=client_config)
8 changes: 5 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,11 @@ def __init__(
remote_server_addr: str,
vendor_prefix: str,
browser_name: str,
keep_alive: bool = True,
ignore_proxy: typing.Optional[bool] = False,
keep_alive: typing.Optional[bool] = None,
ignore_proxy: typing.Optional[bool] = None,
client_config: typing.Optional[ClientConfig] = None,
) -> None:
super().__init__(remote_server_addr, keep_alive, ignore_proxy=ignore_proxy)
super().__init__(remote_server_addr, keep_alive, ignore_proxy=ignore_proxy, 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: 14 additions & 5 deletions py/selenium/webdriver/chromium/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import typing
import warnings

from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
from selenium.webdriver.common.driver_finder import DriverFinder
from selenium.webdriver.common.options import ArgOptions
from selenium.webdriver.common.service import Service
from selenium.webdriver.remote.client_config import ClientConfig
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver


Expand All @@ -31,10 +33,11 @@ def __init__(
self,
browser_name: str = None,
vendor_prefix: str = None,
options: ArgOptions = ArgOptions(),
options: ArgOptions = None,
service: Service = None,
keep_alive: bool = True,
remote_connection: ChromiumRemoteConnection = None,
keep_alive: typing.Optional[bool] = None,
remote_connection: typing.Optional[ChromiumRemoteConnection] = None,
client_config: typing.Optional[ClientConfig] = None,
) -> None:
"""Creates a new WebDriver instance of the ChromiumDriver. Starts the
service and then creates new WebDriver instance of ChromiumDriver.
Expand All @@ -44,7 +47,8 @@ def __init__(
- vendor_prefix - Company prefix to apply to vendor-specific WebDriver extension commands.
- options - this takes an instance of ChromiumOptions
- service - Service object for handling the browser driver if you need to pass extra details
- keep_alive - Whether to configure ChromiumRemoteConnection to use HTTP keep-alive.
- keep_alive - Deprecated: Whether to configure ChromiumRemoteConnection to use HTTP keep-alive.
- client_config - configuration values for the http client
"""

if browser_name:
Expand Down Expand Up @@ -73,7 +77,12 @@ def __init__(
command_executor = remote_connection if remote_connection else self.service.service_url

try:
super().__init__(command_executor=command_executor, options=options, keep_alive=keep_alive)
super().__init__(
command_executor=command_executor,
options=options,
keep_alive=keep_alive,
client_config=client_config,
)
except Exception:
self.quit()
raise
Expand Down
19 changes: 13 additions & 6 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 All @@ -31,6 +32,7 @@ def __init__(self) -> None:
self._proxy = None
self.set_capability("pageLoadStrategy", "normal")
self.mobile_options = None
self._ignore_local_proxy = False

@property
def capabilities(self):
Expand Down Expand Up @@ -225,12 +227,22 @@ def to_capabilities(self):
def default_capabilities(self):
"""Return minimal capabilities necessary as a dictionary."""

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


class ArgOptions(BaseOptions):
def __init__(self) -> None:
super().__init__()
self._arguments = []
self._ignore_local_proxy = False

@property
def arguments(self):
Expand All @@ -250,11 +262,6 @@ def add_argument(self, argument):
else:
raise ValueError("argument can not be null")

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."""
self._ignore_local_proxy = True

def to_capabilities(self):
return self._caps

Expand Down
7 changes: 5 additions & 2 deletions py/selenium/webdriver/edge/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
from selenium.webdriver.remote.client_config import ClientConfig


class EdgeRemoteConnection(ChromiumRemoteConnection):
Expand All @@ -26,13 +27,15 @@ class EdgeRemoteConnection(ChromiumRemoteConnection):
def __init__(
self,
remote_server_addr: str,
keep_alive: bool = True,
ignore_proxy: typing.Optional[bool] = False,
keep_alive: typing.Optional[bool] = None,
ignore_proxy: typing.Optional[bool] = None,
client_config: typing.Optional[ClientConfig] = None,
) -> None:
super().__init__(
remote_server_addr=remote_server_addr,
vendor_prefix="goog",
browser_name="MicrosoftEdge",
keep_alive=keep_alive,
ignore_proxy=ignore_proxy,
client_config=client_config,
)
8 changes: 6 additions & 2 deletions py/selenium/webdriver/edge/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import typing

from selenium.webdriver.chromium.webdriver import ChromiumDriver

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

Expand All @@ -28,7 +30,8 @@ def __init__(
self,
options: Options = None,
service: Service = None,
keep_alive: bool = True,
keep_alive: typing.Optional[bool] = None,
client_config: typing.Optional[ClientConfig] = None,
) -> None:
"""Creates a new instance of the edge driver. Starts the service and
then creates new instance of edge driver.
Expand All @@ -37,8 +40,9 @@ def __init__(
- options - this takes an instance of EdgeOptions
- 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.
- client_config - configuration values for the http client
"""
service = service if service else Service()
options = options if options else Options()

super().__init__(options=options, service=service, keep_alive=keep_alive)
super().__init__(options=options, service=service, keep_alive=keep_alive, client_config=client_config)
12 changes: 10 additions & 2 deletions py/selenium/webdriver/firefox/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import typing

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,
keep_alive: typing.Optional[bool] = None,
ignore_proxy: typing.Optional[bool] = None,
client_config: typing.Optional[ClientConfig] = None,
) -> None:
super().__init__(remote_server_addr, keep_alive, ignore_proxy=ignore_proxy, client_config=client_config)

self._commands["GET_CONTEXT"] = ("GET", "/session/$sessionId/moz/context")
self._commands["SET_CONTEXT"] = ("POST", "/session/$sessionId/moz/context")
Expand Down
17 changes: 13 additions & 4 deletions py/selenium/webdriver/firefox/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import base64
import logging
import os
import typing
import warnings
import zipfile
from contextlib import contextmanager
Expand All @@ -25,6 +26,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 Service

Expand All @@ -41,15 +43,17 @@ def __init__(
self,
options: Options = None,
service: Service = None,
keep_alive: bool = True,
keep_alive: typing.Optional[bool] = None,
client_config: typing.Optional[ClientConfig] = None,
) -> None:
"""Creates a new instance of the Firefox driver. Starts the service and
then creates new instance of Firefox driver.
:Args:
- options - Instance of ``options.Options``.
- options - (Optional) Instance of ``options.Options``.
- service - (Optional) service instance for managing the starting and stopping of the driver.
- keep_alive - Whether to configure remote_connection.RemoteConnection to use HTTP keep-alive.
- keep_alive - Deprecated: Whether to configure remote_connection.RemoteConnection to use HTTP keep-alive.
- client_config - configuration values for the http client
"""

self.service = service if service else Service()
Expand All @@ -59,7 +63,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
15 changes: 12 additions & 3 deletions py/selenium/webdriver/ie/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import typing

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 Service

Expand All @@ -30,16 +32,18 @@ def __init__(
self,
options: Options = None,
service: Service = None,
keep_alive=True,
keep_alive: typing.Optional[bool] = None,
client_config: typing.Optional[ClientConfig] = None,
) -> None:
"""Creates a new instance of the Ie driver.
Starts the service and then creates new instance of Ie driver.
:Args:
- options - IE Options instance, providing additional IE options
- options - (Optional) IE Options instance, providing additional IE options
- service - (Optional) service instance for managing the starting and stopping of the driver.
- keep_alive - Deprecated: Whether to configure RemoteConnection to use HTTP keep-alive.
- client_config - configuration values for the http client
"""

self.service = service if service else Service()
Expand All @@ -49,7 +53,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
Loading

0 comments on commit 460cf1a

Please sign in to comment.