Skip to content

Commit 1cb1bb6

Browse files
committed
add bidi command session status
1 parent 78ffa20 commit 1cb1bb6

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

py/selenium/webdriver/common/bidi/session.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,21 @@ def session_unsubscribe(*events, browsing_contexts=None):
4444
cmd_dict["params"]["browsingContexts"] = browsing_contexts
4545
_ = yield cmd_dict
4646
return None
47+
48+
49+
def session_status():
50+
"""
51+
The session.status command returns information about the remote end's readiness
52+
to create new sessions and may include implementation-specific metadata.
53+
54+
Returns
55+
-------
56+
dict
57+
Dictionary containing the ready state (bool), message (str) and metadata
58+
"""
59+
cmd_dict = {
60+
"method": "session.status",
61+
"params": {},
62+
}
63+
result = yield cmd_dict
64+
return result

py/selenium/webdriver/remote/webdriver.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,30 @@ def network(self):
12691269

12701270
return self._network
12711271

1272+
def get_bidi_session_status(self):
1273+
"""
1274+
Get the session status using WebDriver BiDi.
1275+
Returns information about whether a remote end is in a state
1276+
in which it can create new sessions.
1277+
1278+
Returns:
1279+
-------
1280+
dict
1281+
Dictionary containing the ready state (bool), message (str) and metadata
1282+
1283+
Example:
1284+
--------
1285+
>>> status = driver.get_bidi_session_status()
1286+
>>> print(status["ready"])
1287+
>>> print(status["message"])
1288+
"""
1289+
if not self._websocket_connection:
1290+
self._start_bidi()
1291+
1292+
from selenium.webdriver.common.bidi.session import session_status
1293+
1294+
return self._websocket_connection.execute(session_status())
1295+
12721296
def _get_cdp_details(self):
12731297
import json
12741298

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import pytest
19+
20+
from selenium.webdriver.common.window import WindowTypes
21+
22+
23+
@pytest.mark.xfail_safari
24+
def test_session_status(driver):
25+
result = driver.get_bidi_session_status()
26+
27+
assert result is not None
28+
assert "ready" in result
29+
assert "message" in result
30+
assert isinstance(result["ready"], bool)
31+
assert isinstance(result["message"], str)
32+
33+
34+
@pytest.mark.xfail_safari
35+
def test_session_status_not_closed_with_one_window(driver):
36+
# initial session status
37+
initial_status = driver.get_bidi_session_status()
38+
assert initial_status is not None
39+
40+
# Open new window and tab
41+
driver.switch_to.new_window(WindowTypes.WINDOW)
42+
driver.switch_to.new_window(WindowTypes.TAB)
43+
44+
# Close one window
45+
driver.close()
46+
47+
# Session should still be active
48+
status_after_closing = driver.get_bidi_session_status()
49+
assert status_after_closing is not None
50+
assert "ready" in status_after_closing
51+
assert "message" in status_after_closing

0 commit comments

Comments
 (0)