[py][bidi]: enable `history_updated` event test by navin772 · Pull Request #16236 · SeleniumHQ/selenium

General
Filter events by matching context

Validate the event corresponds to the current context to avoid flakiness in
multi-context environments. Filter to the matching context_id before asserting
URL.

py/test/selenium/webdriver/common/bidi_browsing_context_tests.py [777-779]

 assert len(events_received) >= 1
-assert "/new-path" in events_received[0].url
-assert events_received[0].context == context_id
+matching = [e for e in events_received if e.context == context_id]
+assert matching, "No history_updated event for the current context"
+assert "/new-path" in matching[0].url
  • Apply / Chat
Suggestion importance[1-10]: 7

__

Why: The suggestion improves the assertion logic by filtering events by context_id before checking their properties, making the test more robust against flakiness from other concurrent browsing contexts.

Medium
Add explicit wait failure message

Guard against hanging waits by adding a timeout-safe condition and raise a clear
assertion if the event doesn't arrive. Use an explicit message to aid debugging
when the event fails to fire.

py/test/selenium/webdriver/common/bidi_browsing_context_tests.py [775]

-WebDriverWait(driver, 5).until(lambda d: len(events_received) > 0)
+WebDriverWait(driver, 5).until(
+    lambda d: len(events_received) > 0,
+    message="Timed out waiting for history_updated event"
+)
  • Apply / Chat
Suggestion importance[1-10]: 5

__

Why: This is a good practice suggestion that improves the test's maintainability by adding a descriptive message to the WebDriverWait, which aids in debugging test failures.

Low Possible issue
Target script to correct context

Ensure the BiDi script is executed in the correct browsing context. Pass the
explicit target so the script runs in the current window when multiple contexts
exist, avoiding missed events.

py/test/selenium/webdriver/common/bidi_browsing_context_tests.py [774]

-driver.script.execute("() => { history.pushState({}, '', '/new-path'); }")
+driver.script.execute(
+    function_declaration="() => { history.pushState({}, '', '/new-path'); }",
+    target={"context": driver.current_window_handle}
+)
  • Apply / Chat
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly recommends explicitly setting the target context for script.execute, which enhances the test's robustness by preventing potential ambiguity in multi-context environments.

Low Learned
best practice
Validate event data before use

Guard against empty or malformed event payloads before indexing into the list
and accessing fields. Validate that the first event exists and contains the
expected attributes to prevent intermittent IndexError or AttributeError.

py/test/selenium/webdriver/common/bidi_browsing_context_tests.py [775-779]

 WebDriverWait(driver, 5).until(lambda d: len(events_received) > 0)
 
-assert len(events_received) >= 1
-assert "/new-path" in events_received[0].url
-assert events_received[0].context == context_id
+assert events_received, "No history_updated events received"
+first = events_received[0]
+assert hasattr(first, "url") and "/new-path" in first.url
+assert hasattr(first, "context") and first.context == context_id
  • Apply / Chat
Suggestion importance[1-10]: 6

__

Why:
Relevant best practice - Add null checks and validation for parameters and variables before using them to prevent runtime errors.

Low
  • Update