Skip to content

Commit caf7136

Browse files
committed
add tests
1 parent 4eeef6b commit caf7136

File tree

1 file changed

+218
-0
lines changed

1 file changed

+218
-0
lines changed
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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.emulation import Emulation, GeolocationCoordinates
21+
from selenium.webdriver.common.window import WindowTypes
22+
23+
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+
@pytest.mark.xfail_edge
58+
def test_set_geolocation_override_with_coordinates_in_context(driver, pages):
59+
"""Test setting geolocation override with coordinates."""
60+
context_id = driver.current_window_handle
61+
pages.load("blank.html")
62+
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
63+
64+
driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context_id])
65+
66+
result = get_browser_geolocation(driver)
67+
68+
assert "error" not in result, f"Geolocation error: {result.get('error')}"
69+
assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"
70+
assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"
71+
assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"
72+
73+
74+
@pytest.mark.xfail_edge
75+
def test_set_geolocation_override_with_coordinates_in_user_context(driver, pages):
76+
"""Test setting geolocation override with coordinates in a user context."""
77+
# Create a user context
78+
user_context = driver.browser.create_user_context()
79+
80+
context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)
81+
82+
driver.switch_to.window(context_id)
83+
pages.load("blank.html")
84+
85+
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
86+
87+
driver.emulation.set_geolocation_override(coordinates=coords, user_contexts=[user_context])
88+
89+
result = get_browser_geolocation(driver)
90+
91+
assert "error" not in result, f"Geolocation error: {result.get('error')}"
92+
assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"
93+
assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"
94+
assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"
95+
96+
driver.browsing_context.close(context_id)
97+
driver.browser.remove_user_context(user_context)
98+
99+
100+
@pytest.mark.xfail_edge
101+
def test_set_geolocation_override_all_coords(driver, pages):
102+
"""Test setting geolocation override with coordinates."""
103+
context_id = driver.current_window_handle
104+
pages.load("blank.html")
105+
coords = GeolocationCoordinates(
106+
45.5, -122.4194, accuracy=10.0, altitude=100.2, altitude_accuracy=5.0, heading=183.2, speed=10.0
107+
)
108+
109+
driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context_id])
110+
111+
result = get_browser_geolocation(driver)
112+
113+
assert "error" not in result, f"Geolocation error: {result.get('error')}"
114+
assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"
115+
assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"
116+
assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"
117+
assert abs(result["altitude"] - coords.altitude) < 0.0001, f"Altitude mismatch: {result['altitude']}"
118+
assert abs(result["altitudeAccuracy"] - coords.altitude_accuracy) < 0.1, (
119+
f"Altitude accuracy mismatch: {result['altitudeAccuracy']}"
120+
)
121+
assert abs(result["heading"] - coords.heading) < 0.1, f"Heading mismatch: {result['heading']}"
122+
assert abs(result["speed"] - coords.speed) < 0.1, f"Speed mismatch: {result['speed']}"
123+
124+
driver.browsing_context.close(context_id)
125+
126+
127+
@pytest.mark.xfail_edge
128+
def test_set_geolocation_override_with_multiple_contexts(driver, pages):
129+
"""Test setting geolocation override with multiple browsing contexts."""
130+
# Create two browsing contexts
131+
context1_id = driver.browsing_context.create(type=WindowTypes.TAB)
132+
context2_id = driver.browsing_context.create(type=WindowTypes.TAB)
133+
134+
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
135+
136+
driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context1_id, context2_id])
137+
138+
# Test first context
139+
driver.switch_to.window(context1_id)
140+
pages.load("blank.html")
141+
result1 = get_browser_geolocation(driver)
142+
143+
assert "error" not in result1, f"Geolocation error in context1: {result1.get('error')}"
144+
assert abs(result1["latitude"] - coords.latitude) < 0.0001, f"Context1 latitude mismatch: {result1['latitude']}"
145+
assert abs(result1["longitude"] - coords.longitude) < 0.0001, f"Context1 longitude mismatch: {result1['longitude']}"
146+
assert abs(result1["accuracy"] - coords.accuracy) < 1.0, f"Context1 accuracy mismatch: {result1['accuracy']}"
147+
148+
# Test second context
149+
driver.switch_to.window(context2_id)
150+
pages.load("blank.html")
151+
result2 = get_browser_geolocation(driver)
152+
153+
assert "error" not in result2, f"Geolocation error in context2: {result2.get('error')}"
154+
assert abs(result2["latitude"] - coords.latitude) < 0.0001, f"Context2 latitude mismatch: {result2['latitude']}"
155+
assert abs(result2["longitude"] - coords.longitude) < 0.0001, f"Context2 longitude mismatch: {result2['longitude']}"
156+
assert abs(result2["accuracy"] - coords.accuracy) < 1.0, f"Context2 accuracy mismatch: {result2['accuracy']}"
157+
158+
driver.browsing_context.close(context1_id)
159+
driver.browsing_context.close(context2_id)
160+
161+
162+
@pytest.mark.xfail_edge
163+
def test_set_geolocation_override_with_multiple_user_contexts(driver, pages):
164+
"""Test setting geolocation override with multiple user contexts."""
165+
# Create two user contexts
166+
user_context1 = driver.browser.create_user_context()
167+
user_context2 = driver.browser.create_user_context()
168+
169+
context1_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context1)
170+
context2_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context2)
171+
172+
coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)
173+
174+
driver.emulation.set_geolocation_override(coordinates=coords, user_contexts=[user_context1, user_context2])
175+
176+
# Test first user context
177+
driver.switch_to.window(context1_id)
178+
pages.load("blank.html")
179+
result1 = get_browser_geolocation(driver)
180+
181+
assert "error" not in result1, f"Geolocation error in user_context1: {result1.get('error')}"
182+
assert abs(result1["latitude"] - coords.latitude) < 0.0001, (
183+
f"User context1 latitude mismatch: {result1['latitude']}"
184+
)
185+
assert abs(result1["longitude"] - coords.longitude) < 0.0001, (
186+
f"User context1 longitude mismatch: {result1['longitude']}"
187+
)
188+
assert abs(result1["accuracy"] - coords.accuracy) < 1.0, f"User context1 accuracy mismatch: {result1['accuracy']}"
189+
190+
# Test second user context
191+
driver.switch_to.window(context2_id)
192+
pages.load("blank.html")
193+
result2 = get_browser_geolocation(driver)
194+
195+
assert "error" not in result2, f"Geolocation error in user_context2: {result2.get('error')}"
196+
assert abs(result2["latitude"] - coords.latitude) < 0.0001, (
197+
f"User context2 latitude mismatch: {result2['latitude']}"
198+
)
199+
assert abs(result2["longitude"] - coords.longitude) < 0.0001, (
200+
f"User context2 longitude mismatch: {result2['longitude']}"
201+
)
202+
assert abs(result2["accuracy"] - coords.accuracy) < 1.0, f"User context2 accuracy mismatch: {result2['accuracy']}"
203+
204+
driver.browsing_context.close(context1_id)
205+
driver.browsing_context.close(context2_id)
206+
driver.browser.remove_user_context(user_context1)
207+
driver.browser.remove_user_context(user_context2)
208+
209+
210+
# the error param returns "invalid argument: Invalid input in "coordinates" error, might be bug from the spec/browser
211+
# @pytest.mark.xfail_edge
212+
# def test_set_geolocation_override_with_error(driver):
213+
# """Test setting geolocation override with error."""
214+
# context_id = driver.current_window_handle
215+
#
216+
# error = GeolocationPositionError()
217+
#
218+
# driver.emulation.set_geolocation_override(error=error, contexts=[context_id])

0 commit comments

Comments
 (0)