Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[py] allow more configuration of http client #12370

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions py/selenium/webdriver/chrome/remote_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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.
import typing

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


class ChromeRemoteConnection(ChromiumRemoteConnection):
browser_name = DesiredCapabilities.CHROME["browserName"]

def __init__(
self,
remote_server_addr: str,
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,
)
17 changes: 7 additions & 10 deletions py/selenium/webdriver/chrome/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +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 selenium.webdriver.common.desired_capabilities import DesiredCapabilities

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

Expand All @@ -29,23 +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__(
DesiredCapabilities.CHROME["browserName"],
"goog",
options,
service,
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
55 changes: 37 additions & 18 deletions py/selenium/webdriver/chromium/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +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 @@ -28,11 +31,13 @@ class ChromiumDriver(RemoteWebDriver):

def __init__(
self,
browser_name,
vendor_prefix,
options: ArgOptions,
service: Service,
keep_alive=True,
browser_name: str = None,
vendor_prefix: str = None,
options: ArgOptions = None,
service: Service = 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 @@ -42,26 +47,41 @@ 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
"""
self.vendor_prefix = vendor_prefix

self.service = service
if browser_name:
warnings.warn(
"browser_name is not necessary when an Options class is used", DeprecationWarning, stacklevel=2
)

self.service = service
self.service.path = DriverFinder.get_path(self.service, options)

self.service.start()

if vendor_prefix:
warnings.warn(
"vendor_prefix is deprecated, use a ChromiumRemoteConnection with command_executor instead",
DeprecationWarning,
stacklevel=2,
)
remote_connection = ChromiumRemoteConnection(
remote_server_addr=self.service.service_url,
browser_name=browser_name,
vendor_prefix=vendor_prefix,
keep_alive=keep_alive,
ignore_proxy=options._ignore_local_proxy,
)

command_executor = remote_connection if remote_connection else self.service.service_url

try:
super().__init__(
command_executor=ChromiumRemoteConnection(
remote_server_addr=self.service.service_url,
browser_name=browser_name,
vendor_prefix=vendor_prefix,
keep_alive=keep_alive,
ignore_proxy=options._ignore_local_proxy,
),
command_executor=command_executor,
options=options,
keep_alive=keep_alive,
client_config=client_config,
)
except Exception:
self.quit()
Expand Down Expand Up @@ -185,8 +205,7 @@ def stop_casting(self, sink_name: str) -> dict:
return self.execute("stopCasting", {"sinkName": sink_name})

def quit(self) -> None:
"""Closes the browser and shuts down the ChromiumDriver executable that
is started when starting the ChromiumDriver."""
"""Ends the driver session and shuts down the ChromiumDriver executable."""
try:
super().quit()
except Exception:
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
41 changes: 41 additions & 0 deletions py/selenium/webdriver/edge/remote_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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.
import typing

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


class EdgeRemoteConnection(ChromiumRemoteConnection):
browser_name = DesiredCapabilities.EDGE["browserName"]

def __init__(
self,
remote_server_addr: str,
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,
)
15 changes: 6 additions & 9 deletions py/selenium/webdriver/edge/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +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 selenium.webdriver.common.desired_capabilities import DesiredCapabilities

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

Expand All @@ -29,7 +30,8 @@ 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 edge driver. Starts the service and
then creates new instance of edge driver.
Expand All @@ -38,14 +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__(
DesiredCapabilities.EDGE["browserName"],
"ms",
options,
service,
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
Loading