Skip to content

Commit 0f251c5

Browse files
committed
[py] Configure WebSocket timeout and wait interval via ClientConfig
1 parent f8c356f commit 0f251c5

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

py/selenium/webdriver/remote/client_config.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,19 @@ class ClientConfig:
5050
keep_alive = _ClientConfigDescriptor("_keep_alive")
5151
"""Gets and Sets Keep Alive value."""
5252
proxy = _ClientConfigDescriptor("_proxy")
53-
"""Gets and Sets the proxy used for communicating to the driver/server."""
53+
"""Gets and Sets the proxy used for communicating with the driver/server."""
5454
ignore_certificates = _ClientConfigDescriptor("_ignore_certificates")
5555
"""Gets and Sets the ignore certificate check value."""
5656
init_args_for_pool_manager = _ClientConfigDescriptor("_init_args_for_pool_manager")
5757
"""Gets and Sets the ignore certificate check."""
5858
timeout = _ClientConfigDescriptor("_timeout")
59-
"""Gets and Sets the timeout (in seconds) used for communicating to the
60-
driver/server."""
59+
"""Gets and Sets the timeout (in seconds) used for communicating with the driver/server."""
6160
ca_certs = _ClientConfigDescriptor("_ca_certs")
6261
"""Gets and Sets the path to bundle of CA certificates."""
6362
username = _ClientConfigDescriptor("_username")
64-
"""Gets and Sets the username used for basic authentication to the
65-
remote."""
63+
"""Gets and Sets the username used for basic authentication to the remote."""
6664
password = _ClientConfigDescriptor("_password")
67-
"""Gets and Sets the password used for basic authentication to the
68-
remote."""
65+
"""Gets and Sets the password used for basic authentication to the remote."""
6966
auth_type = _ClientConfigDescriptor("_auth_type")
7067
"""Gets and Sets the type of authentication to the remote server."""
7168
token = _ClientConfigDescriptor("_token")
@@ -74,6 +71,10 @@ class ClientConfig:
7471
"""Gets and Sets user agent to be added to the request headers."""
7572
extra_headers = _ClientConfigDescriptor("_extra_headers")
7673
"""Gets and Sets extra headers to be added to the request."""
74+
websocket_timeout = _ClientConfigDescriptor("_websocket_timeout")
75+
"""Gets and Sets the WebSocket response wait timeout (in seconds) used for communicating with the browser."""
76+
websocket_interval = _ClientConfigDescriptor("_websocket_interval")
77+
"""Gets and Sets the WebSocket response wait interval (in seconds) used for communicating with the browser."""
7778

7879
def __init__(
7980
self,
@@ -90,6 +91,8 @@ def __init__(
9091
token: Optional[str] = None,
9192
user_agent: Optional[str] = None,
9293
extra_headers: Optional[dict] = None,
94+
websocket_timeout: Optional[float] = 30.0,
95+
websocket_interval: Optional[float] = 0.1,
9396
) -> None:
9497
self.remote_server_addr = remote_server_addr
9598
self.keep_alive = keep_alive
@@ -103,6 +106,8 @@ def __init__(
103106
self.token = token
104107
self.user_agent = user_agent
105108
self.extra_headers = extra_headers
109+
self.websocket_timeout = websocket_timeout
110+
self.websocket_interval = websocket_interval
106111

107112
self.ca_certs = (
108113
(os.getenv("REQUESTS_CA_BUNDLE") if "REQUESTS_CA_BUNDLE" in os.environ else certifi.where())

py/selenium/webdriver/remote/webdriver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
1718
"""The WebDriver implementation."""
1819

1920
import base64
@@ -1211,7 +1212,9 @@ def start_devtools(self):
12111212
return self._devtools, self._websocket_connection
12121213
if self.caps["browserName"].lower() == "firefox":
12131214
raise RuntimeError("CDP support for Firefox has been removed. Please switch to WebDriver BiDi.")
1214-
self._websocket_connection = WebSocketConnection(ws_url)
1215+
self._websocket_connection = WebSocketConnection(
1216+
ws_url, self.client_config.websocket_timeout, self.client_config.websocket_interval
1217+
)
12151218
targets = self._websocket_connection.execute(self._devtools.target.get_targets())
12161219
for target in targets:
12171220
if target.target_id == self.current_window_handle:
@@ -1260,7 +1263,9 @@ def _start_bidi(self):
12601263
else:
12611264
raise WebDriverException("Unable to find url to connect to from capabilities")
12621265

1263-
self._websocket_connection = WebSocketConnection(ws_url)
1266+
self._websocket_connection = WebSocketConnection(
1267+
ws_url, self.client_config.websocket_timeout, self.client_config.websocket_interval
1268+
)
12641269

12651270
@property
12661271
def network(self):

py/selenium/webdriver/remote/websocket_connection.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
1718
import json
1819
import logging
1920
from ssl import CERT_NONE
@@ -28,15 +29,14 @@
2829

2930

3031
class WebSocketConnection:
31-
_response_wait_timeout = 30
32-
_response_wait_interval = 0.1
33-
3432
_max_log_message_size = 9999
3533

36-
def __init__(self, url):
34+
def __init__(self, url, timeout, interval):
3735
self.callbacks = {}
3836
self.session_id = None
3937
self.url = url
38+
self.response_wait_timeout = timeout
39+
self.response_wait_interval = interval
4040

4141
self._id = 0
4242
self._messages = {}
@@ -46,7 +46,7 @@ def __init__(self, url):
4646
self._wait_until(lambda: self._started)
4747

4848
def close(self):
49-
self._ws_thread.join(timeout=self._response_wait_timeout)
49+
self._ws_thread.join(timeout=self.response_wait_timeout)
5050
self._ws.close()
5151
self._started = False
5252
self._ws = None
@@ -142,8 +142,8 @@ def _process_message(self, message):
142142
Thread(target=callback, args=(params,)).start()
143143

144144
def _wait_until(self, condition):
145-
timeout = self._response_wait_timeout
146-
interval = self._response_wait_interval
145+
timeout = self.response_wait_timeout
146+
interval = self.response_wait_interval
147147

148148
while timeout > 0:
149149
result = condition()

0 commit comments

Comments
 (0)