Skip to content

Commit 9d49056

Browse files
committed
use Session class for session module
1 parent 485e12b commit 9d49056

File tree

4 files changed

+52
-63
lines changed

4 files changed

+52
-63
lines changed

py/selenium/webdriver/common/bidi/script.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
from dataclasses import dataclass
1919
from typing import List
2020

21-
from .session import session_subscribe
22-
from .session import session_unsubscribe
21+
from .session import Session
2322

2423

2524
class Script:
@@ -43,12 +42,14 @@ def remove_console_message_handler(self, id):
4342

4443
def _subscribe_to_log_entries(self):
4544
if not self.log_entry_subscribed:
46-
self.conn.execute(session_subscribe(LogEntryAdded.event_class))
45+
session = Session(self.conn)
46+
self.conn.execute(session.subscribe(LogEntryAdded.event_class))
4747
self.log_entry_subscribed = True
4848

4949
def _unsubscribe_from_log_entries(self):
5050
if self.log_entry_subscribed and LogEntryAdded.event_class not in self.conn.callbacks:
51-
self.conn.execute(session_unsubscribe(LogEntryAdded.event_class))
51+
session = Session(self.conn)
52+
self.conn.execute(session.unsubscribe(LogEntryAdded.event_class))
5253
self.log_entry_subscribed = False
5354

5455
def _handle_log_entry(self, type, handler):

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

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,43 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
from selenium.webdriver.common.bidi.common import command_builder
1819

19-
def session_subscribe(*events, browsing_contexts=None):
20-
cmd_dict = {
21-
"method": "session.subscribe",
22-
"params": {
23-
"events": events,
24-
},
25-
}
26-
if browsing_contexts is None:
27-
browsing_contexts = []
28-
if browsing_contexts:
29-
cmd_dict["params"]["browsingContexts"] = browsing_contexts
30-
_ = yield cmd_dict
31-
return None
3220

21+
class Session:
22+
23+
def __init__(self, conn):
24+
self.conn = conn
3325

34-
def session_unsubscribe(*events, browsing_contexts=None):
35-
cmd_dict = {
36-
"method": "session.unsubscribe",
37-
"params": {
26+
def subscribe(self, *events, browsing_contexts=None):
27+
params = {
3828
"events": events,
39-
},
40-
}
41-
if browsing_contexts is None:
42-
browsing_contexts = []
43-
if browsing_contexts:
44-
cmd_dict["params"]["browsingContexts"] = browsing_contexts
45-
_ = yield cmd_dict
46-
return None
29+
}
30+
if browsing_contexts is None:
31+
browsing_contexts = []
32+
if browsing_contexts:
33+
params["browsingContexts"] = browsing_contexts
34+
return command_builder("session.subscribe", params)
4735

36+
def unsubscribe(self, *events, browsing_contexts=None):
37+
params = {
38+
"events": events,
39+
}
40+
if browsing_contexts is None:
41+
browsing_contexts = []
42+
if browsing_contexts:
43+
params["browsingContexts"] = browsing_contexts
44+
return command_builder("session.unsubscribe", params)
4845

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.
46+
def status(self):
47+
"""
48+
The session.status command returns information about the remote end's readiness
49+
to create new sessions and may include implementation-specific metadata.
5350
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
51+
Returns
52+
-------
53+
dict
54+
Dictionary containing the ready state (bool), message (str) and metadata
55+
"""
56+
cmd = command_builder("session.status", {})
57+
return self.conn.execute(cmd)

py/selenium/webdriver/remote/webdriver.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from selenium.webdriver.common.bidi.browser import Browser
4545
from selenium.webdriver.common.bidi.network import Network
4646
from selenium.webdriver.common.bidi.script import Script
47+
from selenium.webdriver.common.bidi.session import Session
4748
from selenium.webdriver.common.by import By
4849
from selenium.webdriver.common.options import ArgOptions
4950
from selenium.webdriver.common.options import BaseOptions
@@ -256,6 +257,7 @@ def __init__(
256257
self._script = None
257258
self._network = None
258259
self._browser = None
260+
self._session = None
259261

260262
def __repr__(self):
261263
return f'<{type(self).__module__}.{type(self).__name__} (session="{self.session_id}")>'
@@ -1294,29 +1296,23 @@ def browser(self):
12941296

12951297
return self._browser
12961298

1297-
def get_bidi_session_status(self):
1298-
"""
1299-
Get the session status using WebDriver BiDi.
1300-
Returns information about whether a remote end is in a state
1301-
in which it can create new sessions.
1302-
1303-
Returns:
1304-
-------
1305-
dict
1306-
Dictionary containing the ready state (bool), message (str) and metadata
1299+
@property
1300+
def session(self):
1301+
"""Returns the BiDi session object for the current WebDriver session.
13071302
13081303
Example:
13091304
--------
1310-
>>> status = driver.get_bidi_session_status()
1311-
>>> print(status["ready"])
1312-
>>> print(status["message"])
1305+
>>> driver.session.subscribe()
1306+
>>> driver.session.unsubscribe()
1307+
>>> session = driver.session.status()
13131308
"""
13141309
if not self._websocket_connection:
13151310
self._start_bidi()
13161311

1317-
from selenium.webdriver.common.bidi.session import session_status
1312+
if self._session is None:
1313+
self._session = Session(self._websocket_connection)
13181314

1319-
return self._websocket_connection.execute(session_status())
1315+
return self._session
13201316

13211317
def _get_cdp_details(self):
13221318
import json

py/test/selenium/webdriver/common/bidi_session_tests.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
@pytest.mark.xfail_safari
2424
def test_session_status(driver):
25-
result = driver.get_bidi_session_status()
26-
25+
result = driver.session.status()
2726
assert result is not None
2827
assert "ready" in result
2928
assert "message" in result
@@ -34,7 +33,7 @@ def test_session_status(driver):
3433
@pytest.mark.xfail_safari
3534
def test_session_status_not_closed_with_one_window(driver):
3635
# initial session status
37-
initial_status = driver.get_bidi_session_status()
36+
initial_status = driver.session.status()
3837
assert initial_status is not None
3938

4039
# Open new window and tab
@@ -45,7 +44,7 @@ def test_session_status_not_closed_with_one_window(driver):
4544
driver.close()
4645

4746
# Session should still be active
48-
status_after_closing = driver.get_bidi_session_status()
47+
status_after_closing = driver.session.status()
4948
assert status_after_closing is not None
5049
assert "ready" in status_after_closing
5150
assert "message" in status_after_closing

0 commit comments

Comments
 (0)