Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/bidi_integration_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.webdriver.common.by import By
21
from selenium.webdriver.common.window import WindowTypes
22
from selenium.webdriver.support.ui import WebDriverWait
23
24
25
class TestBidiNetworkWithCookies:
26
"""Test integration of network and storage modules."""
27
28
@pytest.fixture(autouse=True)
29
def setup(self, driver, pages):
30
"""Setup for each test in this class."""
31
pages.load("blank.html")
32
yield
33
# Cleanup: delete all cookies to prevent bleed-through
34
driver.delete_all_cookies()
35
36
def test_cookies_interaction(self, driver, pages):
37
"""Test that cookies work with network operations."""
38
pages.load("blank.html")
39
40
# Set a cookie
41
driver.add_cookie({"name": "test_cookie", "value": "test_value"})
42
43
# Verify cookie is set
44
cookies = driver.get_cookies()
45
assert len(cookies) > 0
46
assert any(c.get("name") == "test_cookie" for c in cookies)
47
48
def test_cookie_modification(self, driver, pages):
49
"""Test that modifying cookies works properly."""
50
pages.load("blank.html")
51
52
# Add first cookie
53
driver.add_cookie({"name": "cookie1", "value": "value1"})
54
55
cookies_before = driver.get_cookies()
56
initial_count = len(cookies_before)
57
58
# Add second cookie
59
driver.add_cookie({"name": "cookie2", "value": "value2"})
60
61
cookies_after = driver.get_cookies()
62
assert len(cookies_after) > initial_count
63
64
65
class TestBidiScriptWithNavigation:
66
"""Test integration of script execution and navigation."""
67
68
@pytest.fixture(autouse=True)
69
def setup(self, driver, pages):
70
"""Setup for each test in this class."""
71
driver.delete_all_cookies()
72
pages.load("blank.html")
73
yield
74
# Cleanup: delete all cookies to prevent bleed-through
75
driver.delete_all_cookies()
76
77
def test_script_execution_after_navigation(self, driver, pages):
78
"""Test script execution after page navigation."""
79
# First page
80
pages.load("blank.html")
81
driver.execute_script("window.page1_loaded = true;")
82
83
# Navigate to different page
84
pages.load("blank.html")
85
86
# Previous page variable should not exist
87
result = driver.execute_script("return window.page1_loaded;")
88
assert result is None
89
90
# New variable should work
91
driver.execute_script("window.page2_loaded = true;")
92
result = driver.execute_script("return window.page2_loaded;")
93
assert result is True
94
95
def test_global_variable_lifecycle(self, driver, pages):
96
"""Test global variable lifecycle across operations."""
97
pages.load("blank.html")
98
99
# Set a global variable
100
driver.execute_script("window.test_var = {data: 'value'};")
101
102
# Verify it exists
103
result = driver.execute_script("return window.test_var.data;")
104
assert result == "value"
105
106
# Navigate away
107
driver.get("about:blank")
108
109
# Variable should not exist anymore
110
result = driver.execute_script("return typeof window.test_var;")
111
assert result == "undefined"
112
113
114
class TestBidiEmulationWithNavigation:
115
"""Test integration of emulation and navigation."""
116
117
@pytest.fixture(autouse=True)
118
def setup(self, driver, pages):
119
"""Setup for each test in this class."""
120
pages.load("blank.html")
121
yield
122
# Cleanup: delete all cookies to prevent bleed-through
123
driver.delete_all_cookies()
124
125
def test_basic_navigation(self, driver, pages):
126
"""Test basic navigation."""
127
pages.load("blank.html")
128
assert driver.find_element(By.TAG_NAME, "body") is not None
129
130
131
class TestBidiContextManagement:
132
"""Test integration of context creation and management."""
133
134
def test_create_and_close_context(self, driver):
135
"""Test creating and closing a user context."""
136
new_context = driver.browser.create_user_context()
137
138
try:
139
assert new_context is not None
140
finally:
141
driver.browser.remove_user_context(new_context)
142
143
def test_multiple_contexts_creation(self, driver):
144
"""Test creating multiple contexts."""
145
context1 = driver.browser.create_user_context()
146
context2 = driver.browser.create_user_context()
147
148
try:
149
assert context1 is not None
150
assert context2 is not None
151
assert context1 != context2
152
finally:
153
driver.browser.remove_user_context(context1)
154
driver.browser.remove_user_context(context2)
155
156
157
class TestBidiEventHandlers:
158
"""Test integration of event handlers."""
159
160
@pytest.fixture(autouse=True)
161
def setup(self, driver, pages):
162
"""Setup for each test in this class."""
163
pages.load("blank.html")
164
yield
165
# Cleanup: delete all cookies to prevent bleed-through
166
driver.delete_all_cookies()
167
168
def test_multiple_console_handlers(self, driver):
169
"""Test multiple console message handlers."""
170
messages1 = []
171
messages2 = []
172
173
handler1 = driver.script.add_console_message_handler(messages1.append)
174
handler2 = driver.script.add_console_message_handler(messages2.append)
175
176
try:
177
driver.execute_script("console.log('test message');")
178
WebDriverWait(driver, 5).until(lambda _: len(messages1) > 0 and len(messages2) > 0)
179
180
assert len(messages1) > 0
181
assert len(messages2) > 0
182
finally:
183
driver.script.remove_console_message_handler(handler1)
184
driver.script.remove_console_message_handler(handler2)
185
186
187
class TestBidiStorageOperations:
188
"""Test storage operations."""
189
190
@pytest.fixture(autouse=True)
191
def setup(self, driver, pages):
192
"""Setup for each test in this class."""
193
driver.delete_all_cookies()
194
pages.load("blank.html")
195
yield
196
# Cleanup: delete all cookies to prevent bleed-through
197
driver.delete_all_cookies()
198
199
def test_cookie_operations(self, driver, pages):
200
"""Test basic cookie operations."""
201
pages.load("blank.html")
202
203
# Set cookie
204
driver.add_cookie({"name": "test", "value": "data"})
205
206
# Get cookies
207
cookies = driver.get_cookies()
208
assert any(c.get("name") == "test" for c in cookies)
209
210
# Delete cookie
211
driver.delete_cookie("test")
212
213
# Verify deletion
214
cookies_after = driver.get_cookies()
215
assert not any(c.get("name") == "test" for c in cookies_after)
216
217
def test_cookie_attributes(self, driver, pages):
218
"""Test cookie with various attributes."""
219
pages.load("blank.html")
220
221
driver.add_cookie({"name": "attr_cookie", "value": "test_value", "path": "/", "secure": False})
222
223
cookies = driver.get_cookies()
224
cookie = next((c for c in cookies if c.get("name") == "attr_cookie"), None)
225
226
assert cookie is not None
227
assert cookie.get("value") == "test_value"
228
229
230
class TestBidiBrowsingContexts:
231
"""Test browsing context operations."""
232
233
@pytest.fixture(autouse=True)
234
def setup(self, driver):
235
"""Setup for each test in this class."""
236
driver.delete_all_cookies()
237
yield
238
# Cleanup: delete all cookies to prevent bleed-through
239
driver.delete_all_cookies()
240
241
def test_create_new_window(self, driver):
242
"""Test creating a new window context."""
243
# Create new tab
244
new_context = driver.browsing_context.create(type=WindowTypes.TAB)
245
246
try:
247
assert new_context is not None
248
finally:
249
driver.browsing_context.close(new_context)
250
251
def test_navigation_in_context(self, driver, pages):
252
"""Test navigation in a specific context."""
253
pages.load("blank.html")
254
255
# Navigate using the BiDi API with the current context
256
driver.browsing_context.navigate(context=driver.current_window_handle, url=pages.url("blank.html"))
257
258
# Verify page loaded
259
element = driver.find_element(By.TAG_NAME, "body")
260
assert element is not None
261
262