From 6df5977617bb00c1f6e7a259aaf4d4c3de924d26 Mon Sep 17 00:00:00 2001 From: Ajith KV Date: Thu, 14 May 2026 12:40:25 +0530 Subject: [PATCH 1/4] Add Python BiDi network intercept examples --- .../tests/bidi/test_network_commands.py | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 examples/python/tests/bidi/test_network_commands.py diff --git a/examples/python/tests/bidi/test_network_commands.py b/examples/python/tests/bidi/test_network_commands.py new file mode 100644 index 00000000000..b3ec4e05396 --- /dev/null +++ b/examples/python/tests/bidi/test_network_commands.py @@ -0,0 +1,65 @@ +# Licensed to the Software Freedom Conservancy (SFC) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The SFC licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import pytest + + +@pytest.mark.driver_type("bidi") +def test_add_intercept(driver): + intercept = driver.network._add_intercept() + assert intercept is not None + + +@pytest.mark.driver_type("bidi") +def test_remove_intercept(driver): + intercept = driver.network._add_intercept() + driver.network._remove_intercept(intercept["intercept"]) + assert driver.network.intercepts == [] + + +@pytest.mark.driver_type("bidi") +def test_fail_request(driver): + import time + from selenium.webdriver.common.bidi.network import Request + + failed = [] + + def on_request(request: Request): + failed.append(request) + + driver.network.add_request_handler("before_request", on_request) + try: + driver.get("https://www.selenium.dev/selenium/web/blank.html") + except Exception: + pass + time.sleep(1) + driver.network.clear_request_handlers() + + +@pytest.mark.driver_type("bidi") +def test_add_and_remove_request_handler(driver): + from selenium.webdriver.common.bidi.network import Request + + requests = [] + + def callback(request: Request): + requests.append(request) + + callback_id = driver.network.add_request_handler("before_request", callback) + assert callback_id is not None + + driver.network.remove_request_handler("before_request", callback_id) \ No newline at end of file From 7e1adcc1f7a8bbf812865a168bd90540201ef81d Mon Sep 17 00:00:00 2001 From: Ajith KV Date: Thu, 14 May 2026 13:15:11 +0530 Subject: [PATCH 2/4] Add Python tab references to W3C BiDi network docs --- .../webdriver/bidi/w3c/network.en.md | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md b/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md index b4310a46c3d..09977e48104 100644 --- a/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md +++ b/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md @@ -8,7 +8,7 @@ aliases: [ --- ## Commands -This section contains the APIs related to network commands. +This section contains the APIs related to network commands. ### Add network intercept @@ -17,6 +17,10 @@ This section contains the APIs related to network commands. {{< badge-version version="4.18" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/NetworkCommandsTest.java#L36-L38" >}} {{< /tab >}} +{{< tab header="Python" >}} +{{< badge-version version="4.32" >}} +{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L21-L24" >}} +{{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} {{< /tab >}} @@ -36,6 +40,10 @@ This section contains the APIs related to network commands. {{< badge-version version="4.18" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/NetworkCommandsTest.java#L46-L50" >}} {{< /tab >}} +{{< tab header="Python" >}} +{{< badge-version version="4.32" >}} +{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L27-L31" >}} +{{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} {{< /tab >}} @@ -55,6 +63,9 @@ This section contains the APIs related to network commands. {{< badge-version version="4.18" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/NetworkCommandsTest.java#L57-L64" >}} {{< /tab >}} +{{< tab header="Python" >}} +{{< badge-code >}} +{{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} {{< /tab >}} @@ -74,6 +85,9 @@ This section contains the APIs related to network commands. {{< badge-version version="4.18" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/NetworkCommandsTest.java#L74-L80" >}} {{< /tab >}} +{{< tab header="Python" >}} +{{< badge-code >}} +{{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} {{< /tab >}} @@ -93,6 +107,9 @@ This section contains the APIs related to network commands. {{< badge-version version="4.18" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/NetworkCommandsTest.java#L90-L96" >}} {{< /tab >}} +{{< tab header="Python" >}} +{{< badge-code >}} +{{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} {{< /tab >}} @@ -112,6 +129,10 @@ This section contains the APIs related to network commands. {{< badge-version version="4.18" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/NetworkCommandsTest.java#L104-L108" >}} {{< /tab >}} +{{< tab header="Python" >}} +{{< badge-version version="4.32" >}} +{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L34-L50" >}} +{{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} {{< /tab >}} @@ -125,7 +146,7 @@ This section contains the APIs related to network commands. ## Events -This section contains the APIs related to network events. +This section contains the APIs related to network events. ### Before Request Sent @@ -134,6 +155,10 @@ This section contains the APIs related to network events. {{< badge-version version="4.15" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/NetworkEventsTest.java#L30-L35" >}} {{< /tab >}} +{{< tab header="Python" >}} +{{< badge-version version="4.32" >}} +{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L53-L65" >}} +{{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} {{< /tab >}} @@ -153,6 +178,9 @@ This section contains the APIs related to network events. {{< badge-version version="4.15" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/NetworkEventsTest.java#L45-L51" >}} {{< /tab >}} +{{< tab header="Python" >}} +{{< badge-code >}} +{{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} {{< /tab >}} @@ -172,6 +200,9 @@ This section contains the APIs related to network events. {{< badge-version version="4.15" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/NetworkEventsTest.java#L62-L68" >}} {{< /tab >}} +{{< tab header="Python" >}} +{{< badge-code >}} +{{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} {{< /tab >}} @@ -191,6 +222,9 @@ This section contains the APIs related to network events. {{< badge-version version="4.17" >}} {{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/NetworkEventsTest.java#L101-L106" >}} {{< /tab >}} +{{< tab header="Python" >}} +{{< badge-code >}} +{{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} {{< /tab >}} @@ -200,6 +234,4 @@ This section contains the APIs related to network events. {{< tab header="Kotlin" >}} {{< badge-code >}} {{< /tab >}} -{{< /tabpane >}} - - +{{< /tabpane >}} \ No newline at end of file From 55e2b79b985406ae693e0a70ef5aac6fce0af8a5 Mon Sep 17 00:00:00 2001 From: Ajith KV Date: Thu, 14 May 2026 13:58:23 +0530 Subject: [PATCH 3/4] fix test_fail_request to actually invoke fail_request and add assertion --- .../python/tests/bidi/test_network_commands.py | 14 +++++++++++--- .../documentation/webdriver/bidi/w3c/network.en.md | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/examples/python/tests/bidi/test_network_commands.py b/examples/python/tests/bidi/test_network_commands.py index b3ec4e05396..37feaffb578 100644 --- a/examples/python/tests/bidi/test_network_commands.py +++ b/examples/python/tests/bidi/test_network_commands.py @@ -33,20 +33,28 @@ def test_remove_intercept(driver): @pytest.mark.driver_type("bidi") def test_fail_request(driver): - import time from selenium.webdriver.common.bidi.network import Request - failed = [] + failed_requests = [] + + intercept = driver.network._add_intercept() def on_request(request: Request): - failed.append(request) + failed_requests.append(request) + driver.network.fail_request(request.request["requestId"]) driver.network.add_request_handler("before_request", on_request) + try: driver.get("https://www.selenium.dev/selenium/web/blank.html") except Exception: pass + + import time time.sleep(1) + + assert len(failed_requests) > 0 + driver.network._remove_intercept(intercept["intercept"]) driver.network.clear_request_handlers() diff --git a/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md b/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md index 09977e48104..38607e26d8f 100644 --- a/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md +++ b/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md @@ -131,7 +131,7 @@ This section contains the APIs related to network commands. {{< /tab >}} {{< tab header="Python" >}} {{< badge-version version="4.32" >}} -{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L34-L50" >}} +{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L34-L58" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} @@ -157,7 +157,7 @@ This section contains the APIs related to network events. {{< /tab >}} {{< tab header="Python" >}} {{< badge-version version="4.32" >}} -{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L53-L65" >}} +{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L61-L72" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} From dcfd84b41398f42816c794b06bd9d78de1d951ed Mon Sep 17 00:00:00 2001 From: Ajith KV Date: Thu, 14 May 2026 14:47:39 +0530 Subject: [PATCH 4/4] Address Qodo review: add comments for private APIs, verify handler removal --- examples/python/tests/bidi/test_network_commands.py | 9 ++++++++- .../documentation/webdriver/bidi/w3c/network.en.md | 8 ++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/examples/python/tests/bidi/test_network_commands.py b/examples/python/tests/bidi/test_network_commands.py index 37feaffb578..e22cf34eb09 100644 --- a/examples/python/tests/bidi/test_network_commands.py +++ b/examples/python/tests/bidi/test_network_commands.py @@ -20,12 +20,16 @@ @pytest.mark.driver_type("bidi") def test_add_intercept(driver): + # _add_intercept is currently the available Python API for BiDi network intercepts. + # This will be updated when a public API is stabilized. intercept = driver.network._add_intercept() assert intercept is not None @pytest.mark.driver_type("bidi") def test_remove_intercept(driver): + # _add_intercept/_remove_intercept are currently the available Python APIs. + # These will be updated when a public API is stabilized. intercept = driver.network._add_intercept() driver.network._remove_intercept(intercept["intercept"]) assert driver.network.intercepts == [] @@ -70,4 +74,7 @@ def callback(request: Request): callback_id = driver.network.add_request_handler("before_request", callback) assert callback_id is not None - driver.network.remove_request_handler("before_request", callback_id) \ No newline at end of file + driver.network.remove_request_handler("before_request", callback_id) + assert callback_id not in [ + h for h in driver.network._handlers.get("before_request", []) + ] \ No newline at end of file diff --git a/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md b/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md index 38607e26d8f..7891f77278a 100644 --- a/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md +++ b/website_and_docs/content/documentation/webdriver/bidi/w3c/network.en.md @@ -19,7 +19,7 @@ This section contains the APIs related to network commands. {{< /tab >}} {{< tab header="Python" >}} {{< badge-version version="4.32" >}} -{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L21-L24" >}} +{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L21-L26" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} @@ -42,7 +42,7 @@ This section contains the APIs related to network commands. {{< /tab >}} {{< tab header="Python" >}} {{< badge-version version="4.32" >}} -{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L27-L31" >}} +{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L29-L35" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} @@ -131,7 +131,7 @@ This section contains the APIs related to network commands. {{< /tab >}} {{< tab header="Python" >}} {{< badge-version version="4.32" >}} -{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L34-L58" >}} +{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L38-L62" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}} @@ -157,7 +157,7 @@ This section contains the APIs related to network events. {{< /tab >}} {{< tab header="Python" >}} {{< badge-version version="4.32" >}} -{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L61-L72" >}} +{{< gh-codeblock path="/examples/python/tests/bidi/test_network_commands.py#L65-L80" >}} {{< /tab >}} {{< tab header="Ruby" >}} {{< badge-code >}}