Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/bidi_emulation_tests.py
1865 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
import pytest
18
19
from selenium.webdriver.common.bidi.emulation import Emulation, GeolocationCoordinates, GeolocationPositionError
20
from selenium.webdriver.common.bidi.permissions import PermissionState
21
from selenium.webdriver.common.window import WindowTypes
22
23
24
def get_browser_geolocation(driver, user_context=None):
25
origin = driver.execute_script("return window.location.origin;")
26
driver.permissions.set_permission("geolocation", PermissionState.GRANTED, origin, user_context=user_context)
27
28
return driver.execute_async_script("""
29
const callback = arguments[arguments.length - 1];
30
navigator.geolocation.getCurrentPosition(
31
position => {
32
const coords = position.coords;
33
callback({
34
latitude: coords.latitude,
35
longitude: coords.longitude,
36
accuracy: coords.accuracy,
37
altitude: coords.altitude,
38
altitudeAccuracy: coords.altitudeAccuracy,
39
heading: coords.heading,
40
speed: coords.speed,
41
timestamp: position.timestamp
42
});
43
},
44
error => {
45
callback({ error: error.message });
46
}
47
);
48
""")
49
50
51
def test_emulation_initialized(driver):
52
"""Test that the emulation module is initialized properly."""
53
assert driver.emulation is not None
54
assert isinstance(driver.emulation, Emulation)
55
56
57
def test_set_geolocation_override_with_coordinates_in_context(driver, pages):
58
"""Test setting geolocation override with coordinates."""
59
context_id = driver.current_window_handle
60
pages.load("blank.html")
61
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
62
63
driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context_id])
64
65
result = get_browser_geolocation(driver)
66
67
assert "error" not in result, f"Geolocation error: {result.get('error')}"
68
assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"
69
assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"
70
assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"
71
72
73
def test_set_geolocation_override_with_coordinates_in_user_context(driver, pages):
74
"""Test setting geolocation override with coordinates in a user context."""
75
# Create a user context
76
user_context = driver.browser.create_user_context()
77
78
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
79
80
driver.switch_to.window(context_id)
81
pages.load("blank.html")
82
83
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
84
85
driver.emulation.set_geolocation_override(coordinates=coords, user_contexts=[user_context])
86
87
result = get_browser_geolocation(driver, user_context=user_context)
88
89
assert "error" not in result, f"Geolocation error: {result.get('error')}"
90
assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"
91
assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"
92
assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"
93
94
driver.browsing_context.close(context_id)
95
driver.browser.remove_user_context(user_context)
96
97
98
def test_set_geolocation_override_all_coords(driver, pages):
99
"""Test setting geolocation override with coordinates."""
100
context_id = driver.current_window_handle
101
pages.load("blank.html")
102
coords = GeolocationCoordinates(
103
45.5, -122.4194, accuracy=10.0, altitude=100.2, altitude_accuracy=5.0, heading=183.2, speed=10.0
104
)
105
106
driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context_id])
107
108
result = get_browser_geolocation(driver)
109
110
assert "error" not in result, f"Geolocation error: {result.get('error')}"
111
assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"
112
assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"
113
assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"
114
assert abs(result["altitude"] - coords.altitude) < 0.0001, f"Altitude mismatch: {result['altitude']}"
115
assert abs(result["altitudeAccuracy"] - coords.altitude_accuracy) < 0.1, (
116
f"Altitude accuracy mismatch: {result['altitudeAccuracy']}"
117
)
118
assert abs(result["heading"] - coords.heading) < 0.1, f"Heading mismatch: {result['heading']}"
119
assert abs(result["speed"] - coords.speed) < 0.1, f"Speed mismatch: {result['speed']}"
120
121
driver.browsing_context.close(context_id)
122
123
124
def test_set_geolocation_override_with_multiple_contexts(driver, pages):
125
"""Test setting geolocation override with multiple browsing contexts."""
126
# Create two browsing contexts
127
context1_id = driver.browsing_context.create(type=WindowTypes.TAB)
128
context2_id = driver.browsing_context.create(type=WindowTypes.TAB)
129
130
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
131
132
driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context1_id, context2_id])
133
134
# Test first context
135
driver.switch_to.window(context1_id)
136
pages.load("blank.html")
137
result1 = get_browser_geolocation(driver)
138
139
assert "error" not in result1, f"Geolocation error in context1: {result1.get('error')}"
140
assert abs(result1["latitude"] - coords.latitude) < 0.0001, f"Context1 latitude mismatch: {result1['latitude']}"
141
assert abs(result1["longitude"] - coords.longitude) < 0.0001, f"Context1 longitude mismatch: {result1['longitude']}"
142
assert abs(result1["accuracy"] - coords.accuracy) < 1.0, f"Context1 accuracy mismatch: {result1['accuracy']}"
143
144
# Test second context
145
driver.switch_to.window(context2_id)
146
pages.load("blank.html")
147
result2 = get_browser_geolocation(driver)
148
149
assert "error" not in result2, f"Geolocation error in context2: {result2.get('error')}"
150
assert abs(result2["latitude"] - coords.latitude) < 0.0001, f"Context2 latitude mismatch: {result2['latitude']}"
151
assert abs(result2["longitude"] - coords.longitude) < 0.0001, f"Context2 longitude mismatch: {result2['longitude']}"
152
assert abs(result2["accuracy"] - coords.accuracy) < 1.0, f"Context2 accuracy mismatch: {result2['accuracy']}"
153
154
driver.browsing_context.close(context1_id)
155
driver.browsing_context.close(context2_id)
156
157
158
def test_set_geolocation_override_with_multiple_user_contexts(driver, pages):
159
"""Test setting geolocation override with multiple user contexts."""
160
# Create two user contexts
161
user_context1 = driver.browser.create_user_context()
162
user_context2 = driver.browser.create_user_context()
163
164
context1_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context1)
165
context2_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context2)
166
167
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
168
169
driver.emulation.set_geolocation_override(coordinates=coords, user_contexts=[user_context1, user_context2])
170
171
# Test first user context
172
driver.switch_to.window(context1_id)
173
pages.load("blank.html")
174
result1 = get_browser_geolocation(driver, user_context=user_context1)
175
176
assert "error" not in result1, f"Geolocation error in user_context1: {result1.get('error')}"
177
assert abs(result1["latitude"] - coords.latitude) < 0.0001, (
178
f"User context1 latitude mismatch: {result1['latitude']}"
179
)
180
assert abs(result1["longitude"] - coords.longitude) < 0.0001, (
181
f"User context1 longitude mismatch: {result1['longitude']}"
182
)
183
assert abs(result1["accuracy"] - coords.accuracy) < 1.0, f"User context1 accuracy mismatch: {result1['accuracy']}"
184
185
# Test second user context
186
driver.switch_to.window(context2_id)
187
pages.load("blank.html")
188
result2 = get_browser_geolocation(driver, user_context=user_context2)
189
190
assert "error" not in result2, f"Geolocation error in user_context2: {result2.get('error')}"
191
assert abs(result2["latitude"] - coords.latitude) < 0.0001, (
192
f"User context2 latitude mismatch: {result2['latitude']}"
193
)
194
assert abs(result2["longitude"] - coords.longitude) < 0.0001, (
195
f"User context2 longitude mismatch: {result2['longitude']}"
196
)
197
assert abs(result2["accuracy"] - coords.accuracy) < 1.0, f"User context2 accuracy mismatch: {result2['accuracy']}"
198
199
driver.browsing_context.close(context1_id)
200
driver.browsing_context.close(context2_id)
201
driver.browser.remove_user_context(user_context1)
202
driver.browser.remove_user_context(user_context2)
203
204
205
@pytest.mark.xfail_firefox
206
def test_set_geolocation_override_with_error(driver, pages):
207
"""Test setting geolocation override with error."""
208
context_id = driver.current_window_handle
209
pages.load("blank.html")
210
211
error = GeolocationPositionError()
212
213
driver.emulation.set_geolocation_override(error=error, contexts=[context_id])
214
215
result = get_browser_geolocation(driver)
216
assert "error" in result, f"Expected geolocation error, got: {result}"
217
218