Path: blob/trunk/py/test/selenium/webdriver/common/bidi_network_tests.py
1865 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import time1819import pytest2021from selenium.common.exceptions import WebDriverException22from selenium.webdriver.common.bidi.browsing_context import ReadinessState23from selenium.webdriver.common.bidi.network import Request24from selenium.webdriver.common.by import By252627def test_network_initialized(driver):28assert driver.network is not None293031def test_add_intercept(driver, pages):32result = driver.network._add_intercept()33assert result is not None, "Intercept not added"343536def test_remove_intercept(driver):37result = driver.network._add_intercept()38driver.network._remove_intercept(result["intercept"])39assert driver.network.intercepts == [], "Intercept not removed"404142def test_add_and_remove_request_handler(driver, pages):43requests = []4445def callback(request: Request):46requests.append(request)4748callback_id = driver.network.add_request_handler("before_request", callback)49assert callback_id is not None, "Request handler not added"50driver.network.remove_request_handler("before_request", callback_id)51pages.load("formPage.html")52assert not requests, "Requests intercepted"53assert driver.find_element(By.NAME, "login").is_displayed(), "Request not continued"545556def test_clear_request_handlers(driver, pages):57requests = []5859def callback(request: Request):60requests.append(request)6162callback_id_1 = driver.network.add_request_handler("before_request", callback)63assert callback_id_1 is not None, "Request handler not added"64callback_id_2 = driver.network.add_request_handler("before_request", callback)65assert callback_id_2 is not None, "Request handler not added"6667driver.network.clear_request_handlers()6869pages.load("formPage.html")70assert not requests, "Requests intercepted"71assert driver.find_element(By.NAME, "login").is_displayed(), "Request not continued"727374def test_continue_request(driver, pages):75def callback(request: Request):76request.continue_request()7778callback_id = driver.network.add_request_handler("before_request", callback)79assert callback_id is not None, "Request handler not added"80url = pages.url("formPage.html")81driver.browsing_context.navigate(context=driver.current_window_handle, url=url, wait=ReadinessState.COMPLETE)82assert driver.find_element(By.NAME, "login").is_displayed(), "Request not continued"838485def test_continue_with_auth(driver):86callback_id = driver.network.add_auth_handler("postman", "password")87assert callback_id is not None, "Request handler not added"88driver.browsing_context.navigate(89context=driver.current_window_handle, url="https://postman-echo.com/basic-auth", wait=ReadinessState.COMPLETE90)91assert "authenticated" in driver.page_source, "Authorization failed"929394def test_remove_auth_handler(driver):95callback_id = driver.network.add_auth_handler("user", "passwd")96assert callback_id is not None, "Request handler not added"97driver.network.remove_auth_handler(callback_id)98assert driver.network.intercepts == [], "Intercept not removed"99100101@pytest.mark.xfail_chrome(reason="Data URLs in Network requests are not implemented in Chrome yet")102@pytest.mark.xfail_edge(reason="Data URLs in Network requests are not implemented in Edge yet")103@pytest.mark.xfail_firefox(reason="Data URLs in Network requests are not implemented in Firefox yet")104def test_handler_with_data_url_request(driver, pages):105data_requests = []106exceptions = []107108def callback(request: Request):109if request.url.startswith("data:"):110data_requests.append(request)111try:112request.continue_request()113except WebDriverException as e:114exceptions.append(e)115116driver.network.add_request_handler("before_request", callback)117url = pages.url("data_url.html")118driver.browsing_context.navigate(context=driver.current_window_handle, url=url, wait=ReadinessState.COMPLETE)119time.sleep(1) # give callback time to complete120assert driver.find_element(By.ID, "data-url-image").is_displayed()121assert len(data_requests) > 0, "BiDi event not captured"122assert len(exceptions) == 0, "Exception raised when continuing request in callback"123124125