Path: blob/trunk/py/test/selenium/webdriver/common/bidi_errors_tests.py
8671 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 pytest1819from selenium.common.exceptions import WebDriverException20from selenium.webdriver.common.by import By212223def test_invalid_browsing_context_id(driver):24"""Test that invalid browsing context ID raises an error."""25with pytest.raises(WebDriverException):26driver.browsing_context.close("invalid-context-id")272829def test_invalid_navigation_url(driver):30"""Test that navigation with invalid context should fail."""31with pytest.raises(WebDriverException):32# Invalid context ID should fail33driver.browsing_context.navigate("invalid-context-id", "about:blank")343536def test_invalid_geolocation_coordinates(driver):37"""Test that invalid geolocation coordinates raise an error."""38from selenium.webdriver.common.bidi.emulation import GeolocationCoordinates3940with pytest.raises((WebDriverException, ValueError, TypeError)):41# Invalid latitude (> 90)42coords = GeolocationCoordinates(latitude=999, longitude=180, accuracy=10)43driver.emulation.set_geolocation_override(coordinates=coords)444546def test_invalid_timezone(driver):47"""Test that invalid timezone string raises an error."""48with pytest.raises((WebDriverException, ValueError)):49driver.emulation.set_timezone_override("Invalid/Timezone")505152def test_invalid_set_cookie(driver, pages):53"""Test that setting cookie with None raises an error."""54pages.load("blank.html")5556with pytest.raises((WebDriverException, TypeError, AttributeError)):57driver.storage.set_cookie(None)585960def test_remove_nonexistent_context(driver):61"""Test that removing non-existent context raises an error."""62with pytest.raises(WebDriverException):63driver.browser.remove_user_context("non-existent-context-id")646566def test_invalid_perform_actions_missing_context(driver, pages):67"""Test that perform_actions without context raises an error."""68pages.load("blank.html")6970with pytest.raises(TypeError):71# Missing required 'context' parameter72driver.input.perform_actions(actions=[])737475def test_error_recovery_after_invalid_navigation(driver):76"""Test that driver can recover after failed navigation."""77# Try an invalid navigation with bad context78with pytest.raises(WebDriverException):79driver.browsing_context.navigate("invalid-context", "about:blank")8081# Driver should still be functional82driver.get("about:blank")83assert driver.find_element(By.TAG_NAME, "body") is not None848586def test_multiple_error_conditions(driver, pages):87"""Test handling multiple error conditions in sequence."""88pages.load("blank.html")8990# First error91with pytest.raises(WebDriverException):92driver.browser.remove_user_context("invalid")9394# Driver should still work95assert driver.find_element(By.TAG_NAME, "body") is not None9697# Second error98with pytest.raises((WebDriverException, ValueError)):99driver.emulation.set_timezone_override("Invalid")100101# Driver still functional102driver.get("about:blank")103104105class TestBidiErrorHandling:106"""Test class for error handling in BiDi operations."""107108@pytest.fixture(autouse=True)109def setup(self, driver, pages):110"""Setup for each test in this class."""111pages.load("blank.html")112113def test_error_on_invalid_context_operations(self, driver):114"""Test error handling with invalid context operations."""115# Try to close non-existent context116with pytest.raises(WebDriverException):117driver.browsing_context.close("nonexistent")118119def test_error_recovery_sequence(self, driver):120"""Test that driver recovers properly from errors."""121# First operation fails122with pytest.raises(WebDriverException):123driver.browser.remove_user_context("bad-id")124125# Recovery test126element = driver.find_element(By.TAG_NAME, "body")127assert element is not None128129def test_consecutive_errors(self, driver):130"""Test handling consecutive errors."""131errors_caught = 0132133# First error134try:135driver.browser.remove_user_context("id1")136except WebDriverException:137errors_caught += 1138139# Second error140try:141driver.browser.remove_user_context("id2")142except WebDriverException:143errors_caught += 1144145assert errors_caught == 2146147# Driver should still work148driver.get("about:blank")149150151