Path: blob/trunk/py/test/selenium/webdriver/common/bidi_emulation_tests.py
1865 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.16import pytest1718from selenium.webdriver.common.bidi.emulation import Emulation, GeolocationCoordinates, GeolocationPositionError19from selenium.webdriver.common.bidi.permissions import PermissionState20from selenium.webdriver.common.window import WindowTypes212223def get_browser_geolocation(driver, user_context=None):24origin = driver.execute_script("return window.location.origin;")25driver.permissions.set_permission("geolocation", PermissionState.GRANTED, origin, user_context=user_context)2627return driver.execute_async_script("""28const callback = arguments[arguments.length - 1];29navigator.geolocation.getCurrentPosition(30position => {31const coords = position.coords;32callback({33latitude: coords.latitude,34longitude: coords.longitude,35accuracy: coords.accuracy,36altitude: coords.altitude,37altitudeAccuracy: coords.altitudeAccuracy,38heading: coords.heading,39speed: coords.speed,40timestamp: position.timestamp41});42},43error => {44callback({ error: error.message });45}46);47""")484950def test_emulation_initialized(driver):51"""Test that the emulation module is initialized properly."""52assert driver.emulation is not None53assert isinstance(driver.emulation, Emulation)545556def test_set_geolocation_override_with_coordinates_in_context(driver, pages):57"""Test setting geolocation override with coordinates."""58context_id = driver.current_window_handle59pages.load("blank.html")60coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)6162driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context_id])6364result = get_browser_geolocation(driver)6566assert "error" not in result, f"Geolocation error: {result.get('error')}"67assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"68assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"69assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"707172def test_set_geolocation_override_with_coordinates_in_user_context(driver, pages):73"""Test setting geolocation override with coordinates in a user context."""74# Create a user context75user_context = driver.browser.create_user_context()7677context_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context)7879driver.switch_to.window(context_id)80pages.load("blank.html")8182coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)8384driver.emulation.set_geolocation_override(coordinates=coords, user_contexts=[user_context])8586result = get_browser_geolocation(driver, user_context=user_context)8788assert "error" not in result, f"Geolocation error: {result.get('error')}"89assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"90assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"91assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"9293driver.browsing_context.close(context_id)94driver.browser.remove_user_context(user_context)959697def test_set_geolocation_override_all_coords(driver, pages):98"""Test setting geolocation override with coordinates."""99context_id = driver.current_window_handle100pages.load("blank.html")101coords = GeolocationCoordinates(10245.5, -122.4194, accuracy=10.0, altitude=100.2, altitude_accuracy=5.0, heading=183.2, speed=10.0103)104105driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context_id])106107result = get_browser_geolocation(driver)108109assert "error" not in result, f"Geolocation error: {result.get('error')}"110assert abs(result["latitude"] - coords.latitude) < 0.0001, f"Latitude mismatch: {result['latitude']}"111assert abs(result["longitude"] - coords.longitude) < 0.0001, f"Longitude mismatch: {result['longitude']}"112assert abs(result["accuracy"] - coords.accuracy) < 1.0, f"Accuracy mismatch: {result['accuracy']}"113assert abs(result["altitude"] - coords.altitude) < 0.0001, f"Altitude mismatch: {result['altitude']}"114assert abs(result["altitudeAccuracy"] - coords.altitude_accuracy) < 0.1, (115f"Altitude accuracy mismatch: {result['altitudeAccuracy']}"116)117assert abs(result["heading"] - coords.heading) < 0.1, f"Heading mismatch: {result['heading']}"118assert abs(result["speed"] - coords.speed) < 0.1, f"Speed mismatch: {result['speed']}"119120driver.browsing_context.close(context_id)121122123def test_set_geolocation_override_with_multiple_contexts(driver, pages):124"""Test setting geolocation override with multiple browsing contexts."""125# Create two browsing contexts126context1_id = driver.browsing_context.create(type=WindowTypes.TAB)127context2_id = driver.browsing_context.create(type=WindowTypes.TAB)128129coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)130131driver.emulation.set_geolocation_override(coordinates=coords, contexts=[context1_id, context2_id])132133# Test first context134driver.switch_to.window(context1_id)135pages.load("blank.html")136result1 = get_browser_geolocation(driver)137138assert "error" not in result1, f"Geolocation error in context1: {result1.get('error')}"139assert abs(result1["latitude"] - coords.latitude) < 0.0001, f"Context1 latitude mismatch: {result1['latitude']}"140assert abs(result1["longitude"] - coords.longitude) < 0.0001, f"Context1 longitude mismatch: {result1['longitude']}"141assert abs(result1["accuracy"] - coords.accuracy) < 1.0, f"Context1 accuracy mismatch: {result1['accuracy']}"142143# Test second context144driver.switch_to.window(context2_id)145pages.load("blank.html")146result2 = get_browser_geolocation(driver)147148assert "error" not in result2, f"Geolocation error in context2: {result2.get('error')}"149assert abs(result2["latitude"] - coords.latitude) < 0.0001, f"Context2 latitude mismatch: {result2['latitude']}"150assert abs(result2["longitude"] - coords.longitude) < 0.0001, f"Context2 longitude mismatch: {result2['longitude']}"151assert abs(result2["accuracy"] - coords.accuracy) < 1.0, f"Context2 accuracy mismatch: {result2['accuracy']}"152153driver.browsing_context.close(context1_id)154driver.browsing_context.close(context2_id)155156157def test_set_geolocation_override_with_multiple_user_contexts(driver, pages):158"""Test setting geolocation override with multiple user contexts."""159# Create two user contexts160user_context1 = driver.browser.create_user_context()161user_context2 = driver.browser.create_user_context()162163context1_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context1)164context2_id = driver.browsing_context.create(type=WindowTypes.TAB, user_context=user_context2)165166coords = GeolocationCoordinates(45.5, -122.4194, accuracy=10.0)167168driver.emulation.set_geolocation_override(coordinates=coords, user_contexts=[user_context1, user_context2])169170# Test first user context171driver.switch_to.window(context1_id)172pages.load("blank.html")173result1 = get_browser_geolocation(driver, user_context=user_context1)174175assert "error" not in result1, f"Geolocation error in user_context1: {result1.get('error')}"176assert abs(result1["latitude"] - coords.latitude) < 0.0001, (177f"User context1 latitude mismatch: {result1['latitude']}"178)179assert abs(result1["longitude"] - coords.longitude) < 0.0001, (180f"User context1 longitude mismatch: {result1['longitude']}"181)182assert abs(result1["accuracy"] - coords.accuracy) < 1.0, f"User context1 accuracy mismatch: {result1['accuracy']}"183184# Test second user context185driver.switch_to.window(context2_id)186pages.load("blank.html")187result2 = get_browser_geolocation(driver, user_context=user_context2)188189assert "error" not in result2, f"Geolocation error in user_context2: {result2.get('error')}"190assert abs(result2["latitude"] - coords.latitude) < 0.0001, (191f"User context2 latitude mismatch: {result2['latitude']}"192)193assert abs(result2["longitude"] - coords.longitude) < 0.0001, (194f"User context2 longitude mismatch: {result2['longitude']}"195)196assert abs(result2["accuracy"] - coords.accuracy) < 1.0, f"User context2 accuracy mismatch: {result2['accuracy']}"197198driver.browsing_context.close(context1_id)199driver.browsing_context.close(context2_id)200driver.browser.remove_user_context(user_context1)201driver.browser.remove_user_context(user_context2)202203204@pytest.mark.xfail_firefox205def test_set_geolocation_override_with_error(driver, pages):206"""Test setting geolocation override with error."""207context_id = driver.current_window_handle208pages.load("blank.html")209210error = GeolocationPositionError()211212driver.emulation.set_geolocation_override(error=error, contexts=[context_id])213214result = get_browser_geolocation(driver)215assert "error" in result, f"Expected geolocation error, got: {result}"216217218