Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/bidi_errors_tests.py
8671 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
import pytest
19
20
from selenium.common.exceptions import WebDriverException
21
from selenium.webdriver.common.by import By
22
23
24
def test_invalid_browsing_context_id(driver):
25
"""Test that invalid browsing context ID raises an error."""
26
with pytest.raises(WebDriverException):
27
driver.browsing_context.close("invalid-context-id")
28
29
30
def test_invalid_navigation_url(driver):
31
"""Test that navigation with invalid context should fail."""
32
with pytest.raises(WebDriverException):
33
# Invalid context ID should fail
34
driver.browsing_context.navigate("invalid-context-id", "about:blank")
35
36
37
def test_invalid_geolocation_coordinates(driver):
38
"""Test that invalid geolocation coordinates raise an error."""
39
from selenium.webdriver.common.bidi.emulation import GeolocationCoordinates
40
41
with pytest.raises((WebDriverException, ValueError, TypeError)):
42
# Invalid latitude (> 90)
43
coords = GeolocationCoordinates(latitude=999, longitude=180, accuracy=10)
44
driver.emulation.set_geolocation_override(coordinates=coords)
45
46
47
def test_invalid_timezone(driver):
48
"""Test that invalid timezone string raises an error."""
49
with pytest.raises((WebDriverException, ValueError)):
50
driver.emulation.set_timezone_override("Invalid/Timezone")
51
52
53
def test_invalid_set_cookie(driver, pages):
54
"""Test that setting cookie with None raises an error."""
55
pages.load("blank.html")
56
57
with pytest.raises((WebDriverException, TypeError, AttributeError)):
58
driver.storage.set_cookie(None)
59
60
61
def test_remove_nonexistent_context(driver):
62
"""Test that removing non-existent context raises an error."""
63
with pytest.raises(WebDriverException):
64
driver.browser.remove_user_context("non-existent-context-id")
65
66
67
def test_invalid_perform_actions_missing_context(driver, pages):
68
"""Test that perform_actions without context raises an error."""
69
pages.load("blank.html")
70
71
with pytest.raises(TypeError):
72
# Missing required 'context' parameter
73
driver.input.perform_actions(actions=[])
74
75
76
def test_error_recovery_after_invalid_navigation(driver):
77
"""Test that driver can recover after failed navigation."""
78
# Try an invalid navigation with bad context
79
with pytest.raises(WebDriverException):
80
driver.browsing_context.navigate("invalid-context", "about:blank")
81
82
# Driver should still be functional
83
driver.get("about:blank")
84
assert driver.find_element(By.TAG_NAME, "body") is not None
85
86
87
def test_multiple_error_conditions(driver, pages):
88
"""Test handling multiple error conditions in sequence."""
89
pages.load("blank.html")
90
91
# First error
92
with pytest.raises(WebDriverException):
93
driver.browser.remove_user_context("invalid")
94
95
# Driver should still work
96
assert driver.find_element(By.TAG_NAME, "body") is not None
97
98
# Second error
99
with pytest.raises((WebDriverException, ValueError)):
100
driver.emulation.set_timezone_override("Invalid")
101
102
# Driver still functional
103
driver.get("about:blank")
104
105
106
class TestBidiErrorHandling:
107
"""Test class for error handling in BiDi operations."""
108
109
@pytest.fixture(autouse=True)
110
def setup(self, driver, pages):
111
"""Setup for each test in this class."""
112
pages.load("blank.html")
113
114
def test_error_on_invalid_context_operations(self, driver):
115
"""Test error handling with invalid context operations."""
116
# Try to close non-existent context
117
with pytest.raises(WebDriverException):
118
driver.browsing_context.close("nonexistent")
119
120
def test_error_recovery_sequence(self, driver):
121
"""Test that driver recovers properly from errors."""
122
# First operation fails
123
with pytest.raises(WebDriverException):
124
driver.browser.remove_user_context("bad-id")
125
126
# Recovery test
127
element = driver.find_element(By.TAG_NAME, "body")
128
assert element is not None
129
130
def test_consecutive_errors(self, driver):
131
"""Test handling consecutive errors."""
132
errors_caught = 0
133
134
# First error
135
try:
136
driver.browser.remove_user_context("id1")
137
except WebDriverException:
138
errors_caught += 1
139
140
# Second error
141
try:
142
driver.browser.remove_user_context("id2")
143
except WebDriverException:
144
errors_caught += 1
145
146
assert errors_caught == 2
147
148
# Driver should still work
149
driver.get("about:blank")
150
151