Skip to content
Open
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
17 changes: 12 additions & 5 deletions py/selenium/webdriver/remote/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
)
from selenium.webdriver.support.relative_locator import RelativeBy

from ..common.service import Service
from .bidi_connection import BidiConnection
from .client_config import ClientConfig
from .command import Command
Expand Down Expand Up @@ -198,7 +199,7 @@ class WebDriver(BaseWebDriver):

def __init__(
self,
command_executor: Union[str, RemoteConnection] = "http://127.0.0.1:4444",
command_executor: Union[str, RemoteConnection, Service] = "http://127.0.0.1:4444",
keep_alive: bool = True,
file_detector: Optional[FileDetector] = None,
options: Optional[Union[BaseOptions, list[BaseOptions]]] = None,
Expand All @@ -211,9 +212,10 @@ def __init__(

Parameters:
-----------
command_executor : str or remote_connection.RemoteConnection
- Either a string representing the URL of the remote server or a custom
remote_connection.RemoteConnection object. Defaults to 'http://127.0.0.1:4444/wd/hub'.
command_executor : str or remote_connection.RemoteConnection or service.Service
- Either a string representing the URL of the remote server, a Service (containing
the URL of the remote server), or a custom remote_connection.RemoteConnection object.
Defaults to 'http://127.0.0.1:4444/wd/hub'.
keep_alive : bool (Deprecated)
- Whether to configure remote_connection.RemoteConnection to use HTTP keep-alive. Defaults to True.
file_detector : object or None
Expand All @@ -240,10 +242,15 @@ def __init__(
capabilities = options.to_capabilities()
_ignore_local_proxy = options._ignore_local_proxy
self.command_executor = command_executor
if isinstance(self.command_executor, Service):
# Make sure the service doesn't drop before this drops.
# We don't use it, so it shouldn't be defined in types.
self._service = self.command_executor # type: ignore[attr-defined]
self.command_executor = self.command_executor.service_url
if isinstance(self.command_executor, (str, bytes)):
self.command_executor = get_remote_connection(
capabilities,
command_executor=command_executor,
command_executor=self.command_executor,
keep_alive=keep_alive,
ignore_local_proxy=_ignore_local_proxy,
client_config=client_config,
Expand Down
52 changes: 52 additions & 0 deletions py/test/unit/selenium/webdriver/remote/init_with_service_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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 gc

from selenium.webdriver import ChromeOptions, Remote
from selenium.webdriver.common.service import Service


class TestService(Service):
service_url = "foo"

def command_line_args(self) -> list[str]:
raise NotImplementedError

def __init__(self, deleted_flag):
self.deleted_flag = deleted_flag

def __del__(self):
self.deleted_flag["deleted"] = True


def test_init_with_service(mocker):
mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.execute")
deleted_flag = {"deleted": False}
service = TestService(deleted_flag)
options = ChromeOptions()
remote = Remote(service, options=options)
assert remote.command_executor.client_config.remote_server_addr == "foo"
del service
gc.collect()
# Even after deleting the local reference, the Service is not GC'ed
assert not deleted_flag["deleted"]
del remote
gc.collect()
# After deleting the Remote, the Service is GC'ed
assert deleted_flag["deleted"]