Path: blob/trunk/py/test/selenium/webdriver/common/bidi_network_tests.py
4091 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"3435# Clean up36driver.network._remove_intercept(result["intercept"])373839def test_remove_intercept(driver):40result = driver.network._add_intercept()41driver.network._remove_intercept(result["intercept"])42assert driver.network.intercepts == [], "Intercept not removed"434445def test_add_and_remove_request_handler(driver, pages):46requests = []4748def callback(request: Request):49requests.append(request)5051callback_id = driver.network.add_request_handler("before_request", callback)52assert callback_id is not None, "Request handler not added"53driver.network.remove_request_handler("before_request", callback_id)54pages.load("formPage.html")55assert not requests, "Requests intercepted"56assert driver.find_element(By.NAME, "login").is_displayed(), "Request not continued"575859def test_clear_request_handlers(driver, pages):60requests = []6162def callback(request: Request):63requests.append(request)6465callback_id_1 = driver.network.add_request_handler("before_request", callback)66assert callback_id_1 is not None, "Request handler not added"67callback_id_2 = driver.network.add_request_handler("before_request", callback)68assert callback_id_2 is not None, "Request handler not added"6970driver.network.clear_request_handlers()7172pages.load("formPage.html")73assert not requests, "Requests intercepted"74assert driver.find_element(By.NAME, "login").is_displayed(), "Request not continued"757677def test_continue_request(driver, pages):78exceptions = []7980def callback(request: Request):81try:82request.continue_request()83except WebDriverException as e:84exceptions.append(e)8586callback_id = driver.network.add_request_handler("before_request", callback)87assert callback_id is not None, "Request handler not added"88url = pages.url("formPage.html")89driver.browsing_context.navigate(context=driver.current_window_handle, url=url, wait=ReadinessState.COMPLETE)90assert driver.find_element(By.NAME, "login").is_displayed(), "Request not continued"91assert len(exceptions) == 0, "Exception raised when continuing request in handler callback"9293driver.network.remove_request_handler("before_request", callback_id)949596def test_continue_with_auth(driver):97callback_id = driver.network.add_auth_handler("postman", "password")98assert callback_id is not None, "Request handler not added"99driver.browsing_context.navigate(100context=driver.current_window_handle, url="https://postman-echo.com/basic-auth", wait=ReadinessState.COMPLETE101)102assert "authenticated" in driver.page_source, "Authorization failed"103104driver.network.remove_auth_handler(callback_id)105106107def test_remove_auth_handler(driver):108callback_id = driver.network.add_auth_handler("user", "passwd")109assert callback_id is not None, "Request handler not added"110driver.network.remove_auth_handler(callback_id)111assert driver.network.intercepts == [], "Intercept not removed"112113114def test_handler_with_classic_navigation(driver, pages):115"""Verify request handlers also work with classic navigation."""116browser_name = driver.caps["browserName"]117if browser_name.lower() in ("chrome", "microsoftedge"):118pytest.skip(reason=f"Request handlers don't yet work in {browser_name} using classic navigation")119120exceptions = []121122def callback(request: Request):123try:124request.continue_request()125except WebDriverException as e:126exceptions.append(e)127128callback_id = driver.network.add_request_handler("before_request", callback)129assert callback_id is not None, "Request handler not added"130pages.load("formPage.html")131assert len(exceptions) == 0, "Exception raised in handler callback"132133driver.network.remove_request_handler("before_request", callback_id)134135136@pytest.mark.xfail_chrome(reason="Data URLs in Network requests are not implemented in Chrome yet")137@pytest.mark.xfail_edge(reason="Data URLs in Network requests are not implemented in Edge yet")138@pytest.mark.xfail_firefox(reason="Data URLs in Network requests are not implemented in Firefox yet")139def test_handler_with_data_url_request(driver, pages):140data_requests = []141exceptions = []142143def callback(request: Request):144if request.url.startswith("data:"):145data_requests.append(request)146try:147request.continue_request()148except WebDriverException as e:149exceptions.append(e)150151callback_id = driver.network.add_request_handler("before_request", callback)152url = pages.url("data_url.html")153driver.browsing_context.navigate(context=driver.current_window_handle, url=url, wait=ReadinessState.COMPLETE)154time.sleep(1) # give callback time to complete155assert driver.find_element(By.ID, "data-url-image").is_displayed()156assert len(data_requests) > 0, "BiDi event not captured"157assert len(exceptions) == 0, "Exception raised when continuing request in handler callback"158159driver.network.remove_request_handler("before_request", callback_id)160161162