Skip to content

Commit 41b243c

Browse files
[wdspec] remove unsubscribe by contexts (#54868)
Co-authored-by: Maksim Sadym <sadym@chromium.org>
1 parent 466c28b commit 41b243c

File tree

6 files changed

+10
-318
lines changed

6 files changed

+10
-318
lines changed

tools/webdriver/webdriver/bidi/modules/session.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ def subscribe(self,
4444
@command
4545
def unsubscribe(self,
4646
events: Optional[List[str]] = None,
47-
contexts: Optional[List[str]] = None,
4847
subscriptions: Optional[List[str]] = None) -> Mapping[str, Any]:
4948
params: MutableMapping[str, Any] = {}
50-
if contexts is not None:
51-
params["contexts"] = contexts
5249
if events is not None:
5350
params["events"] = events
5451
if subscriptions is not None:

tools/wptrunner/wptrunner/executors/executorwebdriver.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
DisplayFeaturesProtocolPart,
5151
merge_dicts)
5252

53-
from typing import Any, List, Dict, Optional, Tuple
53+
from typing import Any, List, Dict, Optional
5454
from webdriver.client import Session
5555
from webdriver import error as webdriver_error
5656
from webdriver.bidi import error as webdriver_bidi_error
@@ -273,7 +273,7 @@ async def handle_user_prompt(self,
273273

274274

275275
class WebDriverBidiEventsProtocolPart(BidiEventsProtocolPart):
276-
_subscriptions: List[Tuple[List[str], Optional[List[str]]]] = []
276+
_subscriptions: List[str] = []
277277

278278
def __init__(self, parent):
279279
super().__init__(parent)
@@ -312,15 +312,10 @@ async def _get_top_context(self, context: str) -> Optional[str]:
312312

313313
async def subscribe(self, events, contexts):
314314
self.logger.info("Subscribing to events %s in %s" % (events, contexts))
315-
# The BiDi subscriptions are done for top context even if the sub-context is provided. We need to get the
316-
# top-level contexts list to handle the scenario when subscription is done for a sub-context which is closed
317-
# afterwards. However, the subscription itself is done for the provided contexts in order to throw in case of
318-
# the sub-context is removed.
319-
top_contexts = await self._contexts_to_top_contexts(contexts)
320315
result = await self.webdriver.bidi_session.session.subscribe(events=events, contexts=contexts)
321316
# The `subscribe` method either raises an exception or adds subscription. The command is atomic, meaning in case
322317
# of exception no subscription is added.
323-
self._subscriptions.append((events, top_contexts))
318+
self._subscriptions.append(result["subscription"])
324319
return result
325320

326321
async def unsubscribe(self, subscriptions):
@@ -331,10 +326,10 @@ async def unsubscribe(self, subscriptions):
331326
async def unsubscribe_all(self):
332327
self.logger.info("Unsubscribing from all the events")
333328
while self._subscriptions:
334-
events, contexts = self._subscriptions.pop()
335-
self.logger.debug("Unsubscribing from events %s in %s" % (events, contexts))
329+
subscription = self._subscriptions.pop()
330+
self.logger.debug("Unsubscribing from event %s" % subscription)
336331
try:
337-
await self.webdriver.bidi_session.session.unsubscribe(events=events, contexts=contexts)
332+
await self.webdriver.bidi_session.session.unsubscribe(subscriptions=[subscription])
338333
except webdriver_bidi_error.NoSuchFrameException:
339334
# The browsing context is already removed. Nothing to do.
340335
pass
@@ -345,7 +340,7 @@ async def unsubscribe_all(self):
345340
else:
346341
raise e
347342
except Exception as e:
348-
self.logger.error("Failed to unsubscribe from events %s in %s: %s" % (events, contexts, e))
343+
self.logger.error("Failed to unsubscribe from event %s: %s" % (subscription, e))
349344
# Re-raise the exception to identify regressions.
350345
# TODO: consider to continue the loop in case of the exception.
351346
raise e

webdriver/tests/bidi/session/unsubscribe/contexts.py

Lines changed: 0 additions & 166 deletions
This file was deleted.

webdriver/tests/bidi/session/unsubscribe/invalid.py

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from webdriver.bidi.error import InvalidArgumentException, NoSuchFrameException
3+
from webdriver.bidi.error import InvalidArgumentException
44

55
from ... import create_console_api_message
66

@@ -89,79 +89,6 @@ async def test_unsubscribe_from_one_event_and_then_from_module(
8989
await bidi_session.session.unsubscribe(events=["browsingContext.load"])
9090

9191

92-
@pytest.mark.asyncio
93-
@pytest.mark.parametrize("value", [True, "foo", 42, {}])
94-
async def test_params_contexts_invalid_type(bidi_session, value):
95-
with pytest.raises(InvalidArgumentException):
96-
await bidi_session.session.unsubscribe(events=["browsingContext.load"], contexts=value)
97-
98-
99-
@pytest.mark.asyncio
100-
async def test_params_contexts_empty(bidi_session):
101-
with pytest.raises(InvalidArgumentException):
102-
await bidi_session.session.unsubscribe(events=["browsingContext.load"], contexts=[])
103-
104-
105-
@pytest.mark.asyncio
106-
@pytest.mark.parametrize("value", [None, True, 42, [], {}])
107-
async def test_params_contexts_value_invalid_type(bidi_session, value):
108-
with pytest.raises(InvalidArgumentException):
109-
await bidi_session.session.unsubscribe(events=["browsingContext.load"], contexts=[value])
110-
111-
112-
@pytest.mark.asyncio
113-
async def test_params_contexts_value_invalid_value(bidi_session):
114-
with pytest.raises(NoSuchFrameException):
115-
await bidi_session.session.unsubscribe(events=["browsingContext.load"], contexts=["foo"])
116-
117-
118-
@pytest.mark.asyncio
119-
async def test_params_contexts_value_valid_and_invalid_value(
120-
bidi_session, subscribe_events, wait_for_event, wait_for_future_safe, top_context
121-
):
122-
# Subscribe to a valid context
123-
await subscribe_events(events=["log.entryAdded"], contexts=[top_context["context"]])
124-
125-
# Try to unsubscribe from the valid and an invalid context
126-
with pytest.raises(NoSuchFrameException):
127-
await bidi_session.session.unsubscribe(events=["log.entryAdded"], contexts=[top_context["context"], "foo"])
128-
129-
# Make sure that we didn't unsubscribe from the valid context because of the error
130-
# and events are still coming
131-
132-
# Track all received log.entryAdded events in the events array
133-
events = []
134-
135-
async def on_event(method, data):
136-
events.append(data)
137-
138-
remove_listener = bidi_session.add_event_listener(
139-
"log.entryAdded", on_event)
140-
141-
on_entry_added = wait_for_event("log.entryAdded")
142-
await create_console_api_message(bidi_session, top_context, "text1")
143-
await wait_for_future_safe(on_entry_added)
144-
145-
assert len(events) == 1
146-
147-
remove_listener()
148-
149-
150-
@pytest.mark.asyncio
151-
async def test_unsubscribe_from_closed_tab(
152-
bidi_session, subscribe_events
153-
):
154-
new_tab = await bidi_session.browsing_context.create(type_hint="tab")
155-
# Subscribe to a new context
156-
await subscribe_events(events=["log.entryAdded"], contexts=[new_tab["context"]])
157-
158-
await bidi_session.browsing_context.close(context=new_tab["context"])
159-
160-
# Try to unsubscribe from the closed context
161-
with pytest.raises(NoSuchFrameException):
162-
await bidi_session.session.unsubscribe(events=["log.entryAdded"], contexts=[new_tab["context"]])
163-
164-
16592
@pytest.mark.asyncio
16693
async def test_params_unsubscribe_globally_without_subscription(bidi_session):
16794
with pytest.raises(InvalidArgumentException):
@@ -180,26 +107,6 @@ async def test_params_unsubscribe_globally_with_individual_subscription(
180107
await bidi_session.session.unsubscribe(events=["log.entryAdded"])
181108

182109

183-
@pytest.mark.asyncio
184-
async def test_params_unsubscribe_from_one_context_without_subscription(
185-
bidi_session, top_context
186-
):
187-
with pytest.raises(InvalidArgumentException):
188-
await bidi_session.session.unsubscribe(events=["log.entryAdded"], contexts=[top_context["context"]])
189-
190-
191-
@pytest.mark.asyncio
192-
async def test_params_unsubscribe_from_one_context_with_global_subscription(
193-
subscribe_events, bidi_session, top_context
194-
):
195-
# Subscribe to all contexts
196-
await subscribe_events(events=["log.entryAdded"])
197-
198-
# Try to unsubscribe from one context
199-
with pytest.raises(InvalidArgumentException):
200-
await bidi_session.session.unsubscribe(events=["log.entryAdded"], contexts=[top_context["context"]])
201-
202-
203110
@pytest.mark.asyncio
204111
@pytest.mark.parametrize("subscriptions", [None, True, 42, {}, "foo"])
205112
async def test_params_subscriptions_invalid_type(bidi_session, subscriptions):

webdriver/tests/bidi/session/unsubscribe/subscriptions.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -215,47 +215,6 @@ async def on_load_event(method, data):
215215
remove_listener_2()
216216

217217

218-
async def test_unsubscribe_partially_from_one_context(
219-
bidi_session, top_context, new_tab, wait_for_events
220-
):
221-
# Subscribe for log events to multiple contexts at once
222-
result = await bidi_session.session.subscribe(
223-
events=["log.entryAdded"], contexts=[top_context["context"], new_tab["context"]]
224-
)
225-
# Unsubscribe from log events in one of the contexts
226-
await bidi_session.session.unsubscribe(
227-
events=["log.entryAdded"], contexts=[top_context["context"]]
228-
)
229-
230-
with wait_for_events(["log.entryAdded"]) as waiter:
231-
# Trigger console event in the unsubscribed context
232-
await create_console_api_message(bidi_session, top_context, "text1")
233-
events = await waiter.get_events(lambda events: len(events) >= 0)
234-
assert len(events) == 0
235-
236-
# Trigger another console event in the still observed context
237-
expected_text = await create_console_api_message(bidi_session, new_tab, "text2")
238-
events = await waiter.get_events(lambda events: len(events) >= 1)
239-
240-
assert len(events) == 1
241-
recursive_compare(
242-
{
243-
"text": expected_text,
244-
},
245-
events[0][1],
246-
)
247-
248-
# Unsubscribe from subscription.
249-
await bidi_session.session.unsubscribe(subscriptions=[result["subscription"]])
250-
251-
# Trigger another console event
252-
await create_console_api_message(bidi_session, new_tab, "text2")
253-
254-
# Make sure that there're no new events
255-
events = await waiter.get_events(lambda events: len(events) >= 1)
256-
assert len(events) == 1
257-
258-
259218
async def test_unsubscribe_with_event_and_subscriptions(bidi_session, new_tab, inline):
260219
result = await bidi_session.session.subscribe(events=["browsingContext"])
261220

0 commit comments

Comments
 (0)