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