Skip to content

Commit fb9ecba

Browse files
committed
Remove from_service, and move functionality to __init__
1 parent 0614ad4 commit fb9ecba

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

py/selenium/webdriver/remote/webdriver.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class WebDriver(BaseWebDriver):
201201

202202
def __init__(
203203
self,
204-
command_executor: Union[str, RemoteConnection] = "http://127.0.0.1:4444",
204+
command_executor: Union[str, RemoteConnection, Service] = "http://127.0.0.1:4444",
205205
keep_alive: bool = True,
206206
file_detector: Optional[FileDetector] = None,
207207
options: Optional[Union[BaseOptions, list[BaseOptions]]] = None,
@@ -214,9 +214,10 @@ def __init__(
214214
215215
Parameters:
216216
-----------
217-
command_executor : str or remote_connection.RemoteConnection
218-
- Either a string representing the URL of the remote server or a custom
219-
remote_connection.RemoteConnection object. Defaults to 'http://127.0.0.1:4444/wd/hub'.
217+
command_executor : str or remote_connection.RemoteConnection or service.Service
218+
- Either a string representing the URL of the remote server, a Service (containing
219+
the URL of the remote server), or a custom remote_connection.RemoteConnection object.
220+
Defaults to 'http://127.0.0.1:4444/wd/hub'.
220221
keep_alive : bool (Deprecated)
221222
- Whether to configure remote_connection.RemoteConnection to use HTTP keep-alive. Defaults to True.
222223
file_detector : object or None
@@ -243,10 +244,15 @@ def __init__(
243244
capabilities = options.to_capabilities()
244245
_ignore_local_proxy = options._ignore_local_proxy
245246
self.command_executor = command_executor
247+
if isinstance(self.command_executor, Service):
248+
# Make sure the service doesn't drop before this drops.
249+
# We don't use it, so it shouldn't be defined in types.
250+
self._service = self.command_executor # type: ignore[attr-defined]
251+
self.command_executor = self.command_executor.service_url
246252
if isinstance(self.command_executor, (str, bytes)):
247253
self.command_executor = get_remote_connection(
248254
capabilities,
249-
command_executor=command_executor,
255+
command_executor=self.command_executor,
250256
keep_alive=keep_alive,
251257
ignore_local_proxy=_ignore_local_proxy,
252258
client_config=client_config,
@@ -279,14 +285,6 @@ def __init__(
279285
self._input = None
280286
self._devtools = None
281287

282-
@classmethod
283-
def from_service(cls, service: Service, *args, **kwargs) -> Self:
284-
self = cls(service.service_url, *args, **kwargs)
285-
# Make sure the service doesn't drop before this drops.
286-
# We don't use it, so it shouldn't be defined in types.
287-
self._service = service # type: ignore[attr-defined]
288-
return self
289-
290288
def __repr__(self):
291289
return f'<{type(self).__module__}.{type(self).__name__} (session="{self.session_id}")>'
292290

py/test/unit/selenium/webdriver/remote/from_server_tests.py renamed to py/test/unit/selenium/webdriver/remote/init_with_service_tests.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,37 @@
1616
# under the License.
1717

1818

19-
from selenium.webdriver import Remote
19+
import gc
2020

21+
from selenium.webdriver import ChromeOptions, Remote
22+
from selenium.webdriver.common.service import Service
2123

22-
class TestService:
24+
25+
class TestService(Service):
2326
service_url = "foo"
2427

28+
def command_line_args(self) -> list[str]:
29+
raise NotImplementedError
30+
2531
def __init__(self, deleted_flag):
2632
self.deleted_flag = deleted_flag
2733

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

3137

32-
class TestRemote(Remote):
33-
def __init__(self, command_executor, arg, kwarg):
34-
assert command_executor == "foo"
35-
assert arg == "arg_value"
36-
assert kwarg == "kwarg_value"
37-
38-
39-
def test_from_service(mocker):
38+
def test_init_with_service(mocker):
39+
mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.execute")
4040
deleted_flag = {"deleted": False}
4141
service = TestService(deleted_flag)
42-
remote = TestRemote.from_service(service, "arg_value", kwarg="kwarg_value")
42+
options = ChromeOptions()
43+
remote = Remote(service, options=options)
44+
assert remote.command_executor.client_config.remote_server_addr == "foo"
4345
del service
46+
gc.collect()
4447
# Even after deleting the local reference, the Service is not GC'ed
4548
assert not deleted_flag["deleted"]
4649
del remote
50+
gc.collect()
4751
# After deleting the Remote, the Service is GC'ed
4852
assert deleted_flag["deleted"]

0 commit comments

Comments
 (0)