Skip to content

Commit

Permalink
[py] have superclass always create remote connection
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jul 14, 2023
1 parent f41cf06 commit b5b3f08
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 72 deletions.
11 changes: 10 additions & 1 deletion py/selenium/webdriver/chrome/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@
# under the License.
import typing

from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection


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

def __init__(
self,
remote_server_addr: str,
keep_alive: bool = True,
ignore_proxy: typing.Optional[bool] = False,
) -> None:
super().__init__(remote_server_addr, 'goog', keep_alive, ignore_proxy=ignore_proxy)
super().__init__(
remote_server_addr=remote_server_addr,
vendor_prefix="goog",
browser_name="chrome",
keep_alive=keep_alive,
ignore_proxy=ignore_proxy,
)
9 changes: 1 addition & 8 deletions py/selenium/webdriver/chrome/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# under the License.

from selenium.webdriver.chromium.webdriver import ChromiumDriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

from .options import Options
from .service import Service
Expand All @@ -42,10 +41,4 @@ def __init__(
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)
50 changes: 30 additions & 20 deletions py/selenium/webdriver/chromium/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import warnings

from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
from selenium.webdriver.common.driver_finder import DriverFinder
Expand All @@ -28,11 +29,12 @@ 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 = ArgOptions(),
service: Service = None,
keep_alive: bool = True,
remote_connection: ChromiumRemoteConnection = None,
) -> None:
"""Creates a new WebDriver instance of the ChromiumDriver. Starts the
service and then creates new WebDriver instance of ChromiumDriver.
Expand All @@ -44,25 +46,34 @@ def __init__(
- 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.
"""
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()

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,
),
options=options,
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=command_executor, options=options, keep_alive=keep_alive)
except Exception:
self.quit()
raise
Expand Down Expand Up @@ -185,8 +196,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
11 changes: 10 additions & 1 deletion py/selenium/webdriver/edge/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@
# under the License.
import typing

from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection


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

def __init__(
self,
remote_server_addr: str,
keep_alive: bool = True,
ignore_proxy: typing.Optional[bool] = False,
) -> None:
super().__init__(remote_server_addr, 'ms', keep_alive, ignore_proxy=ignore_proxy)
super().__init__(
remote_server_addr=remote_server_addr,
vendor_prefix="goog",
browser_name="MicrosoftEdge",
keep_alive=keep_alive,
ignore_proxy=ignore_proxy,
)
11 changes: 2 additions & 9 deletions py/selenium/webdriver/edge/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# under the License.

from selenium.webdriver.chromium.webdriver import ChromiumDriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

from .options import Options
from .service import Service
Expand All @@ -29,7 +28,7 @@ def __init__(
self,
options: Options = None,
service: Service = None,
keep_alive=True,
keep_alive: bool = True,
) -> None:
"""Creates a new instance of the edge driver. Starts the service and
then creates new instance of edge driver.
Expand All @@ -42,10 +41,4 @@ def __init__(
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)
20 changes: 9 additions & 11 deletions py/selenium/webdriver/firefox/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver

from .options import Options
from .remote_connection import FirefoxRemoteConnection
from .service import Service

logger = logging.getLogger(__name__)
Expand All @@ -42,7 +41,7 @@ def __init__(
self,
options: Options = None,
service: Service = None,
keep_alive=True,
keep_alive: bool = True,
) -> None:
"""Creates a new instance of the Firefox driver. Starts the service and
then creates new instance of Firefox driver.
Expand All @@ -59,24 +58,23 @@ def __init__(
self.service.path = DriverFinder.get_path(self.service, options)
self.service.start()

executor = FirefoxRemoteConnection(
remote_server_addr=self.service.service_url,
ignore_proxy=options._ignore_local_proxy,
keep_alive=keep_alive,
)
super().__init__(command_executor=executor, options=options)
try:
super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive)
except Exception:
self.quit()
raise

self._is_remote = False

def quit(self) -> None:
"""Quits the driver and close every associated window."""
"""Ends the driver session and shuts down the geckodriver executable."""
try:
super().quit()
except Exception:
# We don't care about the message because something probably has gone wrong
pass

self.service.stop()
finally:
self.service.stop()

def set_context(self, context) -> None:
self.execute("SET_CONTEXT", {"context": context})
Expand Down
24 changes: 14 additions & 10 deletions py/selenium/webdriver/ie/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# under the License.

from selenium.webdriver.common.driver_finder import DriverFinder
from selenium.webdriver.remote.remote_connection import RemoteConnection
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver

from .options import Options
Expand Down Expand Up @@ -49,15 +48,20 @@ def __init__(
self.service.path = DriverFinder.get_path(self.service, options)
self.service.start()

executor = RemoteConnection(
remote_server_addr=self.service.service_url,
keep_alive=keep_alive,
ignore_proxy=options._ignore_local_proxy,
)
try:
super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive)
except Exception:
self.quit()
raise

super().__init__(command_executor=executor, options=options)
self._is_remote = False

def quit(self) -> None:
super().quit()
self.service.stop()
def quit(self):
"""Ends the driver session and shuts down the safaridriver executable if necessary."""
try:
super().quit()
except Exception:
# We don't care about the message because something probably has gone wrong
pass
finally:
self.service.stop()
20 changes: 8 additions & 12 deletions py/selenium/webdriver/safari/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# specific language governing permissions and limitations
# under the License.

import http.client as http_client
import warnings

from selenium.common.exceptions import WebDriverException
Expand All @@ -24,7 +23,6 @@

from ..common.driver_finder import DriverFinder
from .options import Options
from .remote_connection import SafariRemoteConnection
from .service import Service

DEFAULT_SAFARI_CAPS = DesiredCapabilities.SAFARI.copy()
Expand Down Expand Up @@ -66,22 +64,20 @@ def __init__(
if not self._reuse_service:
self.service.start()

executor = SafariRemoteConnection(
remote_server_addr=self.service.service_url,
keep_alive=keep_alive,
ignore_proxy=options._ignore_local_proxy,
)

super().__init__(command_executor=executor, options=options)
try:
super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive)
except Exception:
self.quit()
raise

self._is_remote = False

def quit(self):
"""Closes the browser and shuts down the SafariDriver executable that
is started when starting the SafariDriver."""
"""Ends the driver session and shuts down the safaridriver executable if necessary."""
try:
super().quit()
except http_client.BadStatusLine:
except Exception:
# We don't care about the message because something probably has gone wrong
pass
finally:
if not self._reuse_service:
Expand Down

0 comments on commit b5b3f08

Please sign in to comment.