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 000000000000..e22cf34eb095 --- /dev/null +++ b/examples/python/tests/bidi/test_network_commands.py @@ -0,0 +1,80 @@ +# 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): + # _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 == [] + + +@pytest.mark.driver_type("bidi") +def test_fail_request(driver): + from selenium.webdriver.common.bidi.network import Request + + failed_requests = [] + + intercept = driver.network._add_intercept() + + def on_request(request: 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() + + +@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) + 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 b4310a46c3d1..7891f77278a2 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-L26" >}} +{{< /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#L29-L35" >}} +{{< /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#L38-L62" >}} +{{< /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#L65-L80" >}} +{{< /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