|
| 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.bidi.permissions import PermissionDescriptor, PermissionState |
| 21 | +from selenium.webdriver.common.window import WindowTypes |
| 22 | + |
| 23 | + |
| 24 | +def get_origin(driver): |
| 25 | + """Get the current window origin.""" |
| 26 | + return driver.execute_script("return window.location.origin;") |
| 27 | + |
| 28 | + |
| 29 | +def get_geolocation_permission(driver): |
| 30 | + """Get the geolocation permission state.""" |
| 31 | + script = """ |
| 32 | + const callback = arguments[arguments.length - 1]; |
| 33 | + navigator.permissions.query({ name: 'geolocation' }).then(permission => { |
| 34 | + callback(permission.state); |
| 35 | + }).catch(error => { |
| 36 | + callback(null); |
| 37 | + }); |
| 38 | + """ |
| 39 | + return driver.execute_async_script(script) |
| 40 | + |
| 41 | + |
| 42 | +def test_permissions_initialized(driver): |
| 43 | + """Test that the permissions module is initialized properly.""" |
| 44 | + assert driver.permissions is not None |
| 45 | + |
| 46 | + |
| 47 | +def test_can_set_permission_to_granted(driver, pages): |
| 48 | + """Test setting permission to granted state.""" |
| 49 | + pages.load("blank.html") |
| 50 | + |
| 51 | + origin = get_origin(driver) |
| 52 | + |
| 53 | + # Set geolocation permission to granted |
| 54 | + driver.permissions.set_permission("geolocation", PermissionState.GRANTED, origin) |
| 55 | + |
| 56 | + result = get_geolocation_permission(driver) |
| 57 | + assert result == PermissionState.GRANTED |
| 58 | + |
| 59 | + |
| 60 | +def test_can_set_permission_to_denied(driver, pages): |
| 61 | + """Test setting permission to denied state.""" |
| 62 | + pages.load("blank.html") |
| 63 | + |
| 64 | + origin = get_origin(driver) |
| 65 | + |
| 66 | + # Set geolocation permission to denied |
| 67 | + driver.permissions.set_permission("geolocation", PermissionState.DENIED, origin) |
| 68 | + |
| 69 | + result = get_geolocation_permission(driver) |
| 70 | + assert result == PermissionState.DENIED |
| 71 | + |
| 72 | + |
| 73 | +def test_can_set_permission_to_prompt(driver, pages): |
| 74 | + """Test setting permission to prompt state.""" |
| 75 | + pages.load("blank.html") |
| 76 | + |
| 77 | + origin = get_origin(driver) |
| 78 | + |
| 79 | + # First set to denied, then to prompt since most of the time the default state is prompt |
| 80 | + driver.permissions.set_permission("geolocation", PermissionState.DENIED, origin) |
| 81 | + driver.permissions.set_permission("geolocation", PermissionState.PROMPT, origin) |
| 82 | + |
| 83 | + result = get_geolocation_permission(driver) |
| 84 | + assert result == PermissionState.PROMPT |
| 85 | + |
| 86 | + |
| 87 | +def test_can_set_permission_for_user_context(driver, pages): |
| 88 | + """Test setting permission for a specific user context.""" |
| 89 | + # Create a user context |
| 90 | + user_context = driver.browser.create_user_context() |
| 91 | + |
| 92 | + context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context) |
| 93 | + |
| 94 | + # Navigate both contexts to the same page |
| 95 | + pages.load("blank.html") |
| 96 | + original_window = driver.current_window_handle |
| 97 | + driver.switch_to.window(context_id) |
| 98 | + pages.load("blank.html") |
| 99 | + |
| 100 | + origin = get_origin(driver) |
| 101 | + |
| 102 | + # Get original permission states |
| 103 | + driver.switch_to.window(original_window) |
| 104 | + original_permission = get_geolocation_permission(driver) |
| 105 | + |
| 106 | + driver.switch_to.window(context_id) |
| 107 | + |
| 108 | + # Set permission only for the user context using PermissionDescriptor |
| 109 | + descriptor = PermissionDescriptor("geolocation") |
| 110 | + driver.permissions.set_permission(descriptor, PermissionState.GRANTED, origin, user_context) |
| 111 | + |
| 112 | + # Check that the original window's permission hasn't changed |
| 113 | + driver.switch_to.window(original_window) |
| 114 | + updated_original_permission = get_geolocation_permission(driver) |
| 115 | + assert updated_original_permission == original_permission |
| 116 | + |
| 117 | + # Check that the new context's permission was updated |
| 118 | + driver.switch_to.window(context_id) |
| 119 | + updated_new_context_permission = get_geolocation_permission(driver) |
| 120 | + assert updated_new_context_permission == PermissionState.GRANTED |
| 121 | + |
| 122 | + driver.browsing_context.close(context_id) |
| 123 | + driver.browser.remove_user_context(user_context) |
| 124 | + |
| 125 | + |
| 126 | +def test_invalid_permission_state_raises_error(driver, pages): |
| 127 | + """Test that invalid permission state raises ValueError.""" |
| 128 | + pages.load("blank.html") |
| 129 | + origin = get_origin(driver) |
| 130 | + |
| 131 | + # set permission using PermissionDescriptor |
| 132 | + descriptor = PermissionDescriptor("geolocation") |
| 133 | + |
| 134 | + with pytest.raises(ValueError, match="Invalid permission state"): |
| 135 | + driver.permissions.set_permission(descriptor, "invalid_state", origin) |
| 136 | + |
| 137 | + |
| 138 | +def test_permission_states_constants(): |
| 139 | + """Test that permission state constants are correctly defined.""" |
| 140 | + assert PermissionState.GRANTED == "granted" |
| 141 | + assert PermissionState.DENIED == "denied" |
| 142 | + assert PermissionState.PROMPT == "prompt" |
0 commit comments