|
| 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 random |
| 19 | +import time |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from selenium.webdriver.common.bidi.storage import BrowsingContextPartitionDescriptor |
| 24 | +from selenium.webdriver.common.bidi.storage import BytesValue |
| 25 | +from selenium.webdriver.common.bidi.storage import CookieFilter |
| 26 | +from selenium.webdriver.common.bidi.storage import PartialCookie |
| 27 | +from selenium.webdriver.common.bidi.storage import SameSite |
| 28 | +from selenium.webdriver.common.bidi.storage import StorageKeyPartitionDescriptor |
| 29 | +from selenium.webdriver.common.window import WindowTypes |
| 30 | + |
| 31 | + |
| 32 | +def generate_unique_key(): |
| 33 | + return f"key_{random.randint(0, 100000)}" |
| 34 | + |
| 35 | + |
| 36 | +def assert_cookie_is_not_present_with_name(driver, key): |
| 37 | + assert driver.get_cookie(key) is None |
| 38 | + document_cookie = get_document_cookie_or_none(driver) |
| 39 | + if document_cookie is not None: |
| 40 | + assert key + "=" not in document_cookie |
| 41 | + |
| 42 | + |
| 43 | +def assert_cookie_is_present_with_name(driver, key): |
| 44 | + assert driver.get_cookie(key) is not None |
| 45 | + document_cookie = get_document_cookie_or_none(driver) |
| 46 | + if document_cookie is not None: |
| 47 | + assert key + "=" in document_cookie |
| 48 | + |
| 49 | + |
| 50 | +def assert_cookie_has_value(driver, key, value): |
| 51 | + assert driver.get_cookie(key)["value"] == value |
| 52 | + document_cookie = get_document_cookie_or_none(driver) |
| 53 | + if document_cookie is not None: |
| 54 | + assert f"{key}={value}" in document_cookie |
| 55 | + |
| 56 | + |
| 57 | +def assert_no_cookies_are_present(driver): |
| 58 | + assert len(driver.get_cookies()) == 0 |
| 59 | + document_cookie = get_document_cookie_or_none(driver) |
| 60 | + if document_cookie is not None: |
| 61 | + assert document_cookie == "" |
| 62 | + |
| 63 | + |
| 64 | +def assert_some_cookies_are_present(driver): |
| 65 | + assert len(driver.get_cookies()) > 0 |
| 66 | + document_cookie = get_document_cookie_or_none(driver) |
| 67 | + if document_cookie is not None: |
| 68 | + assert document_cookie != "" |
| 69 | + |
| 70 | + |
| 71 | +def get_document_cookie_or_none(driver): |
| 72 | + try: |
| 73 | + return driver.execute_script("return document.cookie") |
| 74 | + except Exception: |
| 75 | + return None |
| 76 | + |
| 77 | + |
| 78 | +def test_storage_initialized(driver): |
| 79 | + """Test that the storage module is initialized properly.""" |
| 80 | + assert driver.storage is not None |
| 81 | + |
| 82 | + |
| 83 | +def test_get_cookie_by_name(driver, pages, webserver): |
| 84 | + """Test getting a cookie by name.""" |
| 85 | + # Setup |
| 86 | + driver.get(pages.url("simpleTest.html")) |
| 87 | + driver.delete_all_cookies() |
| 88 | + assert_no_cookies_are_present(driver) |
| 89 | + |
| 90 | + key = generate_unique_key() |
| 91 | + value = "set" |
| 92 | + assert_cookie_is_not_present_with_name(driver, key) |
| 93 | + |
| 94 | + driver.add_cookie({"name": key, "value": value}) |
| 95 | + |
| 96 | + # Test |
| 97 | + cookie_filter = CookieFilter(name=key, value=BytesValue(BytesValue.TYPE_STRING, "set")) |
| 98 | + |
| 99 | + result = driver.storage.get_cookies(filter=cookie_filter) |
| 100 | + |
| 101 | + # Verify |
| 102 | + assert len(result.cookies) > 0 |
| 103 | + assert result.cookies[0].value.value == value |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.xfail_chrome |
| 107 | +@pytest.mark.xfail_edge |
| 108 | +def test_get_cookie_in_default_user_context(driver, pages, webserver): |
| 109 | + """Test getting a cookie in the default user context.""" |
| 110 | + # Setup |
| 111 | + driver.get(pages.url("simpleTest.html")) |
| 112 | + driver.delete_all_cookies() |
| 113 | + assert_no_cookies_are_present(driver) |
| 114 | + |
| 115 | + window_handle = driver.current_window_handle |
| 116 | + key = generate_unique_key() |
| 117 | + value = "set" |
| 118 | + assert_cookie_is_not_present_with_name(driver, key) |
| 119 | + |
| 120 | + driver.add_cookie({"name": key, "value": value}) |
| 121 | + |
| 122 | + # Test |
| 123 | + cookie_filter = CookieFilter(name=key, value=BytesValue(BytesValue.TYPE_STRING, "set")) |
| 124 | + |
| 125 | + driver.switch_to.new_window(WindowTypes.WINDOW) |
| 126 | + |
| 127 | + descriptor = BrowsingContextPartitionDescriptor(driver.current_window_handle) |
| 128 | + |
| 129 | + params = cookie_filter |
| 130 | + result_after_switching_context = driver.storage.get_cookies(filter=params, partition=descriptor) |
| 131 | + |
| 132 | + assert len(result_after_switching_context.cookies) > 0 |
| 133 | + assert result_after_switching_context.cookies[0].value.value == value |
| 134 | + |
| 135 | + driver.switch_to.window(window_handle) |
| 136 | + |
| 137 | + descriptor = BrowsingContextPartitionDescriptor(driver.current_window_handle) |
| 138 | + |
| 139 | + result = driver.storage.get_cookies(filter=cookie_filter, partition=descriptor) |
| 140 | + |
| 141 | + assert len(result.cookies) > 0 |
| 142 | + assert result.cookies[0].value.value == value |
| 143 | + partition_key = result.partition_key |
| 144 | + |
| 145 | + assert partition_key.source_origin is not None |
| 146 | + assert partition_key.user_context is not None |
| 147 | + assert partition_key.user_context == "default" |
| 148 | + |
| 149 | + |
| 150 | +def test_get_cookie_in_a_user_context(driver, pages, webserver): |
| 151 | + """Test getting a cookie in a user context.""" |
| 152 | + # Setup |
| 153 | + driver.get(pages.url("simpleTest.html")) |
| 154 | + driver.delete_all_cookies() |
| 155 | + assert_no_cookies_are_present(driver) |
| 156 | + |
| 157 | + user_context = driver.browser.create_user_context() |
| 158 | + window_handle = driver.current_window_handle |
| 159 | + |
| 160 | + key = generate_unique_key() |
| 161 | + value = "set" |
| 162 | + |
| 163 | + descriptor = StorageKeyPartitionDescriptor(user_context=user_context) |
| 164 | + |
| 165 | + parameters = PartialCookie(key, BytesValue(BytesValue.TYPE_STRING, value), webserver.host) |
| 166 | + |
| 167 | + driver.storage.set_cookie(cookie=parameters, partition=descriptor) |
| 168 | + |
| 169 | + # Test |
| 170 | + cookie_filter = CookieFilter(name=key, value=BytesValue(BytesValue.TYPE_STRING, "set")) |
| 171 | + |
| 172 | + # Create a new window with the user context |
| 173 | + new_window = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context) |
| 174 | + |
| 175 | + driver.switch_to.window(new_window) |
| 176 | + |
| 177 | + result = driver.storage.get_cookies(filter=cookie_filter, partition=descriptor) |
| 178 | + |
| 179 | + assert len(result.cookies) > 0 |
| 180 | + assert result.cookies[0].value.value == value |
| 181 | + partition_key = result.partition_key |
| 182 | + |
| 183 | + assert partition_key.user_context is not None |
| 184 | + assert partition_key.user_context == user_context |
| 185 | + |
| 186 | + driver.switch_to.window(window_handle) |
| 187 | + |
| 188 | + browsing_context_partition_descriptor = BrowsingContextPartitionDescriptor(window_handle) |
| 189 | + |
| 190 | + result1 = driver.storage.get_cookies(filter=cookie_filter, partition=browsing_context_partition_descriptor) |
| 191 | + |
| 192 | + assert len(result1.cookies) == 0 |
| 193 | + |
| 194 | + # Clean up |
| 195 | + driver.browsing_context.close(new_window) |
| 196 | + driver.browser.remove_user_context(user_context) |
| 197 | + |
| 198 | + |
| 199 | +def test_add_cookie(driver, pages, webserver): |
| 200 | + """Test adding a cookie.""" |
| 201 | + # Setup |
| 202 | + driver.get(pages.url("simpleTest.html")) |
| 203 | + driver.delete_all_cookies() |
| 204 | + assert_no_cookies_are_present(driver) |
| 205 | + |
| 206 | + key = generate_unique_key() |
| 207 | + value = "foo" |
| 208 | + |
| 209 | + parameters = PartialCookie(key, BytesValue(BytesValue.TYPE_STRING, value), webserver.host) |
| 210 | + assert_cookie_is_not_present_with_name(driver, key) |
| 211 | + |
| 212 | + # Test |
| 213 | + driver.storage.set_cookie(cookie=parameters) |
| 214 | + |
| 215 | + # Verify |
| 216 | + assert_cookie_has_value(driver, key, value) |
| 217 | + driver.get(pages.url("simpleTest.html")) |
| 218 | + assert_cookie_has_value(driver, key, value) |
| 219 | + |
| 220 | + |
| 221 | +@pytest.mark.xfail_chrome |
| 222 | +@pytest.mark.xfail_edge |
| 223 | +def test_add_and_get_cookie(driver, pages, webserver): |
| 224 | + """Test adding and getting a cookie with all parameters.""" |
| 225 | + # Setup |
| 226 | + driver.get(pages.url("simpleTest.html")) |
| 227 | + driver.delete_all_cookies() |
| 228 | + assert_no_cookies_are_present(driver) |
| 229 | + |
| 230 | + value = BytesValue(BytesValue.TYPE_STRING, "cod") |
| 231 | + domain = webserver.host |
| 232 | + |
| 233 | + expiry = int(time.time() + 3600) # 1 hour from now |
| 234 | + |
| 235 | + path = "/simpleTest.html" |
| 236 | + |
| 237 | + cookie = PartialCookie( |
| 238 | + "fish", value, domain, path=path, http_only=True, secure=False, same_site=SameSite.LAX, expiry=expiry |
| 239 | + ) |
| 240 | + |
| 241 | + # Test |
| 242 | + driver.storage.set_cookie(cookie=cookie) |
| 243 | + |
| 244 | + driver.get(pages.url("simpleTest.html")) |
| 245 | + |
| 246 | + cookie_filter = CookieFilter( |
| 247 | + name="fish", |
| 248 | + value=value, |
| 249 | + domain=domain, |
| 250 | + path=path, |
| 251 | + http_only=True, |
| 252 | + secure=False, |
| 253 | + same_site=SameSite.LAX, |
| 254 | + expiry=expiry, |
| 255 | + ) |
| 256 | + |
| 257 | + descriptor = BrowsingContextPartitionDescriptor(driver.current_window_handle) |
| 258 | + |
| 259 | + result = driver.storage.get_cookies(filter=cookie_filter, partition=descriptor) |
| 260 | + key = result.partition_key |
| 261 | + |
| 262 | + # Verify |
| 263 | + assert len(result.cookies) > 0 |
| 264 | + result_cookie = result.cookies[0] |
| 265 | + |
| 266 | + assert result_cookie.name == "fish" |
| 267 | + assert result_cookie.value.value == value.value |
| 268 | + assert result_cookie.domain == domain |
| 269 | + assert result_cookie.path == path |
| 270 | + assert result_cookie.http_only is True |
| 271 | + assert result_cookie.secure is False |
| 272 | + assert result_cookie.same_site == SameSite.LAX |
| 273 | + assert result_cookie.expiry == expiry |
| 274 | + assert key.source_origin is not None |
| 275 | + assert key.user_context is not None |
| 276 | + assert key.user_context == "default" |
| 277 | + |
| 278 | + |
| 279 | +@pytest.mark.xfail_edge |
| 280 | +def test_get_all_cookies(driver, pages, webserver): |
| 281 | + """Test getting all cookies.""" |
| 282 | + # Setup |
| 283 | + driver.get(pages.url("simpleTest.html")) |
| 284 | + driver.delete_all_cookies() |
| 285 | + assert_no_cookies_are_present(driver) |
| 286 | + |
| 287 | + key1 = generate_unique_key() |
| 288 | + key2 = generate_unique_key() |
| 289 | + |
| 290 | + assert_cookie_is_not_present_with_name(driver, key1) |
| 291 | + assert_cookie_is_not_present_with_name(driver, key2) |
| 292 | + |
| 293 | + # Test |
| 294 | + params = CookieFilter() |
| 295 | + result = driver.storage.get_cookies(filter=params) |
| 296 | + |
| 297 | + count_before = len(result.cookies) |
| 298 | + |
| 299 | + driver.add_cookie({"name": key1, "value": "value"}) |
| 300 | + driver.add_cookie({"name": key2, "value": "value"}) |
| 301 | + |
| 302 | + driver.get(pages.url("simpleTest.html")) |
| 303 | + result = driver.storage.get_cookies(filter=params) |
| 304 | + |
| 305 | + # Verify |
| 306 | + assert len(result.cookies) == count_before + 2 |
| 307 | + cookie_names = [cookie.name for cookie in result.cookies] |
| 308 | + assert key1 in cookie_names |
| 309 | + assert key2 in cookie_names |
| 310 | + |
| 311 | + |
| 312 | +def test_delete_all_cookies(driver, pages, webserver): |
| 313 | + """Test deleting all cookies.""" |
| 314 | + # Setup |
| 315 | + driver.get(pages.url("simpleTest.html")) |
| 316 | + driver.delete_all_cookies() |
| 317 | + assert_no_cookies_are_present(driver) |
| 318 | + |
| 319 | + driver.add_cookie({"name": "foo", "value": "set"}) |
| 320 | + assert_some_cookies_are_present(driver) |
| 321 | + |
| 322 | + # Test |
| 323 | + driver.storage.delete_cookies(filter=CookieFilter()) |
| 324 | + |
| 325 | + # Verify |
| 326 | + assert_no_cookies_are_present(driver) |
| 327 | + |
| 328 | + driver.get(pages.url("simpleTest.html")) |
| 329 | + assert_no_cookies_are_present(driver) |
| 330 | + |
| 331 | + |
| 332 | +def test_delete_cookie_with_name(driver, pages, webserver): |
| 333 | + """Test deleting a cookie with a specific name.""" |
| 334 | + # Setup |
| 335 | + driver.get(pages.url("simpleTest.html")) |
| 336 | + driver.delete_all_cookies() |
| 337 | + assert_no_cookies_are_present(driver) |
| 338 | + |
| 339 | + key1 = generate_unique_key() |
| 340 | + key2 = generate_unique_key() |
| 341 | + |
| 342 | + driver.add_cookie({"name": key1, "value": "set"}) |
| 343 | + driver.add_cookie({"name": key2, "value": "set"}) |
| 344 | + |
| 345 | + assert_cookie_is_present_with_name(driver, key1) |
| 346 | + assert_cookie_is_present_with_name(driver, key2) |
| 347 | + |
| 348 | + # Test |
| 349 | + driver.storage.delete_cookies(filter=CookieFilter(name=key1)) |
| 350 | + |
| 351 | + # Verify |
| 352 | + assert_cookie_is_not_present_with_name(driver, key1) |
| 353 | + assert_cookie_is_present_with_name(driver, key2) |
| 354 | + |
| 355 | + driver.get(pages.url("simpleTest.html")) |
| 356 | + assert_cookie_is_not_present_with_name(driver, key1) |
| 357 | + assert_cookie_is_present_with_name(driver, key2) |
| 358 | + |
| 359 | + |
| 360 | +def test_add_cookies_with_different_paths(driver, pages, webserver): |
| 361 | + """Test adding cookies with different paths that are related to ours.""" |
| 362 | + # Setup |
| 363 | + driver.get(pages.url("simpleTest.html")) |
| 364 | + driver.delete_all_cookies() |
| 365 | + assert_no_cookies_are_present(driver) |
| 366 | + |
| 367 | + cookie1 = PartialCookie("fish", BytesValue(BytesValue.TYPE_STRING, "cod"), webserver.host, path="/simpleTest.html") |
| 368 | + |
| 369 | + cookie2 = PartialCookie("planet", BytesValue(BytesValue.TYPE_STRING, "earth"), webserver.host, path="/") |
| 370 | + |
| 371 | + # Test |
| 372 | + driver.storage.set_cookie(cookie=cookie1) |
| 373 | + driver.storage.set_cookie(cookie=cookie2) |
| 374 | + |
| 375 | + driver.get(pages.url("simpleTest.html")) |
| 376 | + |
| 377 | + # Verify |
| 378 | + assert_cookie_is_present_with_name(driver, "fish") |
| 379 | + assert_cookie_is_present_with_name(driver, "planet") |
| 380 | + |
| 381 | + driver.get(pages.url("formPage.html")) |
| 382 | + assert_cookie_is_not_present_with_name(driver, "fish") |
0 commit comments